-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
70 lines (56 loc) · 1.42 KB
/
QuickSort.cpp
File metadata and controls
70 lines (56 loc) · 1.42 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
using namespace std;
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
int main()
{
int const MAX = 100;
int arraySize;
cout << "Enter arraySize: " << endl;
cin >> arraySize;
int a[arraySize];
srand(time(0));
//filling the array with randomly generated integers
for (int i = 0; i < arraySize; i++) {
a[i] = rand() % MAX;
}
cout << endl << endl;
cout << "Array before quickSorting: " << endl;
for (int i = 0; i < arraySize; i++) {
cout << a[i] << " ";
}
cout<<endl;
quickSort(a, 0, arraySize - 1);
cout << "Array after quickSorting: " << endl;
for (int i = 0; i < arraySize; i++ ){
cout << a[i] << " ";
}
cout << endl << endl;
return 0;
}