-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
48 lines (48 loc) · 898 Bytes
/
QuickSort.cpp
File metadata and controls
48 lines (48 loc) · 898 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
#include<iostream>
using namespace std;
void quicksort(int [],int,int);
int partition(int [],int,int);
main()
{
int n,a[20];
cout<<"Enter no. of elements"<<endl;
cin>>n;
cout<<"Enter the elements "<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<"The sorted array is: "<<endl;
for(int k=0;k<n;k++)
cout<<a[k]<<"\t";
}
void quicksort(int ar[],int l,int u)
{
int j;
if(l<u)
{
j=partition(ar,l,u);
quicksort(ar,l,j-1);
quicksort(ar,j+1,u);
}
}
int partition(int ar[],int l,int u)
{
int temp,v,i,j;
v=ar[l];
i=l;
j=u+1;
do
{
do {i++;} while((ar[i]<v) && (i<=u));
do {j--;} while(v<ar[j]);
if(i<j)
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}while(i<j);
ar[l]=ar[j];
ar[j]=v;
return (j);
}