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..4f8e810d41a1e6f57d80056fbd49be46c2596b06 --- /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,43 @@ +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); + for (int i = 0; i < nums.length; i++) { + System.out.println(nums[i]); + } + } + + 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..d389427c3ac28310942c67268f598c05c35f4882 --- /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,31 @@ +# 归并排序 + +你需要用归并排序算法,对数组进行排序,并输出按照升序排序的字符数字串 +归并排序算法的基本思想是将两个(或两个以上)有序表合并成一个新的有序表, +即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。 + +## 输入描述 + +你需要对输入的数组根据归并排序算法升序排序 + +## 输出描述 + +输出按照升序排序的数组 + +## 输入样例 + +输入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..896ddd8ddc41eaf9497d161cfd9657b729ac97d5 --- /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,8 @@ +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..db3048de0a3d4975ed015aaca989b07cf086037c --- /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,8 @@ +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..01dbdb90361ae2ceae8dd5591c39b7d397cb02a0 --- /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,8 @@ +1 +3 +4 +4 +12 +14 +19 +90 \ No newline at end of file