본문 바로가기

Algorithm/Language Coder

Jungol (Java) - 175 : 함수2 - 형성평가1

문제

정수 N을 입력받고 다시 N개의 정수를 입력받아 내림차순으로 정렬하여 출력하는 프로그램을 작성하시오. (1 ≤ N ≤ 15, 입력과 출력,

정렬은 모두 함수를 이용할 것)

 

입력 예

5

12 35 1 48 9

 

출력 예

48 35 12 9 1

 

import java.util.*;

public class Main {
    public static void print() {
        Scanner sc = new Scanner(System.in);
        int n = 0;
        int bubble;
        do {
            n = sc.nextInt();
        } while (n < 1 || n > 15);

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j + 1]) {
                    bubble = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = bubble;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void main(String[] args) {
        print();
    }
}