Skip to content

Bucket Sort Algorithm: Hacktober2023 #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
46 changes: 46 additions & 0 deletions sort/bucket_sort.dart
Original file line number Diff line number Diff line change
@@ -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<double> arr) {
int n = arr.length;

if (n <= 1) return;

double maxVal = arr.reduce(max);
double minVal = arr.reduce(min);

List<List<double>> 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];
}
}
}
}
46 changes: 46 additions & 0 deletions tests/sort/bucket_sort_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:test/test.dart';
import '../../sort/bucket_sort.dart';

void main() {
test('Test case 1', () {
List<double> 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<double> 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<double> 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<double> arr = [];
bucketSort(arr);
expect(arr, orderedEquals([]));
});

test('Test case 5 (Already Sorted)', () {
List<double> 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<double> 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<double> 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]));
});
}