forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_sort.cpp
More file actions
51 lines (50 loc) · 888 Bytes
/
Copy pathQuick_sort.cpp
File metadata and controls
51 lines (50 loc) · 888 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
#include<bits/stdc++.h>
using namespace std;
int partition(int arr[],int l,int h)
{
int pivot=arr[h];
int i=l-1;
for(int j=l;j<h;j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[h]);
return (i+1);
}
void quick_sort(int arr[],int l,int h)
{
if(l<h)
{
int pi=partition(arr,l,h);
quick_sort(arr,l,pi-1);
quick_sort(arr,pi+1,h);
}
}
void display(int arr[],int n)
{
cout<<"Array elements in sorted order\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main()
{
int n;
cout<<"Enter the size of the array\n";
cin>>n;
int arr[n+1];
cout<<"Enter the elements of array\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quick_sort(arr,0,n);
display(arr,n);
return 0;
}