---
# System prepended metadata

title: '-Selection sort'
tags: [Easy]

---

# -Selection sort
###### tags: `Easy`

從頭開始，然後在剩餘的數據中依次找最小的值，換到相應的位置。
Select: 選最小
```cpp=
vector<int> selectionSort(vector<int> array) {
  // Write your code here.
  if (array.empty()) 
      return {};
  for (int i = 0; i < array.size(); i++){
      int cur_min_idx = i;
      for (int j = i+1; j < array.size();j++){
          if (array[j] < array[cur_min_idx])
              cur_min_idx = j;
      }
      swap(array[i], array[cur_min_idx]);
  }
  return array;
}
```