-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandQuicksort.cpp
65 lines (58 loc) · 1.14 KB
/
RandQuicksort.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
59
60
61
62
63
64
65
/*************************************************************************
> File Name: RandQuicksort.cpp
> Author:
> Mail:
> Created Time: 2017年05月22日 星期一 17时29分33秒
************************************************************************/
#include<iostream>
using namespace std;
#define MAX 10
int RandParTation(int a[], int lo, int hi);
int ParTation(int a[], int lo, int hi);
int RandParTation(int a[], int lo, int hi)
{
int x = rand() % (hi - lo + 1) + lo;
int q = 0;
swap(a[x], a[hi]);
q = ParTation(a, lo, hi);
return q;
}
void QuickSort(int a[], int lo, int hi)
{
int p = 0;
if(lo <= hi)
{
p = RandParTation(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;
}