-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5. Quicksort.cpp
76 lines (75 loc) · 1.69 KB
/
5. 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
#include<iostream>
#include<stdlib.h>
using namespace std;
template<typename t>
int partition(t a[],int l,int h)
{
int i,j;
t pivot=a[l];
i=l;
j=h;
while(i<j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
{
t temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
t v=a[l];
a[l]=a[j];
a[j]=v;
return j;
}
template<typename q>
void qsort(q a[],int l,int h)
{
if(l<h)
{
int j=partition(a,l,h);
qsort(a,0,j-1);
qsort(a,j+1,h);
}
}
int main()
{
int a[100],i,ch,n;
double b[100];
while(1)
{
cout<<"\nQuicksort:-\n1.Integers\n2.Double\n3.Exit\n";
cout<<"Enter Your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the no.of terms:";
cin>>n;
cout<<"Enter the elements:";
for(i=0;i<n;i++)
{ cin>>a[i];}
qsort(a,0,n-1);
cout<<"Sorted array:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
break;
case 2: cout<<"Enter the no.of terms:";
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
{ cin>>b[i];}
qsort(b,0,n-1);
cout<<"Sorted array:";
for(i=0;i<n;i++)
cout<<b[i]<<" ";
break;
case 3:exit(0);
default:cout<<"Inavlid Choice!!";
}
}
return 0;
}