• Define the array : {0, 1, 5, 4, 2, 5, 7, 8, 3, 4, 5, 1, 1, 2, 3, 6, 7, 8}
• Sort by ascending
• Deduplication
#include<stdio.h>
// 快速排序,升序排序
void quiksort(int a[],int low,int high)
{
int i = low;
int j = high;
int temp = a[i];
if( low < high)
{
while(i < j)
{
while((a[j] >= temp) && (i < j))
{
j--;
}
a[i] = a[j];
while((a[i] <= temp) && (i < j))
{
i++;
}
a[j]= a[i];
}
a[i] = temp;
quiksort(a,low,i-1);
quiksort(a,j+1,high);
}
else
{
return;
}
}
void deduplication(int a[], int dataNum){
printf("Deduplication:");
int j;
int iter = a[0];
printf("%d", a[0]);
for(j = 1; j < dataNum; j++){
if(iter != a[j]){
printf(" %d", a[j]);
iter = a[j];
}
}
printf("\n");
}
int main(){
int arry[18];
int i;
printf("initial values:");
for(i = 0; i < 18; i++)
scanf("%d", &arry[i]);
quiksort(arry, 0, 17);
printf("sort values:");
for(i = 0; i < 17; i++)
printf("%d ", arry[i]);
printf("%d\n", arry[i]);
deduplication(arry, 18);
return 0;
}
找 啊哈算法,第一章有解法,