forked from abhaygupta08/Hacktober-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingarr.cpp
122 lines (93 loc) · 2.68 KB
/
sortingarr.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
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
#include <bits/stdc++.h>
using namespace std;
void create1d(int arr[], int &n){
cout << "How many elements do you want to store in your array: ";
cin >> n;
for (int i = 0; i < n; i++){
cout << "a[" << i << "]: ";
cin >> arr[i];
}
}
void printa(int a[], int n){
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
void bubblesort(int arr[], int n){
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if ( arr[j] > arr[j+1] )
{
swap(arr[j], arr[j+1]);
}
}
}
}
void insertionsort(int arr[], int n){
for (int i = 0; i <= n-1; i++)
{
int key = arr[i];
int j = i-1;
while ( j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void selectsort(int arr[], int n)
{
for ( int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++){
if (arr[j] < arr[min_idx]){
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}
}
int main(){
char ans;
do{
int arr[100], n, op;
cout << "\t\tSORTING ARRAYS\n";
cout << "1. Create Array\n";
cout << "2. Bubble Sort\n";
cout << "3. Insertion Sort\n";
cout << "4. Selection Sort\n";
cout << "Choose your option: ";
cin >> op;
switch (op) {
case 1 : cout << "\tCreating Array\n";
create1d (arr, n);
cout << "You array is :\n";
printa (arr, n);
break;
case 2 : cout << "\tBubble Sort\n";
bubblesort (arr, n);
cout << "Your sorted array is: \n";
printa (arr, n);
break;
case 3 : cout << "\tInsertion Sort\n";
insertionsort (arr, n);
cout << "Your sorted array is: \n";
printa (arr, n);
break;
case 4 : cout << "\tSelection Sort\n";
selectsort (arr, n);
cout << "Your sorted array is: \n";
printa (arr, n);
break;
default : cout << "Wrong Option! Try Again.";
}
cout << "Do you want to continue(Y/N): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << "Thank You!";
return 0;
}