-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
154 lines (123 loc) · 2.49 KB
/
QuickSort.cpp
File metadata and controls
154 lines (123 loc) · 2.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include<cstdlib>
#include<ctime>
#include <algorithm>
#define N 10
using namespace std;
void exchange(int A[], int i, int j){
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
/*
* pivot is the first element using the method in algorithm
*/
/*
int Partition(int A[], int p, int q){
int pivot=A[p];
int j;
int i=p;
for(j=p+1;j<=q;j++){
if(A[j]<=pivot){
i++;
exchange(A,i,j);
}
}
exchange(A,p,i);
return i;
}
*/
/*
* pivot is the middle of three random element
*/
/*
int RandPivot(int A[],int p, int q){
srand((unsigned)time(NULL));
int r3[3];
int a3[3];
int i=0;
int k;
for(k=0;k<3;k++){
r3[i]=rand()%(p-q)+p;
a3[i]=A[r3[i]];
}
sort(a3,a3+3);
for(k=0;k<3;k++){
if(A[r3[i]]==a3[1]){
return r3[i];
}
}
}
*/
/*
* quick sort method 1 定轴法
*/
/*
int Partition(int A[], int p, int q){
int pivot=A[p];
int left=p+1;
int right=q;
while(left<=right){
while(A[left]<pivot&&left<=right)
left++;
while(A[right]>pivot&&right>=left)
right--;
if(left<right){
exchange(A,left,right);
left++;
right--;
}
}
exchange(A,p,right);
return right;
}
*/
/*
* quick sort method 2 挖坑法
*/
int Partition(int A[], int p, int q){
int pivot=A[p];
int low=p;
int high=q;
while(low<high){
while(low<high && A[high]>=pivot){ //第一次循环一定要从右向左,因为A[low]是一个坑
high--;
}
if(low<high){
A[low]=A[high]; //A[low]的值已经保存起来了,所以可以填坑;然后A[high]又变成一个坑
low++;
}
while(low<high && A[low]<pivot)
low++;
if(low<high){
A[high]=A[low]; //A[high]的值已经保存起来了,所以可以填坑;然后A[low]又变成一个坑
high--;
}
}
A[low]=pivot; //最后填A[low]的坑
return low;
}
void QuickSort(int A[], int p, int q){
int r;
if(p<q){
r=Partition(A,p,q);
QuickSort(A,p,r-1);
QuickSort(A,r+1,q);
}
}
int main() {
int A[N];
srand((unsigned)time(NULL));
for(int i=0;i<N;i++){
A[i]=rand()%(100-1);
}
for(int k:A){
cout<<k<<endl;
}
cout<<endl;
QuickSort(A,0,N-1);
for(int a:A){
cout<<a<<endl;
}
return 0;
}