GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/basic/sorting/MergeSort/Main.java at main · imranmd/leetcode Expand file tree/ Copy pathMain.javaMore file actions 44 lines (41 loc) · 1.16 KB / Copy pathMain.javaFile metadata and controls44 lines (41 loc) · 1.16 KB 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 import java.util.Scanner; public class Main { private static int[] tmp = new int[100010]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; ++i) { nums[i] = sc.nextInt(); } mergeSort(nums, 0, n - 1); for (int x : nums) { System.out.print(x + " "); } } public static void mergeSort(int[] nums, int left, int right) { if (left >= right) { return; } int mid = (left + right) >> 1; mergeSort(nums, left, mid); mergeSort(nums, mid + 1, right); int i = left, j = mid + 1, k = 0; while (i <= mid && j <= right) { if (nums[i] <= nums[j]) { tmp[k++] = nums[i++]; } else { tmp[k++] = nums[j++]; } } while (i <= mid) { tmp[k++] = nums[i++]; } while (j <= right) { tmp[k++] = nums[j++]; } for (i = left, j = 0; i <= right; ++i, ++j) { nums[i] = tmp[j]; } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |