Skip to content

Commit ee1d6b1

Browse files
committed
changes to main files
1 parent 938e3a4 commit ee1d6b1

File tree

8 files changed

+85
-422
lines changed

8 files changed

+85
-422
lines changed

.gitignore

Whitespace-only changes.

GroupAssignment.zip

-22 Bytes
Binary file not shown.

README.md

Whitespace-only changes.

index/index.cpp renamed to index.cpp

+85-77
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ class Program{
1919
cout<<"\t\t\t\tBelow Are The Type Algorithms Available in This Program"<<endl;
2020
cout<<"\t\t\t\t1. Searching Algorithms"<<endl;
2121
cout<<"\t\t\t\t2. Sorting Algorithms"<<endl;
22+
Sleep(1000);
2223
cout<<endl;
2324
}
2425

2526

2627
void searchingAlgorithm(){
2728
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;
3031
cout<<"0. Go Back"<<endl;
3132
}
3233

3334
void sortingAlgorithm(){
3435
cout<<"Sorting Algorithms"<<endl;
3536
cout<<"1. Selection Sort"<<endl;
3637
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;
4242
cout<<"0. Back"<<endl;
4343
}
4444

@@ -50,6 +50,15 @@ class Program{
5050
cout<<"Element at index "<<i<<": "; cin>>elements[i];
5151
}
5252
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;
5362
}
5463

5564
void displayInput(){
@@ -74,12 +83,16 @@ class AlgorithmsForInt : protected Program{
7483
void linearSearch(){
7584
cout<<"Enter key for linear search>>>>";
7685
cin>>key;
77-
for(int i=0; i<numberOfElements; i++){
86+
int i;
87+
for(i=0; i<numberOfElements; i++){
7888
if(elements[i]==key){
7989
cout<<"Found "<<key<<" at index "<<i<<endl;
8090
break;
8191
}
82-
}cout<<key<<" was not found"<<endl;
92+
}
93+
if(i == numberOfElements){
94+
cout<<key<<" was not found"<<endl;
95+
}
8396
}
8497

8598
// Bianry search algorithm
@@ -97,6 +110,7 @@ class AlgorithmsForInt : protected Program{
97110
else{
98111
ret = mid;
99112
}
113+
100114
}
101115

102116
if(ret == -1){
@@ -139,11 +153,7 @@ class AlgorithmsForInt : protected Program{
139153
}
140154
}
141155

142-
//Nearly sorted
143-
void nearlySorted(){
144-
// codes goes here
145-
146-
}
156+
147157

148158
//Shell sort
149159
void shellSort(){
@@ -157,7 +167,7 @@ class AlgorithmsForInt : protected Program{
157167
}
158168
elements[i] = temp;
159169
}
160-
}
170+
}
161171

162172
}
163173

@@ -253,59 +263,53 @@ class AlgorithmsForInt : protected Program{
253263

254264

255265
// 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;
264273
}
265274

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 };
269280

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]++;
274284

275285
// Change count[i] so that count[i] now contains actual
276286
// position of this digit in output[]
277-
for(i = 0; i < 10; i++){
287+
for (i = 1; i < 10; i++)
278288
count[i] += count[i - 1];
279-
}
280289

281290
// 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]--;
285294
}
286295

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];
293300
}
294301

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){
296305
// Find the maximum number to know number of digits
297-
int m = getMax(elements, elementSize);
306+
int m = getMax(arr, n);
298307

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);
309313
}
310314

311315
};
@@ -322,35 +326,37 @@ class Home: protected AlgorithmsForInt{
322326
string proceed, exit;
323327
do{
324328
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 >>>> ";
326330
cin>>option;
327331
cout<<endl<<endl;
328332

329333
// checking user input validity
330-
while(option != 0 && option != 1){
334+
while(option < 1 || option > 2){
331335
cout<<"Please enter a value between 0 and 1: ";
332336
cin>>option;
333337
}
334338

335339
switch(option){
336-
case 0:
340+
case 1:
337341
do{
338342
searchingAlgorithm();
339343
cout<<endl;
340344
cout<<"Select any of the Searching Algorithms above: ";
341345
cin>>choice;
342346
cout<<endl;
343347

344-
while(choice < 0 || choice > 2){
348+
while(choice < 1 || choice > 2){
345349
cout<<"Please enter a value between 0 and 2: ";
346350
cin>>choice;
347351
}
348352
switch(choice){
349353

350354
case 1:
351-
while(proceed != "N"){
355+
proceed = "Y";
356+
while(proceed == "Y"){
352357
cout<<"Linear search algorithm"<<endl;
353358
takeInput();
359+
354360
displayInput();
355361
linearSearch();
356362

@@ -379,7 +385,7 @@ class Home: protected AlgorithmsForInt{
379385
}while(choice != 0);
380386
break;
381387

382-
case 1:
388+
case 2:
383389
do{
384390
sortingAlgorithm();
385391
cout<<endl;
@@ -423,22 +429,8 @@ class Home: protected AlgorithmsForInt{
423429
}
424430
break;
425431

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;
440432

441-
case 4:
433+
case 3:
442434
proceed = "Y";
443435
while(proceed == "Y"){
444436
cout<<"Shell sort algorithm"<<endl;
@@ -453,7 +445,7 @@ class Home: protected AlgorithmsForInt{
453445
}
454446
break;
455447

456-
case 5:
448+
case 4:
457449
proceed = "Y";
458450
while(proceed == "Y"){
459451
cout<<"Quick sort algorithm"<<endl;
@@ -473,7 +465,7 @@ class Home: protected AlgorithmsForInt{
473465
}
474466
break;
475467

476-
case 6:
468+
case 5:
477469
proceed = "Y";
478470
while(proceed == "Y"){
479471
cout<<"Merge sort algorithm"<<endl;
@@ -492,13 +484,29 @@ class Home: protected AlgorithmsForInt{
492484
cout<<endl;
493485
}
494486
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+
495503
}
496504

497505
}while(choice != 0);
498506
}
499507

500508

501-
}while(option == 0 || option == 1);
509+
}while(option == 1 || option == 2);
502510
}
503511
};
504512

index/demo.cpp

-69
This file was deleted.

index/demo.exe

-2.83 MB
Binary file not shown.

index/index.exe

-2.85 MB
Binary file not shown.

0 commit comments

Comments
 (0)