-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Counting_Sort.dart
92 lines (78 loc) · 2.09 KB
/
Counting_Sort.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
89
90
91
92
/*
Counting Sort in Dart
Counting sort is a sorting algorithm where the number of occurances of each
element of the given array is stored in a frequency counter array which is
used to further map according to their index into the sorted array.
*/
import 'dart:io';
void countSort(List array, int size) {
// Setting limit for length of array
const int MAX = 1000;
// Initializing an array to store sorted elements temporarily
List temp = new List.filled(MAX, null, growable: false);
// Initializing array to store count of elements
List count = new List.filled(MAX, 0, growable: false);
// Finding the largest element in the array
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max) {
max = array[i];
}
}
// Storing the number of occurances of each element in array at the
// respective index in count array
for (int i = 0; i < size; i++) {
count[array[i]] += 1;
}
// Storing the total count of each element
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
// Placing the elements according to the index of count array in the
// temporary array
for (int i = size - 1; i >= 0; i--) {
temp[count[array[i]] - 1] = array[i];
count[array[i]] -= 1;
}
// Placing elements that are sorted back into the array
for (int i = 0; i < size; i++) {
array[i] = temp[i];
}
}
main() {
// Taking user input
var n, ele;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
List array = new List.filled(n, null, growable: false);
for (int i = 0; i < n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
array[i] = ele;
}
countSort(array, n);
print('The sorted array is: $array');
}
/*
Time Complexity: O(n+k) where n is the number of elements in input array and k
is the max element.
Auxiliary Space: O(n+k)
SAMPLE I/O:
Enter the number of Elements:
6
Enter Element 0:
78
Enter Element 1:
56
Enter Element 2:
90
Enter Element 3:
44
Enter Element 4:
23
Enter Element 5:
12
The sorted array is: [12, 23, 44, 56, 78, 90]
*/