-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.cpp
58 lines (49 loc) · 1.17 KB
/
quickSort.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
50
51
52
53
54
55
56
57
58
/*************************************************************************
> File Name: quickSork.cpp
> Author:
> Mail:
> Created Time: 2017年05月22日 星期一 17时11分44秒
************************************************************************/
#include<iostream>
using namespace std;
#define MAX 10
int ParTation(int a[], int lo, int hi);
void QuickSort(int a[], int lo, int hi)
{
int p = 0;
if(lo <= hi)
{
p = ParTation(a, lo, hi);
QuickSort(a, lo, p - 1);
QuickSort(a, p + 1, hi);
}
}
int ParTation(int a[], int lo, int hi)
{
int tmp = a[hi];
int i = lo - 1;
int j = lo;
for(j = lo; j <= hi - 1; ++j)
if(a[j] < tmp)
{
swap(a[i + 1], a[j]);
i++;
}
swap(a[hi], a[i + 1]);
return i+1;
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int i = 0;
int a[MAX] = {2, 3, 53, 564, 23, 43, 4, 32, 34, 8};
QuickSort(a, 0, MAX-1);
for(i = 0; i < MAX; ++i)
cout<<a[i]<<endl;
return 0;
}