Created
August 10, 2022 14:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void selection_sort(vector<int>& arr) { | |
int n = (int) arr.size(); | |
for (int i = 0; i < n; ++i) { | |
// 최솟값과 최솟값의 위치를 저장 | |
int min = arr[i], min_index = i; | |
for (int j = i + 1; j < n; ++j) { | |
// 더 작은 값이 발견되면 최솟값과 그 위치를 업데이트 | |
if (arr[j] < min) { | |
min = arr[j]; | |
min_index = j; | |
} | |
} | |
// 정렬되지 않은 영역의 맨 앞 원소와 최솟값의 자리를 바꿈 | |
swap(arr[i], arr[min_index]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment