forked from TechVine/DSA--Hacktoberfest-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.cpp
72 lines (61 loc) · 1.6 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
61
62
63
64
65
66
67
68
69
70
71
72
/* **************************************************************
* TIME COMPLEXITY: O(N^2)
* AVERAGE : O(NlogN)
* BEST : O(NlogN)
* **************************************************************/
#include <iostream>
using namespace std;
const int N = 1e5+10;
int arr[N];
void swap(int *a, int *b)
{
int temp = *a; // auxiliary space
*a = *b;
*b = temp;
}
void display(int *arr, int startIndex, int endIndex)
{
for (int i = startIndex; i < endIndex; i++)
cout << arr[i] << " ";
cout << '\n';
}
int partition(int *arr, int low, int high)
{
int i = low + 1;
int j = high;
int pivot = arr[low];
do {
while (arr[i] <= pivot) // for greater element
i++;
while (arr[j] > pivot) // for smaller element
j--;
if (i < j) swap(&arr[i], &arr[j]);
} while (i < j);
swap(&arr[low], &arr[j]);
return j;
}
void quickSort(int *arr, int low, int high)
{
int partitionIndex;
if (low < high)
{
partitionIndex = partition(arr, low, high);
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}
int main(void)
{
// int arr[] = {1, 8, 3, 9, 4, 5, 7};
int size;
cout << "Enter the size: "; cin >> size;
cout << "Enter the " << size << " elements" << endl ;
for (int i = 0; i < size; i++)
cin >> arr[i];
cout << "\n Before Sort\n";
display(arr, 0, size);
cout << "\n QuickSort: ";
quickSort(arr, 0, size - 1);
display(arr, 0, size);
return 0;
}