forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cpp
81 lines (69 loc) · 1.86 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
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;
int partition(int* arr, int start, int end)
{
// assuming last element as pivotElement
int index = 0, pivotElement = arr[end], pivotIndex;
int* temp = new int[end - start + 1]; // making an array whose size is equal to current partition range...
for (int i = start; i <= end; i++) // pushing all the elements in temp which are smaller than pivotElement
{
if(arr[i] < pivotElement)
{
temp[index] = arr[i];
index++;
}
}
temp[index] = pivotElement; // pushing pivotElement in temp
index++;
for (int i = start; i < end; i++) // pushing all the elements in temp which are greater than pivotElement
{
if(arr[i] > pivotElement)
{
temp[index] = arr[i];
index++;
}
}
// all the elements now in temp array are order :
// leftmost elements are lesser than pivotElement and rightmost elements are greater than pivotElement
index = 0;
for (int i = start; i <= end; i++) // copying all the elements to original array i.e arr
{
if(arr[i] == pivotElement)
{
// for getting pivot index in the original array.
// we need the pivotIndex value in the original and not in the temp array
pivotIndex = i;
}
arr[i] = temp[index];
index++;
}
return pivotIndex; // returning pivotIndex
}
void quickSort(int* arr, int start, int end)
{
if(start < end)
{
int partitionIndex = partition(arr, start, end); // for getting partition
quickSort(arr, start, partitionIndex - 1); // sorting left side array
quickSort(arr, partitionIndex + 1, end); // sorting right side array
}
return;
}
int main()
{
int size = 9;
int arr[size] = {5, 12, 7, 1, 13, 2 ,23, 11, 18};
cout << "Unsorted array : ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
printf("\n");
quickSort(arr, 0, size - 1);
cout << "Sorted array : ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
return 0;
}