-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
56 lines (45 loc) · 926 Bytes
/
Source.cpp
File metadata and controls
56 lines (45 loc) · 926 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
56
#include <iostream>
void QuickSort(int array[], int left, int right);
void ShowArray(int array[], int array_size);
int main()
{
int array[9] = { 5,4,7,6,8,3,1,2,9 };
int left = 0, right = 8;
std::cout << "ƒ\[ƒg‘O" << std::endl;
ShowArray(array, 9);
QuickSort(array, left, right);
std::cout << "ƒ\[ƒgŒã" << std::endl;
ShowArray(array, 9);
return 0;
}
void QuickSort(int array[], int left, int right)
{
int i = left, k = right;
int tmp;
int pivot = array[(left + right) / 2];
while (i <= k)
{
while (array[i] < pivot)
i++;
while (array[k] > pivot)
k--;
if (i <= k)
{
tmp = array[i];
array[i] = array[k];
array[k] = tmp;
i++;
k--;
}
}
if (left < k)
QuickSort(array, left, k);
if (i < right)
QuickSort(array, i, right);
}
void ShowArray(int array[], int array_size)
{
for (int i = 0; i < array_size; i++)
std::cout << array[i] << " " ;
std::cout << std::endl;
}