-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
52 lines (42 loc) · 921 Bytes
/
quick_sort.cpp
File metadata and controls
52 lines (42 loc) · 921 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
#include <iostream>
using namespace std;
void quick_sort(int array[], int left, int right) {
int i = left, j = right;
int temp;
int center = array[(left + right) / 2];
while (i <= j) {
while (array[i] < center)
i++;
while (array[j] > center)
j--;
if (i <= j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (left < j)
quick_sort(array, left, j);
if (right > i)
quick_sort(array, i, right);
}
int main() {
size_t size;
int value;
cout << "Enter the size of array: ";
cin >> size;
int* array = new int[size];
cout << "Enter " << size << " integer elements of array: ";
for (size_t i = 0; i < size; ++i) {
cin >> array[i];
}
quick_sort(array, 0, size - 1);
cout << "Sorted array: ";
for (size_t i = 0; i < size; ++i) {
cout << array[i] << " ";
}
cout << endl;
return 0;
}