-
Notifications
You must be signed in to change notification settings - Fork 1k
/
BucketSort.dart
88 lines (79 loc) · 1.81 KB
/
BucketSort.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
Algorithm to sort arrays when numbers are distributed over a range. In this
algorithm we divde the whole array into buckets and then sort the smaller buckets
to get the sorted array.
*/
import 'dart:io';
import 'dart:math';
// function that implements bucket sort
List bucketSort(List arr, int bucketNum) {
// get max value in array to find upper limit of bucket
num maxVal = arr[0];
for (int i = 0; i < arr.length; i++) {
maxVal = max(maxVal, arr[i]);
}
maxVal++;
// making bucket list and adding buckets to it
List bucketList = new List.empty(growable: true);
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new List.empty(growable: true));
}
// dividing numbers in different buckets
for (int i = 0; i < arr.length; i++) {
int bucketIndex = ((arr[i] * bucketNum) ~/ maxVal) as int;
bucketList[bucketIndex].add(arr[i]);
}
// sorting each bucket one-by-one
for (int i = 0; i < bucketNum; i++) {
bucketList[i].sort();
}
// joining buckets
int pos = 0;
for (int i = 0; i < bucketNum; i++) {
for (int j = 0; j < bucketList[i].length; j++) {
arr[pos] = bucketList[i][j];
pos += 1;
}
}
return arr;
}
// main function, entry point of the program
void main() {
print("Enter the size of list:");
int size = int.parse(stdin.readLineSync()!);
List arr = [];
print("Enter the numbers:");
for (int i = 0; i < size; i++) {
arr.add(int.parse(stdin.readLineSync()!));
}
print("Enter number of buckets:");
int buckets = int.parse(stdin.readLineSync()!);
// sorting
arr = bucketSort(arr, buckets);
print("Sorted list:");
for (int i = 0; i < size; i++) {
print(arr[i]);
}
}
/**
Enter the size of list:
6
Enter the numbers:
5
0
2
1
4
3
Enter number of buckets:
5
Sorted list:
0
1
2
3
4
5
Time Complexity: O(n+k)
Space Complexity: O(n+k)
*/