-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
95 lines (77 loc) · 1.41 KB
/
quick_sort.cpp
File metadata and controls
95 lines (77 loc) · 1.41 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include "Solution.h"
using namespace std;
int partition(int input[],int si,int ei)
{
int c=0;
for(int i=si+1;i<=ei;i++)
{
if(input[si]>=input[i])
{
c++;
}
}
int temp=input[si+c];
input[si+c]=input[si];
input[si]=temp;
int x=si+c;
int i=si;
int j=ei;
// std::cout <<x;
while(i<x && j>x)
{
if(input[i]<=input[x])
{
i++;
}
else if(input[j]>input[x])
{
j--;
}
else
{
int temp=input[j];
input[j]=input[i];
input[i]=temp;
j--;
i++;
}
}
return x;
}
void sort(int input[],int si,int ei)
{
if(si>=ei)
{
return;
}
else
{
int c=partition(input,si,ei);
sort(input,si,c-1);
sort(input,c+1,ei);
}
}
void quickSort(int input[], int size) {
/* Don't write main().
Don't read input, it is passed as function argument.
Change in the given array itself.
Taking input and printing output is handled automatically.
*/
int si=0;
int ei=size-1;
sort(input,si,ei);
}
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;
}