forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.cpp
40 lines (34 loc) · 826 Bytes
/
SelectionSort.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
#include <iostream>
#include <vector>
using namespace std;
void selectionSort(vector<int> &vector) {
for (uint32_t index = 0; index < vector.size() - 1; index++) {
uint32_t min = index;
for (uint32_t tempIndex = index + 1; tempIndex < vector.size();
++tempIndex) {
if (vector[tempIndex] < vector[min]) {
min = tempIndex;
}
}
if (min != index) {
int temp = vector[index];
vector[index] = vector[min];
vector[min] = temp;
}
}
}
void showVector(vector<int> vector) {
for (uint32_t i = 0; i < vector.size(); ++i) {
cout << vector[i] << ", ";
}
cout << "\n";
}
int main() {
vector<int> vector;
for (uint32_t i = 0; i < 10; ++i) {
vector.push_back(rand() % 100);
}
showVector(vector);
selectionSort(vector);
showVector(vector);
}