-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_technique.cpp
More file actions
222 lines (214 loc) · 5.16 KB
/
sorting_technique.cpp
File metadata and controls
222 lines (214 loc) · 5.16 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
using namespace std;
#define N 10
class Sort
{
public:
void selection_sort(int a[], int len);
void bubble_sort(int a[], int len);
void insertion_sort(int a[], int len);
void merge_sort(int a[], int l, int r);
void quick_sort(int a[], int low, int high);
void display(int a[], int len);
};
//Selection Sort
void Sort::selection_sort(int a[], int len)
{
for(int i = 0; i < len; i++)
{
int temp = a[i];
for(int j = i; j < len; j++)
{
if(temp > a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
display(a, len);
}
//Bubble Sort
void Sort::bubble_sort(int a[], int len)
{
int temp;
for(int i = 0; i < len; i++)
{
for(int j = 0; j <= len; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
display(a, len);
}
//Insertion Sort
void Sort::insertion_sort(int a[], int len)
{
for(int i = 1; i < len; i++)
{
int key = a[i];
int j = i-1;
while(j >= 0 && a[j] > key)
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
display(a, len);
}
void merge(int arr[], int l, int mid, int r) {
int n1 = mid - l + 1; // Size of the left subarray
int n2 = r - mid; // Size of the right subarray
int left[n1], right[n2]; // Temporary arrays
// Copy data to temp arrays left[] and right[]
for (int i = 0; i < n1; i++)
left[i] = arr[l + i];
for (int i = 0; i < n2; i++)
right[i] = arr[mid + 1 + i];
// Merge the temp arrays back into arr[l..r]
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
// Copy the remaining elements of left[], if any
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
// Copy the remaining elements of right[], if any
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
//Merge Sort
void Sort::merge_sort(int arr[], int l, int r) {
if (l < r) {
int mid = l + (r - l) / 2; // Find the middle point
// Sort first and second halves
merge_sort(arr, l, mid);
merge_sort(arr, mid + 1, r);
// Merge the sorted halves
merge(arr, l, mid, r);
}
}
int partition(int arr[], int low, int high)//function to divide the array
{
int pivot = arr[low];
int i = low;
int j = high;
while(i < j)
{
while(arr[i] <= pivot)
{
i++;
}
while(arr[j] > pivot)
{
j--;
}
if(i < j)
swap(arr[i], arr[j]);//swap if i < j
}
swap(arr[low], arr[j]);//swap if i > j
return j;//return integer
}
//Quick Sort
void Sort::quick_sort(int arr[], int low, int high)//where sorting take place
{
if(low < high)
{
int loc = partition(arr, low, high);
quick_sort(arr, low, loc-1);
quick_sort(arr, loc+1, high);
}
}
void swap(int &a, int &b)//where swap happen
{
int temp = a;
a = b;
b = temp;
}
void Sort::display(int a[], int len)
{
cout << "After Sorting " << endl;
for(int i = 0; i < len; i++)
{
cout << a[i] << "\t";
}
cout << endl;
}
int main()
{
Sort n;
int len;
cout << "Enter length of the array :";
cin >> len;
int a[len];
cout << "Enter " << len << " elements of the array " << endl;
for(int i = 0; i < len; i++)
{
cin >> a[i];
}
cout << "Unsorted Elements are " << endl;
for(int i = 0; i < len; i++)
{
cout << a[i] << "\t";
}
cout << endl;
int choice;
do
{
cout << "1) Insertion Sort " << endl;
cout << "2) Selection Sort " << endl;
cout << "3) Bubble Sort " << endl;
cout << "4) Merge Sort " << endl;
cout << "5) Quick Sort " << endl;
cout << "6) Exit " << endl;
cout << "Enter your choice :";
cin >> choice;
switch(choice)
{
case 1:
n.insertion_sort(a, len);
break;
case 2:
n.selection_sort(a, len);
break;
case 3:
n.bubble_sort(a, len);
break;
case 4:
n.merge_sort(a, 0, len-1);
n.display(a, len);
break;
case 5:
n.quick_sort(a, 0, len-1);
n.display(a, len);
break;
case 6:
cout << "Exiting......!" << endl;
exit(0);
break;
default:
cout << "Invalid input " << endl;
}
}while(choice!=6);
return 0;
}