diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/Solution.java" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/Solution.java" new file mode 100644 index 0000000000000000000000000000000000000000..690f1319a4b9c95bca4a384a7d64370e46fbf5f6 --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/Solution.java" @@ -0,0 +1,40 @@ +class Solution { + public static void mergeSort(int[] nums) { + if (nums == null || nums.length == 0) { + return; + } + int[] helper = new int[nums.length]; + mergeSort(nums, helper, 0, nums.length - 1); + } + + private static void mergeSort(int[] nums, int[] helper, int low, int high) { + if (low < high) { + int middle = low + (high - low) / 2; + mergeSort(nums, helper, low, middle); + mergeSort(nums, helper, middle + 1, high); + merge(nums, helper, low, middle, high); + } + } + + private static void merge(int[] nums, int[] helper, int low, int middle, int high) { + for (int i = low; i <= high; i++) { + helper[i] = nums[i]; + } + + int left = low; + int right = middle + 1; + int curr = low; + while (left <= middle && right <= high) { + if (helper[left] <= helper[right]) { + nums[curr++] = helper[left++]; + } else { + nums[curr++] = helper[right++]; + } + } + + int remain = middle - left; + for (int i = 0; i <= remain; i++) { + nums[curr + i] = helper[left + i]; + } + } +} \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/exercies.md" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/exercies.md" new file mode 100644 index 0000000000000000000000000000000000000000..54b8051b7f86043670007834fe4793dc20fa0d7e --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/exercies.md" @@ -0,0 +1,29 @@ +# 归并排序 + +你需要用归并排序算法,对数组进行排序,并输出按照升序排序的字符数字串 + +## 输入描述 + +你需要对输入的数组根据归并排序算法升序排序 + +## 输出描述 + +输出按照升序排序的数组 + +## 输入样例 + +输入nums数组为: +1. int[] nums = new int{}[1,2,4,3,5,6,7,8]; +2. int[] nums = new int{}[1,5,6,10,12,15,2,4]; +3. int[] nums = new int{}[1,4,90,12,14,19,3,4]; + + +## 输出样例 +1. 1,2,3,4,5,6,7,8 +2. 1,2,4,5,6,10,12,15 +3. 1,3,4,4,12,14,19,90 + + +## 提示 + +无 \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.in" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.in" new file mode 100644 index 0000000000000000000000000000000000000000..4454cf2fccf397895f2363be855c93c68cbcff10 --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.in" @@ -0,0 +1 @@ +int[] nums = new int{}[1,2,4,3,5,6,7,8]; \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.out" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.out" new file mode 100644 index 0000000000000000000000000000000000000000..9d4948b8a8d5fb020701f61cb8435a942d7ae90e --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.out" @@ -0,0 +1 @@ +1,2,3,4,5,6,7,8 \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.in" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.in" new file mode 100644 index 0000000000000000000000000000000000000000..bac353332f0baa49e150bf528a67b14ff151f4a0 --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.in" @@ -0,0 +1 @@ +int[] nums = new int{}[1,5,6,10,12,15,2,4]; \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.out" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.out" new file mode 100644 index 0000000000000000000000000000000000000000..74b2936b9c877306eff97fb39d1268b93d2cd3ff --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.out" @@ -0,0 +1 @@ +1,2,4,5,6,10,12,15 \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.in" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.in" new file mode 100644 index 0000000000000000000000000000000000000000..0da1596da1a5f41e445a957366a92c71b4436c35 --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.in" @@ -0,0 +1 @@ +int[] nums = new int{}[1,4,90,12,14,19,3,4]; \ No newline at end of file diff --git "a/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.out" "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.out" new file mode 100644 index 0000000000000000000000000000000000000000..82b8709c0017f931dc2ce2a94680dd22d8a36292 --- /dev/null +++ "b/exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.out" @@ -0,0 +1 @@ +1,3,4,4,12,14,19,90 \ No newline at end of file