数据结构---快速排序

前提是你 提交于 2020-03-10 13:54:58
 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int a[6] = {2,1,0,5,4,3};
 6     int i;
 7     QuickSort(a,0,5);//第二个参数表示第一个元素的下标  第三个参数表示最后的元素的下标
 8 
 9     for(i= 0 ;i<6;++i)
10         printf("%d",a[i]);
11     printf("\n");
12     return 0;
13 }
14 
15 void QuickSort(int *a , int low ,int high)
16 {
17     int pos;
18     if(low<high)
19     {
20         pos = FindPos(a,low,high);
21         QuickSort(a,low,pos-1);
22         QuickSort(a,pos+1,high);
23     }
24 }
25 
26 int FindPos(int *a , int low , int high)
27 {
28     int val = a[low];
29     while(low<high)
30     {
31         while(low<high && a[high]>=val )
32             --high;
33         a[low] = a[high];
34         while(low<high && a[low]<=val )
35             ++low;
36         a[high] = a[low];
37     }//终止while循环之后low和high 一定是相等的
38     a[low] = val;
39 
40     return low;//low也可以为high
41 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!