[AlgoExpert] Selection Sort

Selection Sort

  • Time : O(n^2)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<int> selectionSort(vector<int> array) {
int n = array.size();
for(int i = 0; i < n; i++) {
int mi = array[i], idx = i;
for(int j = i; j < n; j++) {
if(mi > array[j]) {
mi = array[j], idx = j;
}
}
swap(array[i], array[idx]);
}

return array;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/09/PS/AlgoExpert/selecting-sort/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.