//快速排序,主要思想是通过一趟排序将待排序的记录分割成相邻的两个区域,
//其中一个区域中的关键字均比另一区域中记录的关键字要小,在分别对这两个
//区域进行排序,以达到整个序列有序。一般情况是O(logn),最坏情况是O(n)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
int QSorting(int *result ,int first,int end)
{
int key = result[first];
while(first < end)
{
while(first < end&&result[end] >= key)
end--;
if(first != end)
result[first++] = result[end];
while(first < end && result[first] <= key)
first++;
if(first != end)
result[end--] = result[first];
}
result[first] = key;
return first;
}
void Qsort(int *result,int first,int end)
{
if(first < end )
{
int middle = QSorting(result,first,end);
Qsort(result,first,middle-1);
Qsort(result,middle+1,end);
}
}
int main()
{
int value[N] = {18,94,61,87,34,31,78,56,14,17};
int i = 0;
int sum = 0;
Qsort(value,0,N-1);
for(i = 0 ;i < N;i++)
{
sum = sum + value[i];
printf("%d\n",value[i]);
}
printf("the result:%d\n",value[N/2]-sum/N);
return 0;
}
|