코드
import java.util.*;
//오름차순 기준
//선택정렬은 앞자리부터 작은 수를 채워간다.
//즉, 1번째 순회 시 맨 앞자리에 가장 작은 수가 오고
//그 다음은 2 번째 순회 시 그 다음 자리에 그 다음으로 작은 수가 온다.
class 선택정렬 {
public static void main(String[] args){
int[] arr = new Random().ints(10, 50, 101)
.toArray();
// i의 역할은 인덱스로 순회하면서 가장 작은 수를 채운다.
for(int i=0;i<arr.length-1;i++) {
//j 역할은 i 마다 반복하면서 arr[i]보다 작은 수를 검사해 자리를 바꿔치기한다.
System.out.println("i 순회 중 = "+ i);
for(int j=i+1;j<arr.length;j++) {
if(arr[i]>arr[j]) {
System.out.println("arr["+i+"]"+arr[i]+" <==> arr["+j+"]="+arr[j]);
for(int z=0;z<j;z++)System.out.print(" ");
System.out.println(" *");
System.out.println(Arrays.toString(arr));
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
System.out.println(Arrays.toString(arr));
System.out.println("========================================");
}
}
}
System.out.println("\n"+Arrays.toString(arr));
}
}
결과
i 순회 중 = 0
arr[0]83 <==> arr[1]=66
*
[83, 66, 100, 60, 78, 90, 56, 67, 91, 76]
[66, 83, 100, 60, 78, 90, 56, 67, 91, 76]
========================================
arr[0]66 <==> arr[3]=60
*
[66, 83, 100, 60, 78, 90, 56, 67, 91, 76]
[60, 83, 100, 66, 78, 90, 56, 67, 91, 76]
========================================
arr[0]60 <==> arr[6]=56
*
[60, 83, 100, 66, 78, 90, 56, 67, 91, 76]
[56, 83, 100, 66, 78, 90, 60, 67, 91, 76]
========================================
i 순회 중 = 1
arr[1]83 <==> arr[3]=66
*
[56, 83, 100, 66, 78, 90, 60, 67, 91, 76]
[56, 66, 100, 83, 78, 90, 60, 67, 91, 76]
========================================
arr[1]66 <==> arr[6]=60
*
[56, 66, 100, 83, 78, 90, 60, 67, 91, 76]
[56, 60, 100, 83, 78, 90, 66, 67, 91, 76]
========================================
i 순회 중 = 2
arr[2]100 <==> arr[3]=83
*
[56, 60, 100, 83, 78, 90, 66, 67, 91, 76]
[56, 60, 83, 100, 78, 90, 66, 67, 91, 76]
========================================
arr[2]83 <==> arr[4]=78
*
[56, 60, 83, 100, 78, 90, 66, 67, 91, 76]
[56, 60, 78, 100, 83, 90, 66, 67, 91, 76]
========================================
arr[2]78 <==> arr[6]=66
*
[56, 60, 78, 100, 83, 90, 66, 67, 91, 76]
[56, 60, 66, 100, 83, 90, 78, 67, 91, 76]
========================================
i 순회 중 = 3
arr[3]100 <==> arr[4]=83
*
[56, 60, 66, 100, 83, 90, 78, 67, 91, 76]
[56, 60, 66, 83, 100, 90, 78, 67, 91, 76]
========================================
arr[3]83 <==> arr[6]=78
*
[56, 60, 66, 83, 100, 90, 78, 67, 91, 76]
[56, 60, 66, 78, 100, 90, 83, 67, 91, 76]
========================================
arr[3]78 <==> arr[7]=67
*
[56, 60, 66, 78, 100, 90, 83, 67, 91, 76]
[56, 60, 66, 67, 100, 90, 83, 78, 91, 76]
========================================
i 순회 중 = 4
arr[4]100 <==> arr[5]=90
*
[56, 60, 66, 67, 100, 90, 83, 78, 91, 76]
[56, 60, 66, 67, 90, 100, 83, 78, 91, 76]
========================================
arr[4]90 <==> arr[6]=83
*
[56, 60, 66, 67, 90, 100, 83, 78, 91, 76]
[56, 60, 66, 67, 83, 100, 90, 78, 91, 76]
========================================
arr[4]83 <==> arr[7]=78
*
[56, 60, 66, 67, 83, 100, 90, 78, 91, 76]
[56, 60, 66, 67, 78, 100, 90, 83, 91, 76]
========================================
arr[4]78 <==> arr[9]=76
*
[56, 60, 66, 67, 78, 100, 90, 83, 91, 76]
[56, 60, 66, 67, 76, 100, 90, 83, 91, 78]
========================================
i 순회 중 = 5
arr[5]100 <==> arr[6]=90
*
[56, 60, 66, 67, 76, 100, 90, 83, 91, 78]
[56, 60, 66, 67, 76, 90, 100, 83, 91, 78]
========================================
arr[5]90 <==> arr[7]=83
*
[56, 60, 66, 67, 76, 90, 100, 83, 91, 78]
[56, 60, 66, 67, 76, 83, 100, 90, 91, 78]
========================================
arr[5]83 <==> arr[9]=78
*
[56, 60, 66, 67, 76, 83, 100, 90, 91, 78]
[56, 60, 66, 67, 76, 78, 100, 90, 91, 83]
========================================
i 순회 중 = 6
arr[6]100 <==> arr[7]=90
*
[56, 60, 66, 67, 76, 78, 100, 90, 91, 83]
[56, 60, 66, 67, 76, 78, 90, 100, 91, 83]
========================================
arr[6]90 <==> arr[9]=83
*
[56, 60, 66, 67, 76, 78, 90, 100, 91, 83]
[56, 60, 66, 67, 76, 78, 83, 100, 91, 90]
========================================
i 순회 중 = 7
arr[7]100 <==> arr[8]=91
*
[56, 60, 66, 67, 76, 78, 83, 100, 91, 90]
[56, 60, 66, 67, 76, 78, 83, 91, 100, 90]
========================================
arr[7]91 <==> arr[9]=90
*
[56, 60, 66, 67, 76, 78, 83, 91, 100, 90]
[56, 60, 66, 67, 76, 78, 83, 90, 100, 91]
========================================
i 순회 중 = 8
arr[8]100 <==> arr[9]=91
*
[56, 60, 66, 67, 76, 78, 83, 90, 100, 91]
[56, 60, 66, 67, 76, 78, 83, 90, 91, 100]
========================================
[56, 60, 66, 67, 76, 78, 83, 90, 91, 100]