@@ -19,26 +19,26 @@ class Program{
19
19
cout<<" \t\t\t\t Below Are The Type Algorithms Available in This Program" <<endl;
20
20
cout<<" \t\t\t\t 1. Searching Algorithms" <<endl;
21
21
cout<<" \t\t\t\t 2. Sorting Algorithms" <<endl;
22
+ Sleep (1000 );
22
23
cout<<endl;
23
24
}
24
25
25
26
26
27
void searchingAlgorithm (){
27
28
cout<<" Searching Algorithms" <<endl;
28
- cout<<" 1 Linear Search" <<endl;
29
- cout<<" 2 Binary Search" <<endl;
29
+ cout<<" 1. Linear Search" <<endl;
30
+ cout<<" 2. Binary Search" <<endl;
30
31
cout<<" 0. Go Back" <<endl;
31
32
}
32
33
33
34
void sortingAlgorithm (){
34
35
cout<<" Sorting Algorithms" <<endl;
35
36
cout<<" 1. Selection Sort" <<endl;
36
37
cout<<" 2. Insertion Sort" <<endl;
37
- cout<<" 3. Nearly Sorted" <<endl;
38
- cout<<" 4. Shell Sort" <<endl;
39
- cout<<" 5. Quick Sort" <<endl;
40
- cout<<" 6. Merge Sort" <<endl;
41
- cout<<" 7. Radix Sort" <<endl;
38
+ cout<<" 3. Shell Sort" <<endl;
39
+ cout<<" 4. Quick Sort" <<endl;
40
+ cout<<" 5. Merge Sort" <<endl;
41
+ cout<<" 6. Radix Sort" <<endl;
42
42
cout<<" 0. Back" <<endl;
43
43
}
44
44
@@ -50,6 +50,15 @@ class Program{
50
50
cout<<" Element at index " <<i<<" : " ; cin>>elements[i];
51
51
}
52
52
cout<<endl;
53
+
54
+ string isSorted = " Array elements are sorted" ;
55
+ for (int i=1 ; i<numberOfElements; i++){
56
+ if (elements[i - 1 ] > elements[i]){
57
+ isSorted = " Array elements are not sorted!!!" ;
58
+ break ;
59
+ }
60
+ }
61
+ cout<<isSorted<<endl;
53
62
}
54
63
55
64
void displayInput (){
@@ -74,12 +83,16 @@ class AlgorithmsForInt : protected Program{
74
83
void linearSearch (){
75
84
cout<<" Enter key for linear search>>>>" ;
76
85
cin>>key;
77
- for (int i=0 ; i<numberOfElements; i++){
86
+ int i;
87
+ for (i=0 ; i<numberOfElements; i++){
78
88
if (elements[i]==key){
79
89
cout<<" Found " <<key<<" at index " <<i<<endl;
80
90
break ;
81
91
}
82
- }cout<<key<<" was not found" <<endl;
92
+ }
93
+ if (i == numberOfElements){
94
+ cout<<key<<" was not found" <<endl;
95
+ }
83
96
}
84
97
85
98
// Bianry search algorithm
@@ -97,6 +110,7 @@ class AlgorithmsForInt : protected Program{
97
110
else {
98
111
ret = mid;
99
112
}
113
+
100
114
}
101
115
102
116
if (ret == -1 ){
@@ -139,11 +153,7 @@ class AlgorithmsForInt : protected Program{
139
153
}
140
154
}
141
155
142
- // Nearly sorted
143
- void nearlySorted (){
144
- // codes goes here
145
-
146
- }
156
+
147
157
148
158
// Shell sort
149
159
void shellSort (){
@@ -157,7 +167,7 @@ class AlgorithmsForInt : protected Program{
157
167
}
158
168
elements[i] = temp;
159
169
}
160
- }
170
+ }
161
171
162
172
}
163
173
@@ -253,59 +263,53 @@ class AlgorithmsForInt : protected Program{
253
263
254
264
255
265
// Radix sort
256
- int getMax (int elements[], int elementSize){
257
- int max = elements[0 ];
258
- for (int i = 0 ; i < elementSize; i++){
259
- if (elements[i] > max){
260
- max = elements[i];
261
- }
262
- }
263
- return max;
266
+ // A utility function to get maximum value in arr[]
267
+ int getMax (int arr[], int n){
268
+ int mx = arr[0 ];
269
+ for (int i = 1 ; i < n; i++)
270
+ if (arr[i] > mx)
271
+ mx = arr[i];
272
+ return mx;
264
273
}
265
274
266
- void countSort (int elements[], int elementSize, int exp){
267
- int output[elementSize];
268
- int i, count[10 ] = {0 };
275
+ // A function to do counting sort of arr[] according to
276
+ // the digit represented by exp.
277
+ void countSort (int arr[], int n, int exp){
278
+ int output[n]; // output array
279
+ int i, count[10 ] = { 0 };
269
280
270
- // Store count of occurrences of count []
271
- for (i=0 ; i<elementSize; i++){
272
- count[(elements[i] / exp ) % 10 ];
273
- }
281
+ // Store count of occurrences in count[]
282
+ for (i = 0 ; i < n; i++)
283
+ count[(arr[i] / exp ) % 10 ]++;
274
284
275
285
// Change count[i] so that count[i] now contains actual
276
286
// position of this digit in output[]
277
- for (i = 0 ; i < 10 ; i++){
287
+ for (i = 1 ; i < 10 ; i++)
278
288
count[i] += count[i - 1 ];
279
- }
280
289
281
290
// Build the output array
282
- for (i = elementSize - 1 ; i >= 0 ; i--){
283
- output[count[(elements [i] / exp ) % 10 ] -1 ] = elements [i];
284
- count[(elements [i] / exp ) % 10 ]--;
291
+ for (i = n - 1 ; i >= 0 ; i--) {
292
+ output[count[(arr [i] / exp ) % 10 ] - 1 ] = arr [i];
293
+ count[(arr [i] / exp ) % 10 ]--;
285
294
}
286
295
287
- // Copy the output array into elements[], so that
288
- // elements[] now contains sorted numbers according to
289
- // current digits.
290
- for (i = 0 ; i < elementSize; i++){
291
- elements[i] = output[i];
292
- }
296
+ // Copy the output array to arr[], so that arr[] now
297
+ // contains sorted numbers according to current digit
298
+ for (i = 0 ; i < n; i++)
299
+ arr[i] = output[i];
293
300
}
294
301
295
- void radixSort (int elements[], int elementSize){
302
+ // The main function to that sorts arr[] of size n using
303
+ // Radix Sort
304
+ void radixsort (int arr[], int n){
296
305
// Find the maximum number to know number of digits
297
- int m = getMax (elements, elementSize );
306
+ int m = getMax (arr, n );
298
307
299
- // Do counting sort for every digit. Note that instead of
300
- // passing digit number, exp is passed. exp is 10^i
301
- // where i is the current digit number
302
- for (int exp = 1 ; m / exp > 0 ; exp *= 10 ){
303
- countSort (elements, elementSize, exp );
304
- }
305
-
306
- // display results to user
307
- displayResult ();
308
-
308
+ // Do counting sort for every digit. Note that instead
309
+ // of passing digit number, exp is passed. exp is 10^i
310
+ // where i is current digit number
311
+ for (int exp = 1 ; m / exp > 0 ; exp *= 10 )
312
+ countSort (arr, n, exp );
309
313
}
310
314
311
315
};
@@ -322,35 +326,37 @@ class Home: protected AlgorithmsForInt{
322
326
string proceed, exit ;
323
327
do {
324
328
cout<<" Do you want to proceed with Sorting or Searching Algorithm?" <<endl;
325
- cout<<" Enter 0 for Sorting or 1 for Searching Algorithm >>>> " ;
329
+ cout<<" Enter 1 for Searching or 2 for Sorting Algorithm >>>> " ;
326
330
cin>>option;
327
331
cout<<endl<<endl;
328
332
329
333
// checking user input validity
330
- while (option != 0 && option != 1 ){
334
+ while (option < 1 || option > 2 ){
331
335
cout<<" Please enter a value between 0 and 1: " ;
332
336
cin>>option;
333
337
}
334
338
335
339
switch (option){
336
- case 0 :
340
+ case 1 :
337
341
do {
338
342
searchingAlgorithm ();
339
343
cout<<endl;
340
344
cout<<" Select any of the Searching Algorithms above: " ;
341
345
cin>>choice;
342
346
cout<<endl;
343
347
344
- while (choice < 0 || choice > 2 ){
348
+ while (choice < 1 || choice > 2 ){
345
349
cout<<" Please enter a value between 0 and 2: " ;
346
350
cin>>choice;
347
351
}
348
352
switch (choice){
349
353
350
354
case 1 :
351
- while (proceed != " N" ){
355
+ proceed = " Y" ;
356
+ while (proceed == " Y" ){
352
357
cout<<" Linear search algorithm" <<endl;
353
358
takeInput ();
359
+
354
360
displayInput ();
355
361
linearSearch ();
356
362
@@ -379,7 +385,7 @@ class Home: protected AlgorithmsForInt{
379
385
}while (choice != 0 );
380
386
break ;
381
387
382
- case 1 :
388
+ case 2 :
383
389
do {
384
390
sortingAlgorithm ();
385
391
cout<<endl;
@@ -423,22 +429,8 @@ class Home: protected AlgorithmsForInt{
423
429
}
424
430
break ;
425
431
426
- case 3 :
427
- proceed = " Y" ;
428
- while (proceed == " Y" ){
429
- cout<<" Nearly sort algorithm" <<endl;
430
- takeInput ();
431
- displayInput ();
432
- nearlySorted ();
433
- displayResult ();
434
-
435
- cout<<" Do you want to continue with Nearly Sort? (Y or N): " ;
436
- cin>>proceed;
437
- cout<<endl;
438
- }
439
- break ;
440
432
441
- case 4 :
433
+ case 3 :
442
434
proceed = " Y" ;
443
435
while (proceed == " Y" ){
444
436
cout<<" Shell sort algorithm" <<endl;
@@ -453,7 +445,7 @@ class Home: protected AlgorithmsForInt{
453
445
}
454
446
break ;
455
447
456
- case 5 :
448
+ case 4 :
457
449
proceed = " Y" ;
458
450
while (proceed == " Y" ){
459
451
cout<<" Quick sort algorithm" <<endl;
@@ -473,7 +465,7 @@ class Home: protected AlgorithmsForInt{
473
465
}
474
466
break ;
475
467
476
- case 6 :
468
+ case 5 :
477
469
proceed = " Y" ;
478
470
while (proceed == " Y" ){
479
471
cout<<" Merge sort algorithm" <<endl;
@@ -492,13 +484,29 @@ class Home: protected AlgorithmsForInt{
492
484
cout<<endl;
493
485
}
494
486
break ;
487
+
488
+ case 6 :
489
+ proceed = " Y" ;
490
+ while (proceed == " Y" ){
491
+ cout<<" Radix sort algorithm" <<endl;
492
+ takeInput ();
493
+ displayInput ();
494
+ radixsort (elements, numberOfElements);
495
+ displayResult ();
496
+
497
+ cout<<" Do you want to continue with Radix Sort? (Y or N): " ;
498
+ cin>>proceed;
499
+ cout<<endl;
500
+ }
501
+ break ;
502
+
495
503
}
496
504
497
505
}while (choice != 0 );
498
506
}
499
507
500
508
501
- }while (option == 0 || option == 1 );
509
+ }while (option == 1 || option == 2 );
502
510
}
503
511
};
504
512
0 commit comments