forked from krimanisha/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
56 lines (53 loc) · 715 Bytes
/
QuickSort.cpp
File metadata and controls
56 lines (53 loc) · 715 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
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
int swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//Partition Code or Pivot Element Creation
int partition(int a[],int l,int h)
{
int pivot = a[l];
int i=l,j=h,k;
while(i<j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
{
swap(&a[i],&a[j]);
}
}
swap(&a[l],&a[j]);
return j;
}
int Quicksort(int a[],int l,int h)
{
int j;
if(l<h)
{
j = partition(a,l,h);
Quicksort(a,l,j);
Quicksort(a,j+1,h);
}
}
int main()
{
int i,n;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
Quicksort(a,0,n-1);
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}