-
Notifications
You must be signed in to change notification settings - Fork 11
/
快速排序.cpp
49 lines (48 loc) · 1.35 KB
/
快速排序.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
int Partition(int r[ ], int first, int end);
void QuickSort(int r[ ], int first, int end);
int main()
{
int r[]= {23,13,35,6,19,50,28};
QuickSort(r,0,6);
for(int i=0; i<7; i++)
cout<<r[i]<<" ";
return 0;
}
int Partition(int r[ ], int first, int end) //划分
{
int i = first, j=end; //初始化待划分区间
while (i < j)
{
while (i < j && r[i] <= r[j])
j--; //右侧扫描
if (i < j)
{
int temp = r[i];
r[i] = r[j];
r[j] = temp; //将较小记录交换到前面
i++;
}
while (i < j && r[i] <= r[j])
i++; //左侧扫描
if (i < j)
{
int temp = r[i];
r[i] = r[j];
r[j] = temp; //将较大记录交换到后面
j--;
}
}
return i; // 返回轴值记录的位置
}
void QuickSort(int r[ ], int first, int end) //快速排序
{
int pivot;
if (first < end)
{
pivot = Partition(r, first, end); //划分,pivot是轴值在序列中的位置
QuickSort(r, first, pivot-1); //求解子问题1,对左侧子序列进行快速排序
QuickSort(r, pivot+1, end); //求解子问题2,对右侧子序列进行快速排序
}
}