버블정렬?
https://gmlwjd9405.github.io/2018/05/06/algorithm-bubble-sort.html
버블정렬 ver.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N - 1 - i; j++) {
int tmp = 0;
if (arr[j] < arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
for (int i = N - 1; i >= 0; i--) {
bw.write(arr[i] + "\n");
}
bw.flush();
bw.close();
}
}
Arrays.sort ver.
import java.io.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
for (int i : arr) {
bw.write(i + "\n");
}
bw.flush();
bw.close();
}
}
'Algorithm' 카테고리의 다른 글
백준 1918번 :: 후위 표기식 (0) | 2023.07.21 |
---|---|
백준 1874번 :: 스택 수열 (0) | 2023.07.13 |
백준 1181번 :: 단어 정렬 (0) | 2023.07.06 |
백준 10816번 :: 숫자 카드 2 (0) | 2023.06.23 |
프로그래머스 :: 소수만들기 (0) | 2023.04.10 |