-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathSelection Sort.cpp
More file actions
42 lines (35 loc) · 775 Bytes
/
Selection Sort.cpp
File metadata and controls
42 lines (35 loc) · 775 Bytes
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
#include < iostream >
using namespace std;
void selectionSort(int arr[]) {
for (int i = 0; i < 4; i++) {
int min = i;
for (int j = i + 1; j < 5; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
int main() {
int myarr[5];
cout << "Enter 5 integers in random order: " << endl;
for (int i = 0; i < 5; i++) {
cin >> myarr[i];
}
cout << "UNSORTED ARRAY: " << endl;
for (int i = 0; i < 5; i++) {
cout << myarr[i] << " ";
}
cout << endl;
selectionSort(myarr); // sorting actually happening
cout << "SORTED ARRAY: " << endl;
for (int i = 0; i < 5; i++) {
cout << myarr[i] << " ";
}
return 0;
}