-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
80 lines (67 loc) · 1.18 KB
/
quicksort.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
//This program contains qno 17:-
#include<iostream>
using namespace std;
int partition(int [],int,int);
void Sort(int [],int,int);
void Sort(int input[],int start,int end)
{
if(start>=end)
return;
int ans=partition(input,start,end);
Sort(input,start,ans-1);
Sort(input,ans+1,end);
}
int partition(int input[],int start,int end)
{
int count=0;
for(int i=start;i<=end;i++)
{
if(input[i]<input[start])
count++;
}
int p=start+count;
int temp=input[p];
input[p]=input[start];
input[start]=temp;
int i=start,j=end;
while(i<p&&j>p)
{
if(input[i]<input[p])
i++;
else if(input[j]>input[p])
j--;
else
{
int temp=input[i];
input[i]=input[j];
input[j]=temp;
}
}
return p;
}
void sort(int input[],int n)
{
if(n<=1)
return;
Sort(input,0,n-1);
}
int main()
{
int n;
cout<<"enter size of array"<<endl;
cin>>n;
int *input=new int[n];
cout<<"enter elements of array"<<endl;
for(int i=0;i<n;i++)
{
cin>>input[i];
}
sort(input,n);
cout<<endl;
cout<<"elements in sorted order are"<<endl;
for(int i=0;i<n;i++)
{
cout<<input[i]<<" ";
}
delete [] input;
}