forked from suyashXD/hacktoberfest2k20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
53 lines (50 loc) · 1.03 KB
/
QuickSort.cpp
File metadata and controls
53 lines (50 loc) · 1.03 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
#include<bits/stdc++.h>
using namespace std;
int partition(int *a,int start,int end)
{
int pivot=a[end];
int P_index=start;
int i,t;
for(i=start;i<end;i++)
{
if(a[i]<=pivot)
{
t=a[i];
a[i]=a[P_index];
a[P_index]=t;
P_index++;
}
}
t=a[end];
a[end]=a[P_index];
a[P_index]=t;
return P_index;
}
void Quicksort(int *a,int start,int end)
{
if(start<end)
{
int P_index=partition(a,start,end);
Quicksort(a,start,P_index-1);
Quicksort(a,P_index+1,end);
}
}
int main()
{
int n;
cout<<"Enter number of elements: ";
cin>>n;
int a[n];
cout<<"Enter the array elements:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
Quicksort(a,0,n-1);
cout<<"After Quick Sort the array is:\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}