Skip to content

Commit ff0a972

Browse files
committed
增加了算法导论版的快排
1 parent 9edfc57 commit ff0a972

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

各种排序/快速排序/QuickSort.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Email:[email protected]
88

99
void Quick_Sort(int *,int,int);
1010
int findPoss(int *,int,int);
11+
int Patrition(int *,int,int);
12+
void swap(int *,int *);
1113

1214
int 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
*/
5557
int 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

Comments
 (0)