-
- 선택정렬 (Selection Sort)
- 가장 작은 원소를 찾아 앞의 데이터와 자리 교환하는 정렬방식.
- 예) [2, 3, 1, 5, 4] 배열에서 원소 "1"은 가장 작은 원소이므로 원소 "2"와 자리교환 -> [1, 3, 2, 5, 4]
출처: Naver 지식백과 - 구현
public static void main(String[] args) { int[] array = {2, 4, 3, 5, 1}; int length = array.length; for (int i = 0; i < length - 1; i++) { int minIndex = i; for (int j = i + 1; j < length; j++) { if (array[minIndex] > array[j]) minIndex = j; } int temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } }
- for문 2번에 시간복잡도는 O(N^2)
'CS > 알고리즘' 카테고리의 다른 글
[Greedy] 그리디 알고리즘(탐욕법) (0) 2021.09.13 [BFS/DFS] 너비우선탐색과 깊이우선탐색 (0) 2021.08.31 [정렬] 퀵정렬 (0) 2021.08.28 [정렬] 삽입정렬 (0) 2021.08.28 [정렬] 버블정렬 (0) 2021.08.28