-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursive Quicksort and Non Recursive QuickSort
87 lines (72 loc) · 2.15 KB
/
Recursive Quicksort and Non Recursive QuickSort
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
83
84
85
86
87
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <stack>
#include <ctime>
using namespace std;
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void nonRecursiveQuickSort(int arr[], int low, int high) {
stack<int> st;
st.push(low);
st.push(high);
while (!st.empty()) {
high = st.top();
st.pop();
low = st.top();
st.pop();
int pi = partition(arr, low, high);
if (pi - 1 > low) {
st.push(low);
st.push(pi - 1);
}
if (pi + 1 < high) {
st.push(pi + 1);
st.push(high);
}
}
}
void recursiveQuickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
recursiveQuickSort(arr, low, pi - 1);
recursiveQuickSort(arr, pi + 1, high);
}
}
int main()
{
int size;
cout << "total elements in array:";
cin >> size;
int arr[size];
srand(time(NULL));
for (int i = 0; i < size; i++) {
arr[i] = rand();
}
auto startTimeForRecurcive = chrono::high_resolution_clock::now();
recursiveQuickSort(arr, 0, size - 1);
auto endTimeForRecurcive = chrono::high_resolution_clock::now();
auto durationForRecurcive = chrono::duration_cast<chrono::nanoseconds>(endTimeForRecurcive - startTimeForRecurcive).count();
auto startTimeForIterative = chrono::high_resolution_clock::now();
nonRecursiveQuickSort(arr,0,size-1);
auto endTimeForIterative = chrono::high_resolution_clock::now();
auto durationForNonRecursive = chrono::duration_cast<chrono::nanoseconds>(endTimeForIterative - startTimeForIterative).count();
cout << "time taken for recurcive quick sort is " << durationForRecurcive/1000 << endl;
cout << "time taken for non recursive quick sort is " << durationForNonRecursive/1000 << endl;
return 0;
}