88
99void Quick_Sort (int *,int ,int );
1010int findPoss (int *,int ,int );
11+ int Patrition (int *,int ,int );
12+ void swap (int *,int *);
1113
1214int main ()
1315{
@@ -41,7 +43,7 @@ void Quick_Sort(int *a,int low,int high)
4143
4244 if (low < high)
4345 {
44- pos = findPoss (a,low,high);
46+ pos = Patrition (a,low,high);
4547 Quick_Sort (a,low,pos-1 ); // 左边子序列排序
4648 Quick_Sort (a,pos+1 ,high); // 右边子序列排序
4749 }
@@ -54,6 +56,9 @@ void Quick_Sort(int *a,int low,int high)
5456*/
5557int findPoss (int *a,int low,int high)
5658{
59+ if (a==NULL || low>high)
60+ return -1 ;
61+
5762 int val = a[low];
5863 while (low < high)
5964 {
@@ -70,3 +75,35 @@ int findPoss(int *a,int low,int high)
7075 a[low] = val;
7176 return low;
7277}
78+
79+ /*
80+ 算法导论版快速排序
81+ */
82+ int Patrition (int *a,int low ,int high)
83+ {
84+ if (a==NULL || low>high)
85+ return -1 ;
86+
87+ int small = low-1 ;
88+ int j;
89+ for (j=low;j<high;j++)
90+ {
91+ if (a[j]<a[high])
92+ {
93+ small++;
94+ if (small != j)
95+ swap (&a[small],&a[j]);
96+ }
97+ }
98+ small++;
99+ swap (&a[small],&a[high]);
100+ return small;
101+ }
102+
103+
104+ void swap (int *a,int *b)
105+ {
106+ int temp = *a;
107+ *a = *b;
108+ *b = temp;
109+ }
0 commit comments