mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 17:09:46 +08:00
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:
39
codes/dart/chapter_sorting/bucket_sort.dart
Normal file
39
codes/dart/chapter_sorting/bucket_sort.dart
Normal 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');
|
||||
}
|
||||
72
codes/dart/chapter_sorting/counting_sort.dart
Normal file
72
codes/dart/chapter_sorting/counting_sort.dart
Normal 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');
|
||||
}
|
||||
Reference in New Issue
Block a user