Notice
Recent Posts
Recent Comments
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
10-11 00:15
Archives
Today
Total
관리 메뉴

Developer_Neo

[자바] 정렬 알고리즘 - 선택정렬 본문

프로그래밍/알고리즘 with Java

[자바] 정렬 알고리즘 - 선택정렬

_Neo_ 2021. 9. 4. 11:41
반응형

선택 정렬(Selection Sort)

- 가장 작은 것을 선택해 제일 앞으로 보내는 것.

선택정렬 사진

public class Selection_Sort {

public static void selection_sort(int[] a) {
selection_sort(a, a.length);
}

private static void selection_sort(int[] a, int size) {

for(int i = 0; i < size - 1; i++) {
int min_index = i;

// 최솟값을 갖고있는 인덱스 찾기 
for(int j = i + 1; j < size; j++) {
if(a[j] < a[min_index]) {
min_index = j;
}
}

// i번째 값과 찾은 최솟값을 서로 교환 
swap(a, min_index, i);
}
}

private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

위는 자바 코드이다

 

시간복잡도

내가 정리한 것

 

반응형
Comments