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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, 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()); } br.close(); quickSort(arr); for (int i : arr) { System.out.println(i); } }
public static void quickSort(int[] arr) { sort(arr, 0, arr.length - 1); }
private static void sort(int[] arr, int start, int end) { if (start >= end) return;
int mid = partition(arr, start, end); sort(arr, start, mid - 1); sort(arr, mid, end); }
private static int partition(int[] arr, int start, int end) { int pivot = arr[(start + end) / 2]; while (start <= end) { while (arr[start] < pivot) start++; while (arr[end] > pivot) end--; if (start <= end) { swap(arr, start, end); start++; end--; } } return start; }
private static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } }
|