-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++ CODE FOR QUICK SORT
54 lines (52 loc) · 996 Bytes
/
C++ CODE FOR QUICK SORT
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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int partion(int input[],int si,int ei)
{
int pivot=input[si];
int count=0,i=si+1;
while(i<=ei)
{
if(input[i++]<=pivot)
count++;
}
int ip=count+si;
swap(input[ip],input[si]);
i=si;
int j=ei;
while(i<j)
{
if(input[i]<=input[ip])
i++;
else if(input[j]>input[ip])
j--;
else
swap(input[i],input[j]);
}
return ip;
}
void qs(int input[],int si, int ei)
{
if(si>=ei) return;
int pi=partion(input,si,ei);
qs(input,si,pi-1);
qs(input,pi+1,ei);
}
void quickSort(int input[], int size)
{
qs(input,0,size-1);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
quickSort(input, n);
for(int i = 0; i < n; i++) {
cout << input[i] << " ";
}
delete [] input;
}