-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathpigeonhole_sort.dart
More file actions
63 lines (50 loc) · 1.37 KB
/
pigeonhole_sort.dart
File metadata and controls
63 lines (50 loc) · 1.37 KB
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
import 'package:test/test.dart';
void pigeonholeSort(List arr) {
//The length of the list
int n = arr.length;
//checking the size
if (n <= 0) {
return;
}
//Find minimum and maximum values in arr
int min = arr[0];
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}
int range = max - min;
range++;
List<int> phole = List.generate(range, (i) => 0);
//Populate the pigeonholes.
for (int i = 0; i < n; i++) {
phole[arr[i] - min];
phole[arr[i] - min] = phole[arr[i] - min] + 1;
}
//Put the elements back into the array in order
int index = 0;
for (int j = 0; j < range; j++) while (phole[j]-- > 0) arr[index++] = j + min;
}
void main() {
test("Sort empty list returns empty list", () {
List list = [];
pigeonholeSort(list);
expect(list, isEmpty);
});
test("Already sorted list remain sorted", () {
List list = [1, 2, 3, 4, 5];
pigeonholeSort(list);
expect(list, equals([1, 2, 3, 4, 5]));
});
test("Sort", () {
List list = [87, 48, 5, 7, 135, 85];
pigeonholeSort(list);
expect(list, equals([5, 7, 48, 85, 87, 135]));
});
test("Sorted list size doesnt change", () {
List list = [1, 1, 4, 1, -12, -12, 77];
pigeonholeSort(list);
expect(list.length, equals(7));
expect(list, [-12, -12, 1, 1, 1, 4, 77]);
});
}