|
| 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