-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
47 lines (45 loc) · 853 Bytes
/
quick_sort.cpp
File metadata and controls
47 lines (45 loc) · 853 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
#include<iostream>
using namespace std;
int x,i,j;
int partition(int a[],int low, int high)
{
int i = low;
int j = high;
int pivot = a[low];
while(i<j)
{
do{i++;}while(a[i]>pivot);
do{j--;}while(a[j]<pivot);
if(i<j)
{
swap(a[i],a[j]);
}
}
swap(a[low],a[j]);
return j;
}
void quickSort(int a[],int low, int high)
{
if(low<high)
{
int j = partition(a,low,high);
quickSort(a,low,j);
quickSort(a,j+1,high);
}
}
int main()
{
int x;
cout<<"Enter the range of the array : ";
cin>>x;
int n= x+1;
int a[n];
cout<<"Enter all the elements of the array : ";
for(int i=0;i<x;i++)
cin>>a[i];
a[x]=999;
quickSort(a,0,n-1);
cout<<"Sorted array is : ";
for(int i=0;i<x;i++)
cout<<a[i]<<" ";
}