-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA Assignment 14-quick sort.cpp
92 lines (85 loc) · 1.32 KB
/
DSA Assignment 14-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
#include<iostream>
using namespace std;
const int MAX=20;
class Student
{
float perc[MAX];
int n;
public:
void accept();
void display();
void displayTop5();
void quickSort(int start,int end);
int getn()
{
return n;
}
};
void Student::accept()
{
cout<<"\nEnter Number of Students: ";
cin>>n;
cout<<"\nENter percentages of "<<n<<" students: ";
for(int i=0;i<n;i++)
{
cin>>perc[i];
}
}
void Student::display()
{
cout<<"\nStudent List:\n";
for(int i=0;i<n;i++)
{
cout<<perc[i]<<" ";
}
}
void Student::displayTop5()
{
int c;
for(int i=n-1,c=0;i>=0 && c<5; i--,c++)
{
cout<<c+1<<" )"<<perc[i]<<"\n";
}
}
void Student::quickSort(int start,int end)
{
int pivot,i,j;
float temp;
if(start<end)
{
pivot=start;
i=start+1;
j=end;
while(i<j)
{
while(perc[i]<=perc[pivot] && i<j)
i++;
while(perc[j]>=perc[pivot] && i<=j)
j--;
if(i<=j)
{
temp=perc[i];
perc[i]=perc[j];
perc[j]=temp;
}
}
temp=perc[pivot];
perc[pivot]=perc[j];
perc[j]=temp;
quickSort(start,j-1);
quickSort(j+1,end);
}
}
int main()
{
Student s;
int choice,num;
s.accept();
cout<<"\nBefore Sorting: ";
s.display();
cout<<"\nAfter Sorting: ";
num=s.getn();
s.quickSort(0,num-1);
s.display();
return 0;
}