forked from rohitthapliyal2000/Competitive-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort_LomutoPartitionScheme.cpp
More file actions
51 lines (47 loc) · 1000 Bytes
/
QuickSort_LomutoPartitionScheme.cpp
File metadata and controls
51 lines (47 loc) · 1000 Bytes
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void swap(int &a , int &b)
{
int temp = a;
a = b;
b = temp;
}
int quickSort(int arr[], int n, int low, int high)
{
int pivot = arr[high];
int i = low - 1 , j;
for(j = low; j < high; j++)
{
if(arr[j] <= pivot)
{
i = i + 1;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[j]);
return i+1;
}
void partition(int arr[], int n, int low, int high)
{
if(low < high)
{
int pivot = quickSort(arr,n,low,high);
partition(arr,n,low,pivot - 1);
partition(arr,n,pivot + 1, high);
}
}
int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
partition(arr,n,0,n-1);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}