https://www.toptal.com/developers/sorting-algorithms
Sorting Algorithms Animations
Animation, code, analysis, and discussion of 8 sorting algorithms on 4 initial conditions.
www.toptal.com
public class Ex04_selection {
public static void main(String[] args) {
// Selection Sort
int []num = new int[] {25,15,10,5,12,9,17,23,13,19};
int t;
System.out.print("Source data : ");
for(int n: num) {
System.out.printf("%5d", n);
}
System.out.println();
// 정렬
for(int i=0; i<num.length-1; i++) { // i=0; i<9; i++ 9회전
for(int j=i+1; j<num.length; j++) {
if(num[i] > num[j]) { // 부등호에 따라 오름차순, 내림차순
t = num[i];
num[i] = num[j];
num[j] = t;
}
}
}
System.out.print("Sort data : ");
for(int n: num) {
System.out.printf("%5d", n);
}
System.out.println();
}
}
Selection 정렬 : 가장 앞에 가장 작은 값을 놓는다.
50 25 60 35 40
1회전 : (1번째:2번째)(1번째:3번째)(1번째:4번째)(1번째:5번째)
25 : 50 / 25 : 60 / 25: 35 / 25 : 40
결과 : 25 50 60 35 40
총 비교 횟수 n(n-1)/2
가장 쉬운 정렬 방식
'쌍용강북교육센터 > 7월' 카테고리의 다른 글
0713_Ex01_array : 배열복사 (arraycopy) (0) | 2021.07.13 |
---|---|
0712_Ex05_bubble : 버블정렬에 대해 (1) | 2021.07.13 |
0712_Ex03_Quiz : 로또 1~45까지 수중 서로 다른 6개의 수 생성 (0) | 2021.07.12 |
0712_Ex02_Quiz : 5개의 점수를 입력받아 최대점수와 최소점수를 뺀 합구하기 (0) | 2021.07.12 |
! 복습아직 안함 ! 0709_Ex17_calendar : 년, 월을 입력 받아 달력 만들기 (0) | 2021.07.12 |