@@ -7,7 +7,8 @@ class Program{
7
7
public:
8
8
int numberOfElements, key;
9
9
int elements[0 ];
10
- void main (){
10
+ int firstDecision, secondDecision;
11
+ void header (){
11
12
cout<<" \n\t\t *****************************************************************************" <<endl;
12
13
cout<<" \n\t\t\t\t\t SEARCHING AND SORTING ALGORITHMS" <<endl;
13
14
cout<<" \n\t\t *****************************************************************************" <<endl;
@@ -17,8 +18,33 @@ class Program{
17
18
void welcomeMessage (){
18
19
cout<<" The Algorithms available in this Program are:" <<endl;
19
20
cout<<" Searching Algorithms" <<endl;
20
- cout<<" 1. Linear Search" <<endl;
21
- cout<<" 2. Binary Search" <<endl<<endl; Sleep (3000 );
21
+ cout<<" Sorting Algorithms" <<endl;
22
+ cout<<endl;
23
+
24
+ cout<<" Do you want to proceed with Sorting or Searching Algorithm?" <<endl;
25
+ cout<<" Enter 0 for Sorting or 1 for Searching Algorithm......." ;
26
+ cin>>firstDecision;
27
+ cout<<endl;
28
+
29
+ while (firstDecision < 0 or firstDecision > 2 ){
30
+ if (firstDecision == 0 ){
31
+ cout<<" Searching Algorithms" <<endl;
32
+ cout<<" 1 Linear Search" <<endl;
33
+ cout<<" 2 Binary Search" <<endl<<endl; Sleep (3000 );
34
+ }
35
+ else if (firstDecision == 1 ){
36
+ cout<<" Sorting Algorithms" <<endl;
37
+ cout<<" Sorting Algorithms" <<endl;
38
+ cout<<" 1. Selection Sort" <<endl;
39
+ cout<<" 2. Insertion Sort" <<endl;
40
+ cout<<" 3. Nearly Sorted" <<endl;
41
+ cout<<" 4. Shell Sort" <<endl;
42
+ cout<<" 5. Quick Sort" <<endl;
43
+ cout<<" 6. Merge Sort" <<endl;
44
+ cout<<" 7. Radix Sort" <<endl<<endl; Sleep (10000 );
45
+ }
46
+ }
47
+
22
48
cout<<" Sorting Algorithms" <<endl;
23
49
cout<<" 1. Selection Sort" <<endl;
24
50
cout<<" 2. Insertion Sort" <<endl;
@@ -27,8 +53,6 @@ class Program{
27
53
cout<<" 5. Quick Sort" <<endl;
28
54
cout<<" 6. Merge Sort" <<endl;
29
55
cout<<" 7. Radix Sort" <<endl<<endl; Sleep (10000 );
30
- cout<<" Do you want to proceed with Sorting or Searching Algorithm?" <<endl;
31
- cout<<" Enter 0 for Sorting or 1 for Searching Algorithm." <<endl;
32
56
}
33
57
34
58
void takeInput (){
@@ -141,6 +165,7 @@ class Algorithms : public Program{
141
165
takeInput ();
142
166
cout<<" UNSORTED" ;
143
167
displayInput ();
168
+
144
169
}
145
170
146
171
// Shell sort
@@ -172,15 +197,16 @@ class Algorithms : public Program{
172
197
int partition (int elements[], int lowIndex, int highIndex){
173
198
// takeInput();
174
199
// cout<<"UNSORTED";
200
+ // int elements[];
175
201
// displayInput();
176
202
int temp;
177
203
// cout<<"Select a low index for partition ";cin>>lowIndex;
178
204
// cout<<"Select a high index for partition ";cin>>highIndex;
179
205
int midpoint = lowIndex + (highIndex - lowIndex) / 2 ;
180
- int pivot = elements[midpoint];
206
+ int pivot = elements[midpoint]; // 4 is pivot
181
207
bool done = false ;
182
208
while (!done){
183
- while (elements[lowIndex] < pivot){
209
+ while (elements[lowIndex] < pivot){ // array = [10, 2, 78, 4, 45, 32, 7, 11]
184
210
lowIndex += 1 ;
185
211
}
186
212
while (pivot < elements[highIndex]){
@@ -198,10 +224,12 @@ class Algorithms : public Program{
198
224
highIndex -= 1 ;
199
225
}
200
226
}
227
+
201
228
return highIndex;
229
+
202
230
}
203
231
204
- void quickSort (int elements[], int lowIndex, int highIndex){
232
+ void quickSort (int lowIndex, int highIndex){
205
233
if (lowIndex >= highIndex){
206
234
return ;
207
235
}
@@ -229,16 +257,22 @@ class Algorithms : public Program{
229
257
// };
230
258
231
259
int main (){
260
+
261
+ int numberOfElements;
262
+ int elements[0 ];
263
+ cout<<" Enter the number of elements in the Array to be processed >>>>>> " ;
264
+ cin>>numberOfElements;
265
+ for (int i=0 ; i<numberOfElements; i++){
266
+ cout<<" Element at index " <<i<<" " ;
267
+ cin>>elements[i];
268
+ }
232
269
Algorithms m;
233
- // m.project ();
234
- // m.welcomeMessage();
270
+ m. header ();
271
+ // m.welcomeMessage();
235
272
// m.partition();
236
- int elements[8 ] = {10 , 2 , 78 , 4 , 45 , 32 , 7 , 11 };
237
- m.quickSort (elements, 0 , 8 -1 );
238
- cout<<" \n SORTED [ " ;
239
- for (int i=0 ; i < 8 ; i++){
240
- cout<<elements[i]<<" " ;
241
- }cout<<" ]" ;
273
+ // m.binarySearch();
274
+ m.quickSort (elements,0 ,5 );
275
+
242
276
243
277
return 0 ;
244
278
};
0 commit comments