백준_2750_수 정렬하기 (삽입 정렬)

임정민·2023년 1월 28일
3

알고리즘 문제풀이

목록 보기
28/173
post-thumbnail

코딩테스트 연습 스터디 진행중 입니다. ✍✍✍
Notion : https://www.notion.so/1c911ca6572e4513bd8ed091aa508d67

문제

https://www.acmicpc.net/problem/2750

[나의 풀이]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    // 삽입 정렬 활용
    static void insertSort(int[] arr, int N) {

        for (int i = 1; i < N; i++) {
            if (arr[i - 1] > arr[i]) {
                for (int j = 0; j <= i - 1; j++) {
                    if (arr[j] > arr[i]) {
                        swap(arr, i, j);
                    }
                }
            }
        }
    }
	
    // swap
    static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        int [] arr = new int [N];

        for(int i=0;i<N;i++){
        arr[i] = Integer.parseInt(br.readLine());
        }

        insertSort(arr, arr.length);
        
        for(int i=0;i<N;i++){
            System.out.println(arr[i]);
        }

    }
}

정렬 문제입니다! 메서드 활용하지 않고 삽입 정렬 구현해서 풀었습니다!

감사합니다!🐳🐳🐳

profile
https://github.com/min731

1개의 댓글

comment-user-thumbnail
2023년 1월 30일

👍

답글 달기