-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
46 lines (33 loc) · 955 Bytes
/
Copy pathselectionSort.cpp
File metadata and controls
46 lines (33 loc) · 955 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
#include <bits/stdc++.h>
using namespace std;
void selectionSort(vector <int> &number){
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < number.size()-1; i++){ //O(n)
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < number.size(); j++){ //O(n)
if (number[j] < number[min_idx])
min_idx = j;
}
// Swap the found minimum element with the first element using STL lib
swap(number[min_idx], number[i]);
}
}
int main(void){
vector <int> number;
int n;
cin >> n;
for(int i=0; i<n; i++){
int value;
cin >> value;
number.push_back(value);
}
//call selectionsort function
selectionSort(number);
cout<<"Sorted array: \n";
for(auto &v: number){
cout << v << " ";
}
return 0;
}