JAVA/기타

삽입 정렬(Insertion Sort)

99C0RN 2015. 10. 26. 18:09



- 배열의 모든 요소를 앞에서부터 차례대로 이미 정렬된 배열 부분과 비교하여, 자신의 위치를 찾아 삽입함.

- 배열의 두 번째 데이터 부터 연산을 시작함.

- 시간복잡도 : O(n^2)



[1회전]


[2회전]


[3회전]


[4회전]


JAVA 소스코드

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
public class Insertion {
 
    public void sort(int[] A){
        int size = A.length;
        int temp = 0;
        int j = 0;
        for(int i = 1; i < size; i++){
            temp = A[i];
            for(j=i-1; j>=0 && temp<A[j]; j--){
                A[j+1= A[j];
            }
            A[j+1= temp;
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Insertion insertion = new Insertion();
        
        int A[] = {66101345};
        
        insertion.sort(A);
        for(int i = 0; i < A.length; i++){
            System.out.println("A["+i+"] : " + A[i]);
        }
    }
}
cs