-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
82 lines (64 loc) · 1.06 KB
/
Copy pathquickSort.cpp
File metadata and controls
82 lines (64 loc) · 1.06 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 32;
int comp = 0;
void print(int* input)
{
for(int i = 1; i <= SIZE; i++)
cout << input[i] << " ";
cout << endl;
}
void q_sort(int arr[], int left, int right)
{
int i = left, j = right;
int temp;
int pivot = arr[(left + right) / 2];
while(i <= j)
{
while(arr[i] < pivot)
{
i++;
comp++;
}
while(arr[j] > pivot)
{
j--;
comp++;
}
if(i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
comp++;
}
};
if(left < j){
q_sort(arr, left, j);
}
if(i < right){
q_sort(arr, i, right);
}
}
int main()
{
srand(time(NULL));
cout << "The array consists of " << SIZE << " elements: " << endl;
int input[SIZE+1] = { 0 };
for(int i = 1; i <= SIZE; i++)
{
int j = rand() % 100+1;
input[i] = j;
}
cout << "Input: " << endl;
print(input);
q_sort(input, 1, SIZE);
cout << "Output: " << endl;
print(input);
cout << "Count: " << comp << endl;
return 0;
}