퀵정렬 이용한 내 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int i;
int[] list = new int[n];
for(i = 0; i < n; i++){
list[i] = Integer.parseInt(br.readLine());
}
quick_sort(list, 0, n-1);
for(i = 0; i<n; i++){
System.out.println(list[i]);
}
}
public static int partition(int[] list, int left, int right){
int pivot = list[left];
int low = left + 1;
int high = right;
while(true){
while (low < right && list[low] < pivot) {
low++;
}
while (high > left && list[high] > pivot) {
high--;
}
if (low >= high) break;
swap(list, low, high);
}
swap(list, left, high);
return high;
}
public static void quick_sort(int[] list, int left, int right){
if(left < right){
int q = partition(list, left, right);
quick_sort(list, left, q-1);
quick_sort(list, q+1, right);
}
}
public static void swap(int[] list, int i, int j){
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
'백준' 카테고리의 다른 글
백준 문제 풀이 25305 | JAVA (0) | 2024.06.25 |
---|---|
백준 문제 풀이 2587 | JAVA (0) | 2024.06.25 |
백준 문제 풀이 2839 | JAVA (0) | 2024.06.24 |
백준 문제 풀이 2231 | JAVA (0) | 2024.06.24 |
백준 문제 풀이 19532 | JAVA (0) | 2024.06.24 |