From be196d6143948b540d33e15365139ace9139caa6 Mon Sep 17 00:00:00 2001 From: liufq <315458488@qq.com> Date: Fri, 24 Feb 2023 17:43:40 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=BD=92=E5=B9=B6=E7=AE=97=E6=B3=95=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 40 +++++++++++++++++++ .../exercies.md" | 29 ++++++++++++++ .../test_cases/1.in" | 1 + .../test_cases/1.out" | 1 + .../test_cases/2.in" | 1 + .../test_cases/2.out" | 1 + .../test_cases/3.in" | 1 + .../test_cases/3.out" | 1 + 8 files changed, 75 insertions(+) create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/Solution.java" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/exercies.md" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.in" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/1.out" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.in" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/2.out" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.in" create mode 100644 "exercises/liufq/\344\270\255\347\255\211/\345\275\222\345\271\266\346\216\222\345\272\217/test_cases/3.out" 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 0000000..690f131 --- /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 0000000..54b8051 --- /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 0000000..4454cf2 --- /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 0000000..9d4948b --- /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 0000000..bac3533 --- /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 0000000..74b2936 --- /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 0000000..0da1596 --- /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 0000000..82b8709 --- /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 -- GitLab