Skip to content

Commit 5c88863

Browse files
author
tianqing.liang
committed
计数排序
1 parent b99fc4b commit 5c88863

File tree

5 files changed

+159
-4
lines changed

5 files changed

+159
-4
lines changed

21-Hash-Table/Solution.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Solution {
99

1010
List freq = List.empty(growable: true);
1111
for(int i = 0 ; i < s.length ; i ++) {
12-
freq[int.parse(s[i]) - int.parse('a')] ++;
12+
freq[s[i].codeUnits[0] - 'a'.codeUnits[0]] ++;
1313
}
1414
for(int i = 0 ; i < s.length ; i ++) {
15-
if (freq[int.parse(s[i]) - int.parse('a')] == 1) {
15+
if (s[i].codeUnits[0] - 'a'.codeUnits[0] == 1) {
1616
return i;
1717
}
1818
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import 'Student.dart';
2+
import 'dart:math';
3+
4+
void main() {
5+
print("abcdefghijklmnopqrstuvwxyz".codeUnits);
6+
print("abcdefghijklmnopqrstuvwxyz".runes);
7+
print(String.fromCharCode("abcdefghijklmnopqrstuvwxyz".codeUnits[0]));
8+
print(String.fromCharCode("abcdefghijklmnopqrstuvwxyz".codeUnits[0]));
9+
Iterable<int>? string = [
10+
97,
11+
98,
12+
99,
13+
100,
14+
101,
15+
102,
16+
103,
17+
104,
18+
105,
19+
106,
20+
107,
21+
108,
22+
109,
23+
110,
24+
111,
25+
112,
26+
113,
27+
114,
28+
115,
29+
116,
30+
117,
31+
118,
32+
119,
33+
120,
34+
121,
35+
122
36+
];
37+
print(String.fromCharCodes(string, 0, 26));
38+
39+
int n = 26 * 26 * 26 * 26;
40+
41+
List students = List.filled(n, Student, growable: true);
42+
int k = 0;
43+
Random rnd = new Random();
44+
for (var c1 = 'a'.codeUnits[0]; c1 <= 'z'.codeUnits[0]; c1++)
45+
for (var c2 = 'a'.codeUnits[0]; c2 <= 'z'.codeUnits[0]; c2++)
46+
for (var c3 = 'a'.codeUnits[0]; c3 <= 'z'.codeUnits[0]; c3++)
47+
for (var c4 = 'a'.codeUnits[0]; c4 <= 'z'.codeUnits[0]; c4++) {
48+
students[k] = new Student(
49+
"${String.fromCharCode(c1)} + ${String.fromCharCode(c2)} + ${String.fromCharCode(c3)} + ${String.fromCharCode(c4)}",
50+
rnd.nextInt(101));
51+
k++;
52+
}
53+
54+
// 计数排序过程
55+
int R = 101;
56+
57+
// O(n)
58+
List cnt = List.filled(R, int, growable: true);
59+
for (Student student in students) cnt[student.getScore()!]++;
60+
61+
// O(R)
62+
List index = List.filled(R + 1, int, growable: true);
63+
for (int i = 0; i < R; i++) index[i + 1] = index[i] + cnt[i];
64+
65+
// O(n)
66+
List temp = List.filled(n, Student, growable: true);
67+
for (Student student in students) {
68+
temp[index[student.getScore()!]] = student;
69+
index[student.getScore()!]++;
70+
}
71+
72+
// O(n)
73+
for (int i = 0; i < n; i++) students[i] = temp[i];
74+
75+
// O(n + R)
76+
77+
// 验证计数排序算法
78+
for (int i = 1; i < n; i++) {
79+
if (students[i - 1].getScore() > students[i].getScore())
80+
throw new Exception("Sort failed");
81+
82+
if (students[i - 1].getScore() == students[i].getScore()) {
83+
if (students[i - 1].getName().compareTo(students[i].getName()) >= 0)
84+
throw new Exception("Non-Stable counting sort!");
85+
}
86+
}
87+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
sortColors(List<int> nums) {
3+
List? cnt = List.filled(3, int, growable: true);
4+
for (int num in nums!) {
5+
cnt[num]++;}
6+
7+
for (int i = 0; i < cnt[0]; i++) {
8+
nums[i] = 0;
9+
}
10+
for (int i = cnt[0]; i < cnt[0] + cnt[1]; i++) {
11+
nums[i] = 1;
12+
}
13+
for (int i = cnt[0] + cnt[1]; i < cnt[0] + cnt[1] + cnt[2]; i++) {
14+
nums[i] = 2;
15+
}
16+
}
17+
18+
sortColors2(List<int> nums) {
19+
// 处理元素取值范围是 [0, R) 的计数排序
20+
int R = 3;
21+
22+
List? cnt = List.filled(3, int, growable: true);
23+
for (int num in nums!) {
24+
cnt[num]++;
25+
}
26+
27+
// [index[i], index[i + 1]) 的值为 i
28+
List? index = List.filled(R + 1, , int, growable: true);
29+
for (int i = 0; i < R; i++) {
30+
index[i + 1] = index[i] + cnt[i];
31+
}
32+
33+
for (int i = 0; i + 1 < index.length; i++) {
34+
// [index[i], index[i + 1]) 的值为 i
35+
for (int j = index[i]; j < index[i + 1]; j++){
36+
nums[j] = i;
37+
}
38+
}
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Student {
2+
3+
String? name;
4+
int? score;
5+
6+
Student(this.name,this.score);
7+
8+
@override
9+
num? compareTo(other) {
10+
return this.score! - other.score;
11+
}
12+
13+
String? getName(){
14+
return name;
15+
}
16+
17+
int? getScore(){
18+
return score;
19+
}
20+
21+
@override
22+
toString(){
23+
return "Student(name: $name, score: $score)";
24+
}
25+
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# AlgorithmAndDataArchitecture
22

3-
#### 介绍
4-
使用Dart语言编写 [慕课网算法与数据结构](https://class.imooc.com/sale/datastructure)
3+
## 介绍
4+
<h2>使用Dart语言重写数据结构与算法</h2>
5+
56
1. 线性搜索
67
2. 选择排序
78
3. 插入排序
@@ -23,6 +24,8 @@
2324
19. AVL树
2425
20. 红黑树
2526
21. 哈希表
27+
22. 计数排序
2628

2729
#### SDK版本
2830
1. 版本:2.12.3
31+
2. Java版本课程地址:[慕课网算法与数据结构](https://class.imooc.com/sale/datastructure)

0 commit comments

Comments
 (0)