-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort
More file actions
55 lines (49 loc) · 931 Bytes
/
Copy pathQuickSort
File metadata and controls
55 lines (49 loc) · 931 Bytes
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
#include<iostream>
#include<ctime>
using namespace std;
const int N = 1000;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void Qsort(int Array[], int begin, int end)
{
if (begin >= end)
return;
int key = Array[begin];
int i = begin, j = end;
while (i < j)
{
while (i < j && Array[j] >= key)
--j;
Array[i] = Array[j];
while (i < j && Array[i] <= key)
++i;
Array[j] = Array[i];
}
Array[i] = key;
Qsort(Array, begin, i - 1);
Qsort(Array, i + 1, end);
}
void QuickSort(int Array[], int Num)
{
Qsort(Array, 0, Num - 1);
}
int main()
{
int Array[N];
srand((unsigned)time(nullptr));
for (int i = 0; i < N; ++i)
Array[i] = rand() % N + 3;
for (int i = 0; i < N; ++i)
cout << Array[i] << " ";
cout << endl;
cout << "---------------------------------------" << endl;
QuickSort(Array, N);
for (int i = 0; i < N; ++i)
cout << Array[i] << " ";
cout << endl;
system("pause");
}