-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
203 lines (180 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
203 lines (180 loc) · 4.73 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
#include <iostream>
using namespace std;
int arr_size;
// display array content
void display(int arr[]) {
for (int i = 0; i < arr_size; i++)
cout << (i == 0 ? "" : " ") << arr[i];
cout << endl;
}
void BubbleSort(int arr[], int len) {
int i, tmp;
bool swap;
// to be able to access the next element of the array, (the last element)
len--;
// in bubble sort we have to loop at least once on a sorted array,
// so using do while is recomended for this case
cout << "Bubble Sort steps: " << endl;
do {
swap = false;
for (i = 0; i < len; i++) {
if (arr[i] > arr[i + 1]) {
swap = true;
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
display(arr);
} while (swap);
}
void SelectionSort(int arr[], int len) {
int i, j, tmp,
// used to hold the minimum value index, to perform one swap at a time
index;
cout << "Selection Sort steps: " << endl;
for (i = 0; i < len; i++) {
index = i;
for (j = i + 1; j < len; j++) {
if (arr[index] > arr[j])
index = j;
}
// perform the swap only once if there exists a value less than the current value
if (index != i) {
tmp = arr[i];
arr[i] = arr[index];
arr[index] = tmp;
}
display(arr);
}
}
void InsertionSort(int arr[], int len) {
int i, j, tmp;
cout << "Insertion Sort steps: " << endl;
// start with i = 1 to be able to access the previous element (at position 0)
// this implementation shifts all elements to the right to provide an empty cell at the position
// needed to perform the swap, and in the outer loop it perform the swap,
// it seems to be faster, as it perform only one assign operation in the inner loop instead of the
// traditional swap (3 assign operation) as noted in
// https://en.wikipedia.org/wiki/Insertion_sort
for (i = 1; i < len; i++) {
tmp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > tmp; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = tmp;
display(arr);
}
}
// helper function to be used with merge sort to merge two halfs of the array
void _msMerge(int arr[], int l, int m, int r) {
int i, j, k,
*L, *R,
lLen = m - l + 1,
rLen = r - m;
// create Left and Right temporary arrays
L = new int[lLen];
R = new int[rLen];
// copy the data from the original array to the new arrays
for (i = 0; i < lLen; i++)
L[i] = arr[l + i];
// here we add one as the middle position is part of the left array
for (j = 0; j < rLen; j++)
R[j] = arr[m + j + 1];
// set the control variables
i = 0;
j = 0;
k = l; // the l variable will refere to 0 in the original array (in first call)
// first we will loop on both L & R
while (i < lLen && j < rLen) {
if (L[i] < R[j])
arr[k++] = L[i++];
else {
arr[k++] = R[j++];
// split_inversion += lLen - i;
}
}
// check if there is a remaining elements in the left array
while (i < lLen)
arr[k++] = L[i++];
// check remaining elements in the right array
while (j < rLen)
arr[k++] = R[j++];
// clean up the reserved temporary arrays
delete[] L;
delete[] R;
}
// helper function to divide the array in merge sort
void _msDivide(int arr[], int l, int r) {
if (l < r) {
// calculate the middle position
int m = (l + r) >> 1;
// divide the left half recursively
_msDivide(arr, l, m);
// divide the right half recursively
_msDivide(arr, m + 1, r);
// merge the two halfs
_msMerge(arr, l, m, r);
display(arr);
}
}
// this method is the main method to use merge sort
// it make the call of merge sort more convenient to the user
// as the user will just pass the array and its length
void MergeSort(int arr[], int len) {
cout << "Merge Sort steps: " << endl;
// passing the first position as (l) and the last position as (r)
_msDivide(arr, 0, len - 1);
}
int main() {
int fn = 0, *arr;
do {
system("cls");
// get the array size and values from the user
cout << "Please enter the size of the array: ";
cin >> arr_size;
arr = new int[arr_size];
system("cls");
cout << "Please enter the array values:" << endl;
for (int i = 0; i < arr_size; i++)
cin >> arr[i];
system("cls");
cout << "Unsorted array" << endl;
display(arr);
cout << "\nPlease select the sorting algorithm:\n"
<< "\t1. Bubble Sort\n"
<< "\t2. Selection Sort\n"
<< "\t3. Insertion Sort\n"
<< "\t4. Merge Sort\n\n"
<< "any thing else to Quit ";
cin >> fn;
switch (fn) {
case 1:
BubbleSort(arr, arr_size);
break;
case 2:
SelectionSort(arr, arr_size);
break;
case 3:
InsertionSort(arr, arr_size);
break;
case 4:
MergeSort(arr, arr_size);
break;
default:
fn = 0;
break;
}
if (fn) {
cout << endl << "Sorted array" << endl;
display(arr);
fn = 0;
cout << endl << endl << "Enter 1 to try another array: ";
cin >> fn;
}
delete[] arr;
} while (fn);
cout << endl;
system("pause");
return 0;
}