forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.cpp
61 lines (50 loc) · 1.04 KB
/
Quick_Sort.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
#include <iostream>
using namespace std;
int Parition(int array[], int left, int right)
{
int pivot = array[left];
int index = right;
int temp;
for(int j = right; j > left; j--)
{
if(array[j] > pivot)
{
temp = array[j];
array[j] = array[index];
array[index] = temp;
index--;
}
}
array[left] = array[index];
array[index] = pivot;
return index;
}
void Quick(int array[], int left, int right)
{
if(left < right)
{
int pivot = Parition(array, left, right);
Quick(array, left, pivot - 1);
Quick(array, pivot + 1, right);
}
}
void Quick_Sort(int array[], int size)
{
Quick(array, 0, size - 1);
}
// Function to print elements of array
void Print_Array(int array[], int size)
{
for(int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
int main()
{
int array[] = {2, 4, 3, 1, 6, 8, 4};
Quick_Sort(array, 7);
Print_Array(array, 7);
return 0;
}
// Output
// 1 2 3 4 4 6 8