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 <huangjianqing@52tt.com>
This commit is contained in:
Jefferson Huang
2023-05-18 19:05:48 +08:00
committed by GitHub
parent ec4202031e
commit 335bc29af2
5 changed files with 224 additions and 0 deletions

View File

@ -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<int, int> map, int target) {
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
if (!map.containsKey(target)) {
return -1;
}
return map[target]!;
}
/* 哈希查找(链表) */
ListNode? hashingSearchLinkedList(Map<int,ListNode> map, int target) {
// 哈希表的 key: 目标节点值value: 节点对象
// 若哈希表中无此 key ,返回 null
if (!map.containsKey(target)) {
return null;
}
return map[target]!;
}
/* Driver Code */
void main(){
int target = 3;
/* 哈希查找(数组) */
List<int> nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// 初始化哈希表
Map<int,int> 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<int,ListNode> map1 = HashMap();
while (head != null) {
map1.putIfAbsent(head.val, () => head!); // key: 节点值value: 节点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
print('目标节点值 3 的对应节点对象为 $node');
}

View File

@ -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<int> 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<int> 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');
}

View File

@ -0,0 +1,39 @@
/**
* File: bucket_sort.dart
* Created Time: 2023-05-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
/* 桶排序 */
void bucketSort(List<double> nums) {
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
int k = nums.length ~/ 2;
List<List<double>> 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<double> bucket in buckets) {
bucket.sort();
}
// 3. 遍历桶合并结果
int i = 0;
for (List<double> 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');
}

View File

@ -0,0 +1,72 @@
/**
* File: counting_sort.dart
* Created Time: 2023-05-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'dart:math';
/* 计数排序 */
// 简单实现,无法用于排序对象
void countingSortNaive(List<int> nums) {
// 1. 统计数组最大元素 m
int m = 0;
for (int num in nums) {
m = max(m, num);
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
List<int> 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<int> nums) {
// 1. 统计数组最大元素 m
int m = 0;
for (int num in nums) {
m = max(m, num);
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
List<int> 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<int> 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');
}

View File

@ -18,3 +18,14 @@ class ListNode {
return 'ListNode{val: $val, next: $next}'; return 'ListNode{val: $val, next: $next}';
} }
} }
/* Generate a linked list with a vector */
ListNode? listToLinkedList(List<int> list) {
ListNode dum = ListNode(0);
ListNode? head = dum;
for (int val in list) {
head?.next = ListNode(val);
head = head?.next;
}
return dum.next;
}