//快速排序法
int QuickSort_process3(int *a, int low, int high)
{
int l, h, temp;
l = low;
h = high;
temp = a[low];
while (l < h){
while (l< h&&a[h] >= temp)
--h;
if (l < h)
a[l] = a[h];
while (l < h&&a[l] < temp)
++l;
if (l < h)
a[h] = a[l];
}
a[h] = temp;
return h;
}
void QuickSort3(int *a, int low,int high)
{
int pos;
if (low < high){
pos = QuickSort_process3(a,low,high);
QuickSort3(a,low,pos-1);
QuickSort3(a,pos+1,high);
}
}
int getTop_N_Numbers(int *a,int n)
{
int pos, curpos, e;
do{
printf("Please input a numbers(less than %d and bigger than 0).\n", n);
scanf("%d",&e);
} while (!(e<=n&&e>=1));
pos = n - e;/*在一個從小到大排序的陣列中,最大第e個數的下標恰好是n-e*/
curpos = QuickSort_process3(a, 0, n - 1);/*一趟快排結束時,肯定會有一個已經確定了位置的數,返回它的下標*/
while (1){
if (curpos == pos){/*當要找的第幾最大的數的位子已經確定後,說明,比它還要大的數也已經確定了,就不再排序了,直接返回*/
break;
}
else if (curpos < pos){/*在後半部找*/
curpos = QuickSort_process3(a,curpos+1,n-1);
}
else{/*在前半部分找*/
curpos = QuickSort_process3(a,0,curpos-1);
}
}
return pos;
}
void show_TopN_numbers(int *a,int n)
{
int k = getTop_N_Numbers(a, n);
int i;
printf("The Top %d numbers are :\n",n-k);
for (i = k; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
}