정렬 - Bubble Sort

이형석·2024년 1월 22일

알고리즘 Phase1

목록 보기
2/59

정리
주석 //bubble sort 아래 코드 참고

import java.io.*;
import java.util.*;

public class BubbleSort {
    public static void main(String[] args) throws 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());
        }

        // bubble sort
        for(int i = 0; i < n-1; i++){
            //처음~(마지막칸-1)칸까지

            //지금칸_비교_다음칸 => n-1칸, 그다음은 n-2.. 즉 n-1-i번
            for(int j = 0; j < n-1-i; j++){
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
        for(int i = 0; i < n; i++){
            System.out.println(arr[i]);
        }    
    }
}
profile
금융IT 개발자

0개의 댓글