Skip to content

Commit b23ad09

Browse files
author
tianqing.liang
committed
排序工具类,希尔排序
1 parent 87682a6 commit b23ad09

File tree

14 files changed

+530
-5
lines changed

14 files changed

+530
-5
lines changed

02-Selection-Sort/Selection-sort.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
22
* 选择排序
3+
*
4+
* 选择最小的
35
*/
46
void main(){
57

@@ -15,7 +17,7 @@ void selectionSort(List arr){
1517
for(int i = 0; i < arr.length; i ++){
1618
int minIndex = i;
1719
for(int j = i; j < arr.length; j ++){
18-
if(arr[j].compareTo(arr[minIndex]) > 0)
20+
if(arr[j].compareTo(arr[minIndex]) < 0)
1921
minIndex = j;
2022
}
2123
swap(arr, i, minIndex);
@@ -32,12 +34,12 @@ void swap(arr, int i, int j) {
3234
class Student {
3335

3436
String? name;
35-
int? score;
37+
num? score;
3638

3739
Student(this.name,this.score);
3840

3941
@override
40-
int compareTo(other) {
41-
return other.score - this.score;
42+
num? compareTo(other) {
43+
return this.score! - other.score;
4244
}
4345
}

02-Selection-Sort/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

03-Insertion-Sort/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

07-MergeSort-More/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

07-MergeSort/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

08-QuickSort-More/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

08-QuickSort/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

12-Heap/SortingHelper.dart

Lines changed: 46 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)