-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.QUICK_SORT.cpp
114 lines (105 loc) · 1.99 KB
/
13.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//types of recursion pg 5-89 tb
#include<iostream>
#define N 7
using namespace std;
class sorting
{
public:
float s[N];
//-------------------------------------------------------
void enter_percentage()
{
for(int i=0;i<N;i++)
{
cout<<"Enter number : ";
cin>>s[i];
}
}
//-------------------------------------------------------
int partition(float s[] , int start , int end)
{
int i;
int pi;
int pivot;
float temp;
i=start;
pi=start;
pivot=end;
for(i=start;i<=end;i++)
{
if(s[i] < s[pivot])
{
temp = s[i];
s[i]=s[pi];
s[pi]=temp;
pi++;
}
}
temp=s[pi];
s[pi]=s[pivot];
s[pivot]=temp;
return pi;
}
//-------------------------------------------------------
void quick_sort(float s[] , int start , int end)
{
int pi;
if(start<=end)//remember if
{
pi=partition(s,start,end);
quick_sort(s,start,pi-1);//direct recursion//types direct indirect tail(goto) tree
quick_sort(s,pi+1,end);
}
//display();
}
//-------------------------------------------------------
void display()
{
for(int i=0;i<N;i++)
{
cout<<"Number : "<<s[i]<<endl;
}
}
//-------------------------------------------------------
void display_top_5()
{
int cnt=0;
for(int i=N-1;cnt<5;i--)
{
cout<<"Number : "<<s[i]<<endl;
cnt++;
}
}
//-------------------------------------------------------
};
int main()
{
sorting s1;
int op=-1;
while(op!=0)
{
cout<<"\n1.ENTER PERCENTAGE ";
cout<<"\n2.QUICK SORT ";
cout<<"\n3.DISPLAY TOP 5";
cout<<"\n4.EXIT";
cout<<"\n\nEnter choice : ";
cin>>op;
switch(op)
{
case 1:
s1.enter_percentage();
break;
case 2:
s1.quick_sort(s1.s,0,N-1); //error don't pass s1.N
//pass array , starting value ,end value
break;
case 3:
s1.display();
break;
case 4:
op=0;
break;
}
}
return 0;
}