diff --git a/pubspec.yaml b/pubspec.yaml index 0ce0039e..ddc6d722 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,4 +10,4 @@ dev_dependencies: stack: ^0.2.1 environment: - sdk: ">=2.10.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" diff --git a/sort/bucket_sort.dart b/sort/bucket_sort.dart new file mode 100644 index 00000000..4d265340 --- /dev/null +++ b/sort/bucket_sort.dart @@ -0,0 +1,46 @@ +import 'dart:math'; +///This algorithm uses the bucket sort algorithm to sort a list of doubles. +///The algorithm is as follows: +///1. Find the maximum and minimum values in the list. +///2. Create a list of buckets, where each bucket is a list of doubles. +///3. For each value in the list, add it to the bucket corresponding to its index. +///4. Sort each bucket. +///5. Concatenate the buckets together. +///The algorithm is O(n) in the best case, and O(n^2) in the worst case. +///Source: https://en.wikipedia.org/wiki/Bucket_sort + +void bucketSort(List arr) { + int n = arr.length; + + if (n <= 1) return; + + double maxVal = arr.reduce(max); + double minVal = arr.reduce(min); + + List> buckets = List.generate(n, (index) => []); + + int calculateBucketIndex(double value, double minVal, double maxVal, int n) { + if (value.isNaN || value.isInfinite) { + return 0; + } + if (maxVal == minVal) { + return 0; + } + return ((value - minVal) / (maxVal - minVal) * (n - 1)).floor(); + } + + for (int i = 0; i < n; i++) { + int bucketIndex = calculateBucketIndex(arr[i], minVal, maxVal, n); + buckets[bucketIndex].add(arr[i]); + } + + int index = 0; + for (int i = 0; i < n; i++) { + if (buckets[i].isNotEmpty) { + buckets[i].sort(); + for (int j = 0; j < buckets[i].length; j++) { + arr[index++] = buckets[i][j]; + } + } + } +} diff --git a/tests/sort/bucket_sort_test.dart b/tests/sort/bucket_sort_test.dart new file mode 100644 index 00000000..376f6090 --- /dev/null +++ b/tests/sort/bucket_sort_test.dart @@ -0,0 +1,46 @@ +import 'package:test/test.dart'; +import '../../sort/bucket_sort.dart'; + +void main() { + test('Test case 1', () { + List arr = [0.42, 0.32, 0.33, 0.52, 0.37, 0.47, 0.51]; + bucketSort(arr); + expect(arr, orderedEquals([0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52])); + }); + + test('Test case 2', () { + List arr = [5.0, 4.0, 3.0, 2.0, 1.0]; + bucketSort(arr); + expect(arr, orderedEquals([1.0, 2.0, 3.0, 4.0, 5.0])); + }); + + test('Test case 3', () { + List arr = [1.1, 2.2, 3.3, 4.4, 5.5]; + bucketSort(arr); + expect(arr, orderedEquals([1.1, 2.2, 3.3, 4.4, 5.5])); + }); + + test('Test case 4 (Empty List)', () { + List arr = []; + bucketSort(arr); + expect(arr, orderedEquals([])); + }); + + test('Test case 5 (Already Sorted)', () { + List arr = [1.0, 2.0, 3.0, 4.0, 5.0]; + bucketSort(arr); + expect(arr, orderedEquals([1.0, 2.0, 3.0, 4.0, 5.0])); + }); + + test('Test case 6 (Reverse Sorted)', () { + List arr = [5.0, 4.0, 3.0, 2.0, 1.0]; + bucketSort(arr); + expect(arr, orderedEquals([1.0, 2.0, 3.0, 4.0, 5.0])); + }); + + test('Test case 7 (All Equal)', () { + List arr = [2.0, 2.0, 2.0, 2.0, 2.0]; + bucketSort(arr); + expect(arr, orderedEquals([2.0, 2.0, 2.0, 2.0, 2.0])); + }); +} \ No newline at end of file