1 #include<stdio.h>
2 #include<math.h>
3 #define N 10
4 void quicksort(int a[],int low,int high);
5 int split(int a[],int low,int high);
6 main()
7 {
8 int a[N],i;
9 printf("Enter %d numbers to be sorted: ",N);
10 for(i=0;i<N;i++)
11 {
12 scanf("%d",&a[i]);
13 }
14 quicksort(a,0,N-1);
15 printf("In sorted order: ");
16 for(i=0;i<N;i++)
17 {
18 printf("%d ",a[i]);
19
20 }
21 printf("\n");
22 getch();
23 }
24 void quicksort(int a[],int low,int high)
25 {
26 int middle;
27 if(low>=high)return;
28 middle=split(a,low,high);
29 quicksort(a,low,middle-1);
30 quicksort(a,middle+1,high);
31 }
32 int split(int a[],int low,int high)
33 {
34 int part_element=a[low];
35 for(;;){
36 while(low<high&&part_element<=a[high])
37 high--;
38 if(low>=high) break;
39 a[low++]=a[high];
40
41 while(low<high&&a[low]<=part_element)
42 low++;
43 if(low>=high) break;
44 a[high--]=a[low];
45 }
46 a[high]=part_element;
47 return high;
48 }
阅读(668) | 评论(0) | 转发(0) |