From 09873885b94ec35397eb900a45e012263f122373 Mon Sep 17 00:00:00 2001 From: bryanoli Date: Thu, 19 Oct 2023 14:54:38 -0700 Subject: [PATCH 1/2] Added bucket sort and the test --- pubspec.yaml | 2 +- sort/bucket_sort.dart | 37 +++++++++++++++++++++++++ tests/sort/bucket_sort_test.dart | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 sort/bucket_sort.dart create mode 100644 tests/sort/bucket_sort_test.dart 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..44fa3e12 --- /dev/null +++ b/sort/bucket_sort.dart @@ -0,0 +1,37 @@ +import 'dart:math'; + +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]; + } + } + } +} \ No newline at end of file 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 From 29b3ae4ea4b0f690bef131c5e2955d76451fb97e Mon Sep 17 00:00:00 2001 From: bryanoli Date: Thu, 19 Oct 2023 15:08:14 -0700 Subject: [PATCH 2/2] Added URL for wikipedia and description --- sort/bucket_sort.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sort/bucket_sort.dart b/sort/bucket_sort.dart index 44fa3e12..4d265340 100644 --- a/sort/bucket_sort.dart +++ b/sort/bucket_sort.dart @@ -1,4 +1,13 @@ 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; @@ -34,4 +43,4 @@ void bucketSort(List arr) { } } } -} \ No newline at end of file +}