본문 바로가기

정올문제소스코드

1158 : 삽입정렬

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
         
        int n, i, j;
        int arr[] = new int[101];
         
        n = scan.nextInt();
         
        for (i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
         
        for (i = 1; i < n; i++) {
            for (j = i; j > 0; j--) {
                if (arr[j] < arr[j - 1])
                {
                    int c = arr[j];
                    arr[j]=arr[j-1];
                    arr[j-1]=c;
                }
                else break;
            }
            for (j = 0; j < n; j++) {
                System.out.printf("%d ", arr[j]);
            }
            System.out.printf("\n");
        } 
    }
}

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=438&sca=2070

'정올문제소스코드' 카테고리의 다른 글

1814 : 삽입정렬 횟수 세기  (0) 2021.12.05
1157 : 버블정렬  (0) 2021.12.05
1146 : 선택정렬  (0) 2021.12.05
1697 : 큐(queue)  (0) 2021.10.31
1102 : 스택 (stack)  (0) 2021.10.31