-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
48 lines (28 loc) · 748 Bytes
/
selectionSort.cpp
File metadata and controls
48 lines (28 loc) · 748 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
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
void selectionSort(vector<int> &elements){
int unsortedPos;
int temp;
for(int i =0; i<elements.size()-1; i++){
unsortedPos = i;
temp = i;
for(int j = i+1 ; j< elements.size(); j++){
temp = min(elements[j], elements[temp]) == elements[j] ? j : temp;
}
swap(elements[unsortedPos], elements[temp]);
}
}
int main(){
vector<int> elements;
srand (time(NULL));
for(int i =0; i< 10; i++){
elements.push_back(rand()%41+1);
}
selectionSort(elements);
for (int j = 0; j < elements.size(); ++j) {
cout << elements[j] << endl;
}
}