-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick sort.cpp
84 lines (59 loc) · 2.01 KB
/
quick sort.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
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std; // this means that you do not need to start the line with std
void quick_sort(int quick_array[], int index_low, int index_high){
if (index_low >= index_high){
return;
}
int temp_swap;
int temp_low;
int temp_high;
temp_low = index_low + 1;
temp_high = index_high;
int pivot = quick_array[index_low];
bool finished = 0;
while(finished == 0){
while (temp_low <= temp_high and quick_array[temp_low] <= pivot){
temp_low = temp_low + 1;
}
while (temp_low <= temp_high and quick_array[temp_high] >= pivot){
temp_high = temp_high - 1;
}
if (temp_low < temp_high){
temp_swap = quick_array[temp_low];
quick_array[temp_low] = quick_array[temp_high];
quick_array[temp_high] = temp_swap;
}
else{
finished = 1;
}
}
temp_swap = quick_array[index_low];
quick_array[index_low] = quick_array[temp_high];
quick_array[temp_high] = temp_swap;
quick_sort(quick_array,index_low, temp_high - 1);
quick_sort(quick_array, temp_high + 1, index_high);
}
int main(int argc, char** argv)
{
unsigned int n = 0;
cout << "Hello this is a test of the quick sort in c++! please enter the size of the unordered array: ";
cin >> n;
cout << "You have chosen for the array to be: " << n << "\n";
int* quick_array = new int[n];
for (int i = 0; i < n; i++) {
cout << "Please enter the: " << i + 1 << " item in the list ";
int x = 0;
cin >> x;
quick_array[i] = x;
}
quick_sort(quick_array, 0, n-1);//getting all user inputs! quick sort starts here!
cout << "The sorted array is: "; //outputting the sorted array
for (int i = 0; i < n; i++) {
if (i + 1 == n) {
cout << quick_array[i];
}
else {
cout << quick_array[i] << ",";
}
}
}