CS/Data Structure
선택 정렬 (Selection Sort)
kwan-dev
2023. 10. 30. 20:13
최댓값 혹은 최솟값을 하나씩 찾아서 한쪽으로 몰아넣는 방식.
예를들어 오름차순 정렬이라고 한다면,
- 배열 전체 순회하면서 최댓값을 찾음
- 맨 오른쪽에 배치.
- 정렬한 숫자 빼고 나머지 또 다 순회해서 그 다음으로 큰 값을 찾음.
- 정렬 안된 숫자 중 가장 오른쪽 배치
- 반복.
int min_index;
for (int i = 0; i < size - 1; i++)
{
min_index = i;
for (int j = i + 1; j < size; j++)
{
if (arr[j] < arr[min_index])
{
min_index = j;
}
}
swap(arr[i], arr[min_index]);
}
Worst-case O(n^2) 성능
https://en.wikipedia.org/wiki/Selection_sort
Selection sort - Wikipedia
From Wikipedia, the free encyclopedia Sorting algorithm In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(n2) time complexity, which makes it inefficient on large lists, and generally performs worse than the simil
en.wikipedia.org