From 82d918844513fb9501b247e665b433567adce47a Mon Sep 17 00:00:00 2001 From: caichengyang Date: Sun, 25 Jun 2023 09:15:17 +0800 Subject: [PATCH 1/2] fix counting sort --- basic/sorting/CountingSort/CountingSort.go | 36 ++++++++++-- basic/sorting/CountingSort/CountingSort.java | 27 +++++++-- basic/sorting/CountingSort/README.md | 61 -------------------- 3 files changed, 54 insertions(+), 70 deletions(-) diff --git a/basic/sorting/CountingSort/CountingSort.go b/basic/sorting/CountingSort/CountingSort.go index 30abaa800670e..4ae2e8486089d 100644 --- a/basic/sorting/CountingSort/CountingSort.go +++ b/basic/sorting/CountingSort/CountingSort.go @@ -1,5 +1,11 @@ package main +import ( + "fmt" + "log" + "time" +) + func CountingSort(nums []int, min, max int) { n := len(nums) k := max - min + 1 @@ -7,20 +13,42 @@ func CountingSort(nums []int, min, max int) { for _, v := range nums { c[v-min]++ } + log.Println(c) for i := 1; i < k; i++ { c[i] += c[i-1] } + log.Println(c) r := make([]int, n) for i := n - 1; i >= 0; i-- { v := nums[i] - a := c[v] - r[a-1] = v + min - c[v]-- + a := c[v-min] + r[a-1] = v + c[v-min]-- + log.Println(r) } for i, v := range r { nums[i] = v } -} \ No newline at end of file + log.Println(nums) +} + +func main() { + // uncomment following line to enable log print + // log.SetOutput(io.Discard) + + // test case 1 + nums := []int{1, 2, 7, 9, 5, 5, 8} + CountingSort(nums, 1, 9) + fmt.Println(nums) + + // wait complete output to terminal + time.Sleep(time.Second) + + // test case 2 + nums = []int{3, 7, 9, 5, 5, 8, 11} + CountingSort(nums, 3, 11) + fmt.Println(nums) +} diff --git a/basic/sorting/CountingSort/CountingSort.java b/basic/sorting/CountingSort/CountingSort.java index e102d08e58ef3..f4f5dd6e5758d 100644 --- a/basic/sorting/CountingSort/CountingSort.java +++ b/basic/sorting/CountingSort/CountingSort.java @@ -1,21 +1,38 @@ +import java.util.Arrays; + public class CountingSort { - public static void sort(int[] nums, int min, int max) { + public static void countingSort(int[] nums, int min, int max) { int n = nums.length; int k = max - min + 1; int[] c = new int[k]; for (int v : nums) { c[v - min]++; } + for (int i = 1; i < k; i++) { c[i] += c[i - 1]; } + int[] r = new int[n]; for (int i = n - 1; i >= 0; i--) { int v = nums[i]; - int a = c[v]; - r[a - 1] = v + min; - c[v]--; + int a = c[v - min]; + r[a - 1] = v; + c[v - min]--; } System.arraycopy(r, 0, nums, 0, n); } -} \ No newline at end of file + + public static void main(String[] args) { + + // test case 1 + int[] nums = { 1, 2, 7, 9, 5, 5, 8 }; + countingSort(nums, 1, 9); + System.out.println(Arrays.toString(nums)); + + // test case 2 + int[] nums2 = { 2, 7, 9, 5, 5, 8 }; + countingSort(nums2, 2, 9); + System.out.println(Arrays.toString(nums2)); + } +} diff --git a/basic/sorting/CountingSort/README.md b/basic/sorting/CountingSort/README.md index 1faf81ae32315..fc76b5e38cedc 100644 --- a/basic/sorting/CountingSort/README.md +++ b/basic/sorting/CountingSort/README.md @@ -10,67 +10,6 @@ - 创建结果数组 `r`,起始索引 `i` - 遍历数组 `c`,找出其中元素大于 `0` 的元素,将其对应的索引作为元素值填充到 `r` 数组中,每处理一次,`c` 中的元素值减 `1`,直到该元素值不大于 `0`,依次处理 `c` 中剩下的元素 -## 代码示例 - - - -### **Java** - -```java -public class CountingSort { - public static void sort(int[] nums, int min, int max) { - int n = nums.length; - int k = max - min + 1; - int[] c = new int[k]; - for (int v : nums) { - c[v - min]++; - } - for (int i = 1; i < k; i++) { - c[i] += c[i - 1]; - } - int[] r = new int[n]; - for (int i = n - 1; i >= 0; i--) { - int v = nums[i]; - int a = c[v]; - r[a - 1] = v + min; - c[v]--; - } - System.arraycopy(r, 0, nums, 0, n); - } -} -``` - -### **Go** - -```go -func CountingSort(nums []int, min, max int) { - n := len(nums) - k := max - min + 1 - c := make([]int, k) - for _, v := range nums { - c[v-min]++ - } - - for i := 1; i < k; i++ { - c[i] += c[i-1] - } - - r := make([]int, n) - for i := n - 1; i >= 0; i-- { - v := nums[i] - a := c[v] - r[a-1] = v + min - c[v]-- - } - - for i, v := range r { - nums[i] = v - } -} -``` - - - ## 算法分析 - 时间复杂度 $O(n+k)$, 其中 $n$ 为排序数组长度,而 $k$ 为排序数组中数值的取值范围,当 $k\lt n$ 时,时间复杂度为 $O(n)$。 From a05ae00689843dd78ae3fde8798aa1348f2352a7 Mon Sep 17 00:00:00 2001 From: caichengyang Date: Tue, 27 Jun 2023 08:55:07 +0800 Subject: [PATCH 2/2] reformat --- basic/sorting/CountingSort/CountingSort.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basic/sorting/CountingSort/CountingSort.java b/basic/sorting/CountingSort/CountingSort.java index f4f5dd6e5758d..e5b8403c39c8d 100644 --- a/basic/sorting/CountingSort/CountingSort.java +++ b/basic/sorting/CountingSort/CountingSort.java @@ -26,12 +26,12 @@ public static void countingSort(int[] nums, int min, int max) { public static void main(String[] args) { // test case 1 - int[] nums = { 1, 2, 7, 9, 5, 5, 8 }; + int[] nums = {1, 2, 7, 9, 5, 5, 8}; countingSort(nums, 1, 9); System.out.println(Arrays.toString(nums)); // test case 2 - int[] nums2 = { 2, 7, 9, 5, 5, 8 }; + int[] nums2 = {2, 7, 9, 5, 5, 8}; countingSort(nums2, 2, 9); System.out.println(Arrays.toString(nums2)); }