|
| 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 | +} |
0 commit comments