Skip to content

Commit 54ff2a7

Browse files
authored
Merge pull request #2 from msidrisi91/main
Added sorting algorithms
2 parents 911c934 + 0abbe07 commit 54ff2a7

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

sorting/bubble_sort.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Bubble sort algorithm in cpp
2+
#include <iostream>
3+
using namespace std;
4+
5+
int main() {
6+
int n, i, j, temp;
7+
cout << "Enter the number of elements: ";
8+
cin >> n;
9+
int a[n];
10+
cout << "Enter the elements: ";
11+
for (i = 0; i < n; i++) {
12+
cin >> a[i];
13+
}
14+
for (i = 0; i < n; i++) {
15+
for (j = 0; j < n - 1; j++) {
16+
if (a[j] > a[j + 1]) {
17+
temp = a[j];
18+
a[j] = a[j + 1];
19+
a[j + 1] = temp;
20+
}
21+
}
22+
}
23+
cout << "Sorted array: ";
24+
for (i = 0; i < n; i++) {
25+
cout << a[i] << " ";
26+
}
27+
return 0;
28+
}

sorting/insertion_sort.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Insertion Sort algorithm in cpp
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
int n, i, j, key, temp;
8+
cout << "Enter the number of elements: ";
9+
cin >> n;
10+
int a[n];
11+
cout << "Enter the elements: ";
12+
for (i = 0; i < n; i++) {
13+
cin >> a[i];
14+
}
15+
for (i = 1; i < n; i++) {
16+
key = a[i];
17+
j = i - 1;
18+
while (j >= 0 && a[j] > key) {
19+
a[j + 1] = a[j];
20+
j = j - 1;
21+
}
22+
a[j + 1] = key;
23+
}
24+
cout << "Sorted array is: ";
25+
for (i = 0; i < n; i++) {
26+
cout << a[i] << " ";
27+
}
28+
return 0;
29+
}

sorting/merge_sort.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Merge Sort Algorithm in Cpp
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
int n;
8+
cin >> n;
9+
int arr[n];
10+
for (int i = 0; i < n; i++) {
11+
cin >> arr[i];
12+
}
13+
int temp[n];
14+
int mid = n / 2;
15+
int i = 0;
16+
int j = mid;
17+
int k = 0;
18+
while (i < mid && j < n) {
19+
if (arr[i] < arr[j]) {
20+
temp[k] = arr[i];
21+
i++;
22+
} else {
23+
temp[k] = arr[j];
24+
j++;
25+
}
26+
k++;
27+
}
28+
while (i < mid) {
29+
temp[k] = arr[i];
30+
i++;
31+
k++;
32+
}
33+
while (j < n) {
34+
temp[k] = arr[j];
35+
j++;
36+
k++;
37+
}
38+
for (int i = 0; i < n; i++) {
39+
arr[i] = temp[i];
40+
}
41+
for (int i = 0; i < n; i++) {
42+
cout << arr[i] << " ";
43+
}
44+
return 0;
45+
}

sorting/selection sort.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Selection sort algorithm in cpp
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main () {
7+
int n, i, j, min, temp;
8+
cout << "Enter the number of elements: ";
9+
cin >> n;
10+
int a[n];
11+
cout << "Enter the elements: ";
12+
for (i = 0; i < n; i++) {
13+
cin >> a[i];
14+
}
15+
for (i = 0; i < n; i++) {
16+
min = i;
17+
for (j = i + 1; j < n; j++) {
18+
if (a[j] < a[min]) {
19+
min = j;
20+
}
21+
}
22+
temp = a[i];
23+
a[i] = a[min];
24+
a[min] = temp;
25+
}
26+
cout << "Sorted array: ";
27+
for (i = 0; i < n; i++) {
28+
cout << a[i] << " ";
29+
}
30+
return 0;
31+
}

0 commit comments

Comments
 (0)