-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quick_sort.cpp
88 lines (64 loc) · 1013 Bytes
/
Quick_sort.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#include<iostream>
using namespace std;
int partition(int a[],int l,int r);
void quick_sort(int a[],int l,int r);
int main()
{
int i,a[10],n;
cout<<"Enter the no of array elements ";
cin>>n;
cout<<"Enter the array elements ";
for(i=0;i<n;i++)
cin>>a[i];
int l=0;
int r=n-1;
quick_sort(a,l,r);
cout<<"The sorted array is as follows \n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
return 0;
}
int partition(int a[],int l,int r)
{
int X,i=l,j=r,temp;
X=a[l];
while(i<j)
{
while(a[i]<=X && i<=r)
{
i++;
}
while(a[j]>X)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[l];
a[l]=a[j];
a[j]=temp;
return j;
}
void quick_sort(int a[],int l,int r)
{
int p;
if(l<r)
{
p=partition(a,l,r);
quick_sort(a,l,p-1);
quick_sort(a,p+1,r);
}
}
/* Output
Enter the no of array elements 4
Enter the array elements 55
11
33
88
The sorted array is as follows
11 33 55 88
*/