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.util.*; class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = in.nextInt(); } sort(arr, 0, arr.length - 1); for(int i = 0; i < n; i++){ System.out.print(arr[i] + " "); } } public static void sort(int[] arr, int left, int right){ int middle = left + right >> 1; if(left >= right){ return; } sort(arr, left, middle); sort(arr, middle + 1, right); int[] tmp = new int[right - left + 1]; int k = 0; int l = left; int r = middle + 1; while(l <= middle && r <= right){ if(arr[l] <= arr[r]){ tmp[k++] = arr[l++]; }else{ tmp[k++] = arr[r++]; } } while(l <= middle){ tmp[k++] = arr[l++]; } while(r <= right){ tmp[k++] = arr[r++]; } for(int i = left , j = 0; i <= right; i++,j++){ arr[i] = tmp[j]; } } }
|