Skip to content

Commit 7acb29b

Browse files
author
tianqing.liang
committed
LSD排序,MSD排序,桶排序
1 parent d95f037 commit 7acb29b

File tree

11 files changed

+587
-4
lines changed

11 files changed

+587
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:math';
2+
3+
class ArrayGenerator{
4+
5+
static List generateOrderedArray(int n){
6+
List arr = List.filled(n, true);
7+
for(int i = 0;i<n;i++){
8+
arr[i] =i;
9+
}
10+
return arr;
11+
}
12+
13+
static List generateRandomArray(int n,int bound){
14+
List arr = List.filled(n, true);
15+
Random random = new Random();
16+
for(int i = 0; i < n; i ++){
17+
arr[i] = random.nextInt(bound);
18+
}
19+
return arr;
20+
}
21+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class LSDSort {
2+
LSDSort() {}
3+
4+
static sort(List? arr, int W) {
5+
for (String s in arr!)
6+
if (s.length != W) {
7+
throw new Exception("All Strings' length must be the same.");
8+
}
9+
int R = 256;
10+
11+
List temp = List.filled(arr.length, String, growable: true);
12+
List index = List.filled(R + 1, 0, growable: true);
13+
for (int r = W - 1; r >= 0; r--) {
14+
// O(n)
15+
List cnt = List.filled(R, 0, growable: true);
16+
for (String s in arr) {
17+
cnt[s[r].codeUnits[0]]++;
18+
}
19+
// O(R)
20+
for (int i = 0; i < R; i++) {
21+
index[i + 1] = index[i] + cnt[i];
22+
}
23+
// O(n)
24+
for (String s in arr) {
25+
temp[index[s[r].codeUnits[0]]] = s;
26+
index[s[r].codeUnits[0]]++;
27+
}
28+
// O(n)
29+
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
30+
}
31+
}
32+
33+
34+
}
35+
36+
void main() {
37+
List arr = ["BCA", "CAB", "ACB", "BAC", "ABC", "CBA"];
38+
LSDSort.sort(arr, 3);
39+
for (String s in arr) {
40+
print(s);
41+
}
42+
}
43+
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'SortingHelper.dart';
2+
import 'ArrayGenerator.dart';
3+
4+
/**
5+
* 希尔排序
6+
*/
7+
class ShellSort {
8+
//初始版本
9+
static sort(List? data) {
10+
int h = (data!.length / 2).toInt();
11+
while (h >= 1) {
12+
for (int start = 0; start < h; start++) {
13+
// 对 data[start, start + h, start + 2h, start + 3h ...], 进行插入排序
14+
for (int i = start + h; i < data.length; i += h) {
15+
var t = data[i];
16+
int j;
17+
for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h)
18+
data[j] = data[j - h];
19+
data[j] = t;
20+
}
21+
}
22+
h = (h / 2).toInt();
23+
}
24+
}
25+
26+
//希尔排序优化
27+
static sort2(List? data) {
28+
int h = (data!.length / 2).toInt();
29+
while (h >= 1) {
30+
for (int i = h; i < data.length; i++) {
31+
var t = data[i];
32+
int j;
33+
for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h) {
34+
data[j] = data[j - h];
35+
}
36+
data[j] = t;
37+
}
38+
h = (h / 2).toInt();
39+
}
40+
}
41+
42+
//希尔排序设定步长序列
43+
static sort3(List? data) {
44+
int h = 1;
45+
while (h < data!.length) {
46+
h = 3 * h + 1;
47+
}
48+
// 1, 4, 13, 40 ...
49+
while (h >= 1) {
50+
for (int i = h; i < data.length; i++) {
51+
var t = data[i];
52+
int j;
53+
for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h) {
54+
data[j] = data[j - h];
55+
}
56+
data[j] = t;
57+
}
58+
h = (h / 3).toInt();
59+
}
60+
}
61+
}
62+
63+
void main() {
64+
int n = 100000;
65+
List arr = ArrayGenerator.generateRandomArray(n, n);
66+
67+
SortingHelper.sortTest("ShellSort", arr);
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'ShellSort.dart';
2+
3+
class SortingHelper {
4+
SortingHelper() {}
5+
6+
static bool isSorted(List? arr) {
7+
for (int i = 1; i < arr!.length; i++)
8+
if (arr[i - 1].compareTo(arr[i]) > 0) return false;
9+
return true;
10+
}
11+
12+
static sortTest(String sortname, List? arr) {
13+
var now = new DateTime.now();
14+
num startTime = now.millisecondsSinceEpoch;
15+
16+
if (sortname == "SelectionSort") {
17+
// SelectionSort.sort(arr);
18+
} else if (sortname == "InsertionSort") {
19+
// InsertionSort.sort(arr);
20+
} else if (sortname == "MergeSort") {
21+
// MergeSort.sort(arr);
22+
} else if (sortname == "MergeSortBU") {
23+
// MergeSort.sortBU(arr);
24+
} else if (sortname == "QuickSort") {
25+
// QuickSort.sort(arr);
26+
} else if (sortname == "QuickSort2Ways") {
27+
// QuickSort.sort2ways(arr);
28+
} else if (sortname == "QuickSort3Ways") {
29+
// QuickSort.sort3ways(arr);
30+
} else if (sortname == "QuickSort3Ways") {
31+
// QuickSort.sort3ways(arr);
32+
} else if (sortname == "HeapSort") {
33+
// HeapSort.sort(arr);
34+
} else if (sortname == "BubbleSort") {
35+
// BubbleSort.sort(arr);
36+
} else if (sortname == "ShellSort") {
37+
ShellSort.sort(arr);
38+
}
39+
var endNow = new DateTime.now();
40+
num endTime = endNow.millisecondsSinceEpoch;
41+
print("开始时间:$startTime : 结束时间:$endTime");
42+
double time = (endTime - startTime) / 1000.0;
43+
if (!SortingHelper.isSorted(arr)) throw new Exception(sortname + " failed");
44+
print("$sortname , n = ${arr!.length}: $time s");
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:math';
2+
3+
class ArrayGenerator{
4+
5+
static List generateOrderedArray(int n){
6+
List arr = List.filled(n, true);
7+
for(int i = 0;i<n;i++){
8+
arr[i] =i;
9+
}
10+
return arr;
11+
}
12+
13+
static List generateRandomArray(int n,int bound){
14+
List arr = List.filled(n, true);
15+
Random random = new Random();
16+
for(int i = 0; i < n; i ++){
17+
arr[i] = random.nextInt(bound);
18+
}
19+
return arr;
20+
}
21+
}
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import 'ArrayGenerator.dart';
2+
import 'SortingHelper.dart';
3+
import 'dart:math';
4+
5+
class BucketSort {
6+
BucketSort() {}
7+
8+
static sort(List<int> arr, int B) {
9+
if (B <= 1) throw new Exception("B must be > 1");
10+
11+
List temp = List.filled(arr.length, 0, growable: true);
12+
_sortDetail(arr, 0, arr.length - 1, B, temp);
13+
}
14+
15+
static _sortDetail(List<int> arr, int left, int right, int B, List? temp) {
16+
if (left >= right) return;
17+
18+
int maxv = 1 >> 32, minv = 1 << 32;
19+
for (int i = left; i <= right; i++) {
20+
maxv = [maxv, arr[i]].reduce(max);
21+
minv = [minv, arr[i]].reduce(min);
22+
}
23+
24+
if (maxv == minv) {return;}
25+
26+
int d =
27+
((maxv - minv + 1) / B).toInt() + ((maxv - minv + 1) % B > 0 ? 1 : 0);
28+
29+
List cnt = List.filled(B, 0, growable: true);
30+
List index = List.filled(B + 1, 0, growable: true);
31+
32+
// O(n)
33+
for (int i = left; i <= right; i++) cnt[((arr[i] - minv) / d).toInt()]++;
34+
35+
// O(R)
36+
for (int i = 0; i < B; i++) {
37+
index[i + 1] = index[i] + cnt[i];
38+
}
39+
// O(n)
40+
for (int i = left; i <= right; i++) {
41+
int p = ((arr[i] - minv) / d).toInt();
42+
temp![(left + index[p]).toInt()] = arr[i];
43+
index[p]++;
44+
}
45+
46+
// O(n)
47+
for (int i = left; i <= right; i++) {
48+
arr[i] = temp![i];
49+
}
50+
51+
// 递归下去:
52+
_sortDetail(arr, left, (left + index[0] - 1).toInt(), B, temp);
53+
for (int i = 0; i < B - 1; i++)
54+
_sortDetail(arr, (left + index[i]).toInt(),
55+
(left + index[i + 1] - 1).toInt(), B, temp);
56+
}
57+
58+
static sort2(List<int> arr, int c) {
59+
if (c <= 0) throw new Exception("c must be > 0");
60+
61+
int maxv = 1 >> 32, minv = 1 << 32;
62+
for (int e in arr) {
63+
maxv = [maxv, e].reduce(max);
64+
minv = [minv, e].reduce(min);
65+
}
66+
67+
int range = maxv - minv + 1; // arr 中的数据范围
68+
int B = (range / c).toInt() + (range % c > 0 ? 1 : 0); // 根据数据范围决定桶的个数
69+
70+
List buckets = List.filled(B, List, growable: true);
71+
// for(int i = 0; i < B; i ++)
72+
// buckets[i] = new LinkedList<>();
73+
for (int e in arr) {
74+
buckets[((e - minv) / range).toInt()].add(e);
75+
}
76+
for (int i = 0; i < B; i++) {
77+
buckets[i].sort();
78+
}
79+
int index = 0;
80+
for (int i = 0; i < B; i++) for (int e in buckets[i]) {
81+
arr[index++] = e;
82+
}
83+
}
84+
}
85+
86+
void main() {
87+
int n = 1000000;
88+
List? arr = ArrayGenerator.generateRandomArray(n, n);
89+
List? arr2 = List.from(arr);
90+
91+
//SortingHelper.sortTest("BucketSort", arr);
92+
//SortingHelper.sortTest("BucketSort2", arr2);
93+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class MSDSort {
2+
MSDSort() {}
3+
4+
static sort(List? arr) {
5+
int N = arr!.length;
6+
List? temp = List.filled(N, String, growable: true);
7+
_sortDetail(arr, 0, N - 1, 0, temp);
8+
}
9+
10+
static _sortDetail(List? arr, int left, int right, int r, List? temp) {
11+
if (left >= right) return;
12+
13+
int R = 256;
14+
List? cnt = List.filled(R + 1, 0, growable: true);
15+
List? index = List.filled(R + 2, 0, growable: true);
16+
17+
// O(n)
18+
for (int i = left; i <= right; i++) {
19+
cnt[r >= arr![i].length ? 0 : (arr[i][r].codeUnits[0] + 1)]++;
20+
}
21+
// O(R)
22+
for (int i = 0; i < R + 1; i++) {
23+
index[i + 1] = index[i] + cnt[i];
24+
}
25+
// O(n)
26+
for (int i = left; i <= right; i++) {
27+
temp![index[r >= arr![i].length ? 0 : (arr[i][r].codeUnits[0] + 1)] +
28+
left] = arr[i];
29+
index[r >= arr[i].length ? 0 : (arr[i][r].codeUnits[0] + 1)]++;
30+
}
31+
32+
// O(n)
33+
for (int i = left; i <= right; i++) {
34+
arr![i] = temp![i];
35+
}
36+
37+
for (int i = 0; i < R; i++) {
38+
_sortDetail(arr, (left + index[i]).toInt(),
39+
(left + index[i + 1] - 1).toInt(), r + 1, temp);
40+
}
41+
}
42+
}
43+
44+
void main() {
45+
List? arr = ["BCA", "CBAA", "AC", "BADFE", "ABC", "CBA"];
46+
MSDSort.sort(arr);
47+
for (String s in arr) {
48+
print(s);
49+
}
50+
}

0 commit comments

Comments
 (0)