just do it

백준2750번/버블정렬/java 본문

자료구조&알고리즘/코딩테스트

백준2750번/버블정렬/java

밍풀 2022. 10. 14. 16:01
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
import java.util.Scanner;
 
public class Main {
//수의 갯수 N과 N개의 자연수 받고 오름차순정렬하기, 버블정렬
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        
        int A[]=new int[N];
        
        for(int i=0; i<N;i++) {
            A[i]=sc.nextInt();    
        }//N개의 수 다 받아줌
        
        for(int i=0;i<N-1;i++) {
         for(int j=0; j<N-1-i;j++) {//뒤에 i개는 정렬 됐으니 제외
            if(A[j]>A[j+1]) {
                int temp = A[j];
                A[j]=A[j+1];
                A[j+1]=temp;
            }
        }
 
    }
        for(int i=0;i<N;i++) {
        System.out.println(A[i]);
        }
    
 
    }
}
 
cs