-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha12.cpp
More file actions
168 lines (165 loc) · 3.68 KB
/
a12.cpp
File metadata and controls
168 lines (165 loc) · 3.68 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//============================================================================
// Name : Assignment12.cpp
// Author : near
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<bits/stdc++.h>
using namespace std;
#define MAX 50
class Database{
float array[MAX];
int n;
public:
Database();
void setData();
void display();
void selectionSort();
void bubbleSort();
void insertionSort();
void shellSort();
void topFive();
void menu();
};
Database::Database(){
for(int i=0;i<MAX;i++)
array[i]=0;
n=0;
}
void Database::selectionSort(){
float min,temp;
int loc;
for(int i=0;i<n-1;i++){
min=array[i];
loc=i;
for(int j=i+1;j<n;j++){
if(min>array[j]){
min=array[j];
loc=j;
}
}
temp=array[i];
array[i]=array[loc];
array[loc]=temp;
}
}
void Database::bubbleSort(){
float temp;
for(int i=1;i<n;++i){
for(int j=0;j<(n-i);++j){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
void Database::insertionSort(){
float temp;
int j;
for(int i=1;i<=n-1;i++)
{
temp=array[i];
j=i-1;
while((temp<array[j])&&(j>=0))
{
array[j+1]=array[j]; //moves element forward
j=j-1;
}
array[j+1]=temp; //insert element in proper place
}
}
void Database::shellSort(){
int gap,i,j;
float temp;
for(gap=n/2;gap>0;gap/=2){
if(gap%2==0) //optimization technique
gap++;
for(i=gap;i<n;i+=1){
temp=array[i];
for(j=i;j>=gap&&array[j-gap]>temp;j-=gap){
array[j]=array[j-gap];
}
array[j]=temp;
}
}
}
void Database::topFive(){
shellSort();
int j=n-1,count=0;
cout<<"\nTop Five Scores: ";
while(j>=0&&count<5){
cout<<array[j]<<" ";
count++;
while(array[j]==array[j-1])
j--;
j--;
}
}
void Database::setData(){
cout<<"\nEnter No. of Students: ";
cin>>n;
for(int i=0;i<n;i++){
label:
cout<<"\nEnter Marks for Roll No. "<<i+1<<": ";
cin>>array[i];
if(array[i]<0||array[i]>100){
cout<<"\nError: Invalid Value for Percentage!\nTry Again!\n";
goto label;
}
}
}
void Database::display(){
cout<<"\nArray of Marks: \n";
for(int i=0;i<n;i++)
cout<<array[i]<<"\n";
}
void Database::menu(){
cout<<"\n-----------------DEQUE OPERATIONS-------------------";
cout<<"\nPress 1: Enter Score Array";
cout<<"\nPress 2: Sort Scores by Bubble Sort";
cout<<"\nPress 3: Sort Scores by Selection Sort";
cout<<"\nPress 4: Sort Scores by Insertion Sort";
cout<<"\nPress 5: Sort Scores by Shell Sort";
cout<<"\nPress 6: Display Top Five Scores";
cout<<"\nPress 7: Display Score Array";
cout<<"\nPress 9: Display Operations";
}
int main(){
Database result;
int choice,key;
result.menu();
do{
cout<<endl<<"____________________________________________________";
cout<<endl<<"Enter Choice [Press 0:exit|9:Options]: ";
cin>>choice;
switch(choice){
case 1: result.setData();
break;
case 2: cout<<"\nSorted by Bubble Sort!";
result.bubbleSort();
break;
case 3: cout<<"\nSorted by Selection Sort!";
result.selectionSort();
break;
case 4: cout<<"\nSorted by Insertion Sort!";
result.insertionSort();
break;
case 5: cout<<"\nSorted by Shell Sort!";
result.shellSort();
break;
case 6: result.topFive();
break;
case 7: result.display();
break;
case 9: result.menu();
break;
case 0: cout<<"\n----------------------END---------------------------\n";
break;
default: cout<<"\nInvalid Choice Entered!";
}
}while(choice);
return 0;
}