From 335bc29af22bd8b49f0185ff7f6902bf3c8ffe0a Mon Sep 17 00:00:00 2001 From: Jefferson Huang Date: Thu, 18 May 2023 19:05:48 +0800 Subject: [PATCH] feat: add chapter_sorting and chapter_searching by dart (#497) * feat: add chapter_sorting by dart * feat: add chapter_searching by dart --------- Co-authored-by: huangjianqing --- .../chapter_searching/hashing_search.dart | 54 ++++++++++++++ .../dart/chapter_searching/linear_search.dart | 48 +++++++++++++ codes/dart/chapter_sorting/bucket_sort.dart | 39 ++++++++++ codes/dart/chapter_sorting/counting_sort.dart | 72 +++++++++++++++++++ codes/dart/utils/list_node.dart | 11 +++ 5 files changed, 224 insertions(+) create mode 100644 codes/dart/chapter_searching/hashing_search.dart create mode 100644 codes/dart/chapter_searching/linear_search.dart create mode 100644 codes/dart/chapter_sorting/bucket_sort.dart create mode 100644 codes/dart/chapter_sorting/counting_sort.dart diff --git a/codes/dart/chapter_searching/hashing_search.dart b/codes/dart/chapter_searching/hashing_search.dart new file mode 100644 index 000000000..8c0b5e65e --- /dev/null +++ b/codes/dart/chapter_searching/hashing_search.dart @@ -0,0 +1,54 @@ +/** + * File: hashing_search.dart + * Created Time: 2023-05-12 + * Author: Jefferson (JeffersonHuang77@gmail.com) + */ + +import 'dart:collection'; +import '../utils/list_node.dart'; + +/* 哈希查找(数组) */ +int hashingSearchArray(Map map, int target) { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + if (!map.containsKey(target)) { + return -1; + } + return map[target]!; +} + +/* 哈希查找(链表) */ +ListNode? hashingSearchLinkedList(Map map, int target) { + // 哈希表的 key: 目标节点值,value: 节点对象 + // 若哈希表中无此 key ,返回 null + if (!map.containsKey(target)) { + return null; + } + return map[target]!; +} + +/* Driver Code */ +void main(){ + int target = 3; + + /* 哈希查找(数组) */ + List nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + // 初始化哈希表 + Map map = HashMap(); + for (int i = 0; i < nums.length; i++) { + map.putIfAbsent(nums[i], () => i);// key: 元素,value: 索引 + } + int index = hashingSearchArray(map, target); + print('目标元素 3 的索引 = $index'); + + /* 哈希查找(链表) */ + ListNode? head = listToLinkedList(nums); + // 初始化哈希表 + Map map1 = HashMap(); + while (head != null) { + map1.putIfAbsent(head.val, () => head!); // key: 节点值,value: 节点 + head = head.next; + } + ListNode? node = hashingSearchLinkedList(map1, target); + print('目标节点值 3 的对应节点对象为 $node'); +} \ No newline at end of file diff --git a/codes/dart/chapter_searching/linear_search.dart b/codes/dart/chapter_searching/linear_search.dart new file mode 100644 index 000000000..24a90fc99 --- /dev/null +++ b/codes/dart/chapter_searching/linear_search.dart @@ -0,0 +1,48 @@ +/** + * File: linear_search.dart + * Created Time: 2023-05-12 + * Author: Jefferson (JeffersonHuang77@gmail.com) + */ + +import '../utils/list_node.dart'; + +/* 线性查找(数组) */ +int linearSearchArray(List nums, int target) { + // 遍历数组 + for (int i = 0; i < nums.length; i++) { + // 找到目标元素,返回其索引 + if (nums[i] == target) { + return i; + } + } + // 未找到目标元素,返回 -1 + return -1; +} + +/* 线性查找(链表) */ +ListNode? linearSearchList(ListNode? head, int target) { + // 遍历链表 + while (head != null) { + // 找到目标节点,返回之 + if (head.val == target) + return head; + head = head.next; + } + // 未找到目标元素,返回 null + return null; +} + +/* Driver Code */ +void main(){ + int target = 3; + + /* 在数组中执行线性查找 */ + List nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + int index = linearSearchArray(nums, target); + print('目标元素 3 的索引 = $index'); + + /* 在链表中执行线性查找 */ + ListNode? head = listToLinkedList(nums); + ListNode? node = linearSearchList(head, target); + print('目标节点值 3 的对应节点对象为 $node'); +} \ No newline at end of file diff --git a/codes/dart/chapter_sorting/bucket_sort.dart b/codes/dart/chapter_sorting/bucket_sort.dart new file mode 100644 index 000000000..812304043 --- /dev/null +++ b/codes/dart/chapter_sorting/bucket_sort.dart @@ -0,0 +1,39 @@ +/** + * File: bucket_sort.dart + * Created Time: 2023-05-12 + * Author: Jefferson (JeffersonHuang77@gmail.com) + */ + +/* 桶排序 */ +void bucketSort(List nums) { + // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 + int k = nums.length ~/ 2; + List> buckets = List.generate(k, (index) => []); + + // 1. 将数组元素分配到各个桶中 + for (double num in nums) { + // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] + int i = (num * k).toInt(); + // 将 num 添加进桶 bucket_idx + buckets[i].add(num); + } + // 2. 对各个桶执行排序 + for (List bucket in buckets) { + bucket.sort(); + } + // 3. 遍历桶合并结果 + int i = 0; + for (List bucket in buckets) { + for (double num in bucket) { + nums[i++] = num; + } + } +} + +/* Driver Code*/ +void main() { + // 设输入数据为浮点数,范围为 [0, 1) + final nums = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]; + bucketSort(nums); + print('桶排序完成后 = $nums'); +} diff --git a/codes/dart/chapter_sorting/counting_sort.dart b/codes/dart/chapter_sorting/counting_sort.dart new file mode 100644 index 000000000..e3ccd4ffc --- /dev/null +++ b/codes/dart/chapter_sorting/counting_sort.dart @@ -0,0 +1,72 @@ +/** + * File: counting_sort.dart + * Created Time: 2023-05-12 + * Author: Jefferson (JeffersonHuang77@gmail.com) + */ +import 'dart:math'; + +/* 计数排序 */ +// 简单实现,无法用于排序对象 +void countingSortNaive(List nums) { + // 1. 统计数组最大元素 m + int m = 0; + for (int num in nums) { + m = max(m, num); + } + // 2. 统计各数字的出现次数 + // counter[num] 代表 num 的出现次数 + List counter = List.filled(m + 1, 0); + for (int num in nums) { + counter[num]++; + } + // 3. 遍历 counter ,将各元素填入原数组 nums + int i = 0; + for (int num = 0; num < m + 1; num++) { + for (int j = 0; j < counter[num]; j++, i++) { + nums[i] = num; + } + } +} + +/* 计数排序 */ +// 完整实现,可排序对象,并且是稳定排序 +void countingSort(List nums) { + // 1. 统计数组最大元素 m + int m = 0; + for (int num in nums) { + m = max(m, num); + } + // 2. 统计各数字的出现次数 + // counter[num] 代表 num 的出现次数 + List counter = List.filled(m + 1, 0); + for (int num in nums) { + counter[num]++; + } + // 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引” + // 即 counter[num]-1 是 num 在 res 中最后一次出现的索引 + for (int i = 0; i < m; i++) { + counter[i + 1] += counter[i]; + } + // 4. 倒序遍历 nums ,将各元素填入结果数组 res + // 初始化数组 res 用于记录结果 + int n = nums.length; + List res = List.filled(n, 0); + for (int i = n - 1; i >= 0; i--) { + int num = nums[i]; + res[counter[num] - 1] = num; // 将 num 放置到对应索引处 + counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引 + } + // 使用结果数组 res 覆盖原数组 nums + nums.setAll(0, res); +} + +/* Driver Code*/ +void main() { + final nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]; + countingSortNaive(nums); + print('计数排序(无法排序对象)完成后 nums = $nums'); + + final nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]; + countingSort(nums1); + print('计数排序完成后 nums1 = $nums1'); +} diff --git a/codes/dart/utils/list_node.dart b/codes/dart/utils/list_node.dart index 163f9179b..2f9b522fa 100644 --- a/codes/dart/utils/list_node.dart +++ b/codes/dart/utils/list_node.dart @@ -18,3 +18,14 @@ class ListNode { return 'ListNode{val: $val, next: $next}'; } } + +/* Generate a linked list with a vector */ +ListNode? listToLinkedList(List list) { + ListNode dum = ListNode(0); + ListNode? head = dum; + for (int val in list) { + head?.next = ListNode(val); + head = head?.next; + } + return dum.next; +}