728x90
반응형

 

삽입 정렬 알고리즘

삽입 정렬(Insertion Sort) 알고리즘은 자신의 위치를 찾아 삽입함으로써 정렬을 완성하는 알고리즘입니다.

  • Index가 2번째부터 시작합니다.
  • (오름차순) Index의 값보다 크면 한 칸씩 뒤에 저장합니다.
  • 위치를 찾으면 그 위치로 삽입합니다.
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
32
33
public class Insertion_Sort {
    public static void main(String[] args) {
        
        int[] arr = { 1050809070 };
 
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            for (int j = i; j > 0; j--) {
                
                if (arr[j - 1> temp) {
                    arr[j] = arr[j - 1];
                    
                    if (j == 1) {
                        arr[j] = temp;
                        break;
                    }
                    
                } else {
                    arr[j] = temp;
                    break;
                }
            }
        }
        output(arr);
    }
 
    private static void output(int[] arr) {
        
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
728x90
반응형

+ Recent posts