-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuicksort In-Place.cpp
More file actions
49 lines (46 loc) · 982 Bytes
/
Quicksort In-Place.cpp
File metadata and controls
49 lines (46 loc) · 982 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
// https://www.hackerrank.com/challenges/quicksort3
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> arr;
void partition(int start, int end)
{
if(start+1>end) return;
int istart = start;
for(int k = start; k<end; k++)
{
if(arr[k]<arr[end])
{
int temp = arr[k];
arr[k] = arr[istart];
arr[istart] = temp;
istart++;
}
}
int temp = arr[end];
arr[end] = arr[istart];
arr[istart] = temp;
for(int i = 0; i<arr.size(); i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
partition(start, istart-1);
partition(istart+1, end);
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
for(int i = 0; i<n; i++)
{
int t;
cin>>t;
arr.push_back(t);
}
partition(0, n-1);
return 0;
}