import java.util.Arrays;
public class SelectionSort {
public static void selectionSort(int[] arr){
int count=0;
int minIndex;
System.out.println("Before SelectionSort");
Arrays.stream(arr).forEach(value -> System.out.print(value + " "));
System.out.println();
for(int i=0; i<arr.length-1; i++){
minIndex = i;
for(int j=i+1; j<arr.length;j++){
count++;
if(arr[j] < arr[minIndex]){
minIndex = j;
}
}
SortUtil.swap(arr, minIndex, i);
}
System.out.println("Before SelectionSort");
System.out.println("The number of count : " + count);
Arrays.stream(arr).forEach(value -> System.out.print(value + " "));
System.out.println();
}
}
Before SelectionSort
58 9 66 95 11 69 49 104 101 102
After SelectionSort
The number of count : 45
9 11 49 58 66 69 95 101 102 104
장점
구현이 쉽다.
제자리 정렬(추가 저장공간이 필요없음) 알고리즘
단점
데이터의 양에 유연하지 않다(비교회수 정해짐)
알고리즘
1. 목록에서 최소값을 찾는다.
2. 찾은 최소값을 맨 앞의 갚(혹은 현재 위치의 값)과 교체한다.
3. 배열의 전체 요소가 정렬될 때가지 이 과정을 반복한다.