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>
pull/506/head
Jefferson Huang 2 years ago committed by GitHub
parent ec4202031e
commit 335bc29af2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

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

@ -18,3 +18,14 @@ class ListNode {
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;
}

Loading…
Cancel
Save