Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 560565a

Browse files
authored
Implement counting sort in Python, JavaScript, Java, and C++. Counting sort is a linear time sorting algorithm that works well when the range of values in the input array is small. These implementations all follow the same algorithm, but use different syntax and data structures specific to each language. In each implementation, there is a function that takes an array of integers as input and returns a sorted array of integers. Additionally, there is an example array defined and sorted using the counting sort function, with the sorted result printed to the console or standard output. Added a docstring explaining the algorithm's time and space complexities. This commit completes the implementation of counting sort in four different programming languages. (#935)
1 parent 0f107f6 commit 560565a

File tree

4 files changed

+222
-27
lines changed

4 files changed

+222
-27
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
/**
8+
* Implements the counting sort algorithm on an input array of integers.
9+
*
10+
* @param array - An array of integers to be sorted.
11+
*
12+
* @return An array of integers sorted in non-descending order.
13+
*
14+
* Time Complexity:
15+
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
16+
*
17+
* Space Complexity:
18+
* - O(k), where k is the range of values in the input array.
19+
*/
20+
vector<int> countingSort(vector<int>& array) {
21+
// determine the maximum value in the input array
22+
int max = *max_element(array.begin(), array.end());
23+
24+
// initialize a count array of size max + 1 with all elements set to 0
25+
vector<int> count(max + 1, 0);
26+
27+
// iterate over the input array and increment the count of each element's value
28+
for (int i = 0; i < array.size(); i++) {
29+
count[array[i]]++;
30+
}
31+
32+
// modify the count array to reflect the number of elements <= each element's value
33+
for (int i = 1; i <= max; i++) {
34+
count[i] += count[i - 1];
35+
}
36+
37+
// create a result array of size array.size() with all elements set to 0
38+
vector<int> result(array.size(), 0);
39+
40+
// iterate over the input array backwards and place each element in its sorted position in the result array
41+
for (int i = array.size() - 1; i >= 0; i--) {
42+
result[count[array[i]] - 1] = array[i];
43+
count[array[i]]--;
44+
}
45+
46+
// return the sorted result array
47+
return result;
48+
}
49+
50+
int main() {
51+
// define an example array to sort
52+
vector<int> array = {3, 1, 7, 4, 9, 2, 6, 5, 8, 0};
53+
54+
// sort the array using countingSort
55+
vector<int> sortedArray = countingSort(array);
56+
57+
// print the sorted array
58+
for (int i = 0; i < sortedArray.size(); i++) {
59+
cout << sortedArray[i] << " ";
60+
}
61+
cout << endl; // expected output: 0 1 2 3 4 5 6 7 8 9
62+
63+
return 0;
64+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Implements the counting sort algorithm on an input array of integers.
3+
*
4+
* @param array - An array of integers to be sorted.
5+
*
6+
* @return An array of integers sorted in non-descending order.
7+
*
8+
* Time Complexity:
9+
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
10+
*
11+
* Space Complexity:
12+
* - O(k), where k is the range of values in the input array.
13+
*/
14+
public static int[] countingSort(int[] array) {
15+
// determine the maximum value in the input array
16+
int max = Arrays.stream(array).max().orElse(0);
17+
18+
// initialize a count array of size max + 1 with all elements set to 0
19+
int[] count = new int[max + 1];
20+
21+
// iterate over the input array and increment the count of each element's value
22+
for (int i = 0; i < array.length; i++) {
23+
count[array[i]]++;
24+
}
25+
26+
// modify the count array to reflect the number of elements <= each element's value
27+
for (int i = 1; i <= max; i++) {
28+
count[i] += count[i - 1];
29+
}
30+
31+
// create a result array of size array.length with all elements set to 0
32+
int[] result = new int[array.length];
33+
34+
// iterate over the input array backwards and place each element in its sorted position in the result array
35+
for (int i = array.length - 1; i >= 0; i--) {
36+
result[count[array[i]] - 1] = array[i];
37+
count[array[i]]--;
38+
}
39+
40+
// return the sorted result array
41+
return result;
42+
}
43+
44+
public static void main(String[] args) {
45+
// define an example array to sort
46+
int[] array = {3, 1, 7, 4, 9, 2, 6, 5, 8, 0};
47+
48+
// sort the array using countingSort
49+
int[] sortedArray = countingSort(array);
50+
51+
// print the sorted array
52+
System.out.println(Arrays.toString(sortedArray)); // expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
53+
}
Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1-
const countingSort = (inputArr, n = inputArr.length) => {
2-
let k = Math.max(...inputArr);
3-
let t;
4-
const temp = new Array(k + 1).fill(0);
5-
6-
for(let i = 0; i < n; i++){
7-
t = inputArr[i];
8-
temp[t]++;
9-
}
10-
11-
for(let i = 1; i <= k; i++){
12-
13-
temp[i] = temp[i] + temp[i - 1];
14-
}
15-
16-
const outputArr = new Array(n).fill(0);
17-
18-
for(let i = n - 1; i >= 0; i--) {
19-
20-
t = inputArr[i];
21-
outputArr[temp[t] - 1] = t;
22-
23-
temp[t] = temp[t] - 1;
24-
}
25-
26-
return outputArr;
27-
}
1+
/**
2+
* Implements the counting sort algorithm on an input array of integers.
3+
*
4+
* @param {number[]} array - A list of integers to be sorted.
5+
*
6+
* @returns {number[]} A list of integers sorted in non-descending order.
7+
*
8+
* Time Complexity:
9+
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
10+
*
11+
* Space Complexity:
12+
* - O(k), where k is the range of values in the input array.
13+
*/
14+
function countingSort(array) {
15+
// determine the maximum value in the input array
16+
let n = Math.max(...array) + 1;
17+
18+
// initialize a count array of size n with all elements set to 0
19+
let count = new Array(n).fill(0);
20+
21+
// iterate over the input array and increment the count of each element's value
22+
for (let i = 0; i < array.length; i++) {
23+
count[array[i]]++;
24+
}
25+
26+
// modify the count array to reflect the number of elements <= each element's value
27+
for (let i = 1; i < n; i++) {
28+
count[i] += count[i - 1];
29+
}
30+
31+
// create a result array of size array.length with all elements set to 0
32+
let result = new Array(array.length).fill(0);
33+
34+
// iterate over the input array backwards and place each element in its sorted position in the result array
35+
for (let i = array.length - 1; i >= 0; i--) {
36+
result[count[array[i]] - 1] = array[i];
37+
count[array[i]]--;
38+
}
39+
40+
// return the sorted result array
41+
return result;
42+
}
43+
44+
// define an example array to sort
45+
let array = [3, 1, 7, 4, 9, 2, 6, 5, 8, 0];
46+
47+
// sort the array using countingSort
48+
let sortedArray = countingSort(array);
49+
50+
// print the sorted array
51+
console.log(sortedArray); // expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
def countingSort(array):
2+
"""
3+
Implements the counting sort algorithm on an input array of integers.
4+
5+
Args:
6+
- array: A list of integers to be sorted.
7+
8+
Returns:
9+
- A list of integers sorted in non-descending order.
10+
11+
Time Complexity:
12+
- O(n + k), where n is the length of the input array and k is the range of values in the input array.
13+
14+
Space Complexity:
15+
- O(k), where k is the range of values in the input array.
16+
"""
17+
18+
# determine the maximum value in the input array
19+
n = max(array) + 1
20+
21+
# initialize a count array of size n with all elements set to 0
22+
count = [0] * n
23+
24+
# iterate over the input array and increment the count of each element's value
25+
for i in array:
26+
count[i] += 1
27+
28+
# modify the count array to reflect the number of elements <= each element's value
29+
for i in range(1, n):
30+
count[i] += count[i - 1]
31+
32+
# create a result array of size len(array) with all elements set to 0
33+
result = [0] * len(array)
34+
35+
# iterate over the input array backwards and place each element in its sorted position in the result array
36+
for i in range(len(array) - 1, -1, -1):
37+
result[count[array[i]] - 1] = array[i]
38+
count[array[i]] -= 1
39+
40+
# return the sorted result array
41+
return result
42+
43+
def main():
44+
# define an example array to sort
45+
array = [3, 1, 7, 4, 9, 2, 6, 5, 8, 0]
46+
47+
# sort the array using countingSort
48+
sorted_array = countingSort(array)
49+
50+
# print the sorted array
51+
print(sorted_array) # expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)