-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuickSort.cpp
60 lines (44 loc) · 1.07 KB
/
QuickSort.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
// Assignment 6 :- Write a program for analysis of quick sort by using deterministic and randomized variant
#include <bits/stdc++.h>
using namespace std;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int generateRandomPivot(int low, int high) {
srand(time(NULL));
return low + rand() % (high - low + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = generateRandomPivot(low, high);
int pivotValue = arr[pivotIndex];
swap(&arr[pivotIndex], &arr[high]);
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivotValue) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
quickSort(arr, low, i);
quickSort(arr, i+2, high);
}
}
int main() {
int arr[] = {5, 2, 7, 3, 1, 6, 4, 8};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
quickSort(arr, 0, n-1);
cout << "\nSorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
//Time Complexity - O(N^2 ).