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

Commit

Permalink
Implement counting sort in Python, JavaScript, Java, and C++. Countin…
Browse files Browse the repository at this point in the history
…g 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)
  • Loading branch information
yohannesME authored Jun 28, 2023
1 parent 0f107f6 commit 560565a
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 27 deletions.
64 changes: 64 additions & 0 deletions C++/Algorithms/SortingAlgorithms/countingSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

/**
* Implements the counting sort algorithm on an input array of integers.
*
* @param array - An array of integers to be sorted.
*
* @return An array of integers sorted in non-descending order.
*
* Time Complexity:
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
*
* Space Complexity:
* - O(k), where k is the range of values in the input array.
*/
vector<int> countingSort(vector<int>& array) {
// determine the maximum value in the input array
int max = *max_element(array.begin(), array.end());

// initialize a count array of size max + 1 with all elements set to 0
vector<int> count(max + 1, 0);

// iterate over the input array and increment the count of each element's value
for (int i = 0; i < array.size(); i++) {
count[array[i]]++;
}

// modify the count array to reflect the number of elements <= each element's value
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}

// create a result array of size array.size() with all elements set to 0
vector<int> result(array.size(), 0);

// iterate over the input array backwards and place each element in its sorted position in the result array
for (int i = array.size() - 1; i >= 0; i--) {
result[count[array[i]] - 1] = array[i];
count[array[i]]--;
}

// return the sorted result array
return result;
}

int main() {
// define an example array to sort
vector<int> array = {3, 1, 7, 4, 9, 2, 6, 5, 8, 0};

// sort the array using countingSort
vector<int> sortedArray = countingSort(array);

// print the sorted array
for (int i = 0; i < sortedArray.size(); i++) {
cout << sortedArray[i] << " ";
}
cout << endl; // expected output: 0 1 2 3 4 5 6 7 8 9

return 0;
}
53 changes: 53 additions & 0 deletions Java/Algorithm/SortingAlgorithm/CountingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Implements the counting sort algorithm on an input array of integers.
*
* @param array - An array of integers to be sorted.
*
* @return An array of integers sorted in non-descending order.
*
* Time Complexity:
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
*
* Space Complexity:
* - O(k), where k is the range of values in the input array.
*/
public static int[] countingSort(int[] array) {
// determine the maximum value in the input array
int max = Arrays.stream(array).max().orElse(0);

// initialize a count array of size max + 1 with all elements set to 0
int[] count = new int[max + 1];

// iterate over the input array and increment the count of each element's value
for (int i = 0; i < array.length; i++) {
count[array[i]]++;
}

// modify the count array to reflect the number of elements <= each element's value
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}

// create a result array of size array.length with all elements set to 0
int[] result = new int[array.length];

// iterate over the input array backwards and place each element in its sorted position in the result array
for (int i = array.length - 1; i >= 0; i--) {
result[count[array[i]] - 1] = array[i];
count[array[i]]--;
}

// return the sorted result array
return result;
}

public static void main(String[] args) {
// define an example array to sort
int[] array = {3, 1, 7, 4, 9, 2, 6, 5, 8, 0};

// sort the array using countingSort
int[] sortedArray = countingSort(array);

// print the sorted array
System.out.println(Arrays.toString(sortedArray)); // expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
78 changes: 51 additions & 27 deletions Javascript/Algorithms/counting-sort.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
const countingSort = (inputArr, n = inputArr.length) => {
let k = Math.max(...inputArr);
let t;
const temp = new Array(k + 1).fill(0);

for(let i = 0; i < n; i++){
t = inputArr[i];
temp[t]++;
}

for(let i = 1; i <= k; i++){

temp[i] = temp[i] + temp[i - 1];
}

const outputArr = new Array(n).fill(0);

for(let i = n - 1; i >= 0; i--) {

t = inputArr[i];
outputArr[temp[t] - 1] = t;

temp[t] = temp[t] - 1;
}

return outputArr;
}
/**
* Implements the counting sort algorithm on an input array of integers.
*
* @param {number[]} array - A list of integers to be sorted.
*
* @returns {number[]} A list of integers sorted in non-descending order.
*
* Time Complexity:
* - O(n + k), where n is the length of the input array and k is the range of values in the input array.
*
* Space Complexity:
* - O(k), where k is the range of values in the input array.
*/
function countingSort(array) {
// determine the maximum value in the input array
let n = Math.max(...array) + 1;

// initialize a count array of size n with all elements set to 0
let count = new Array(n).fill(0);

// iterate over the input array and increment the count of each element's value
for (let i = 0; i < array.length; i++) {
count[array[i]]++;
}

// modify the count array to reflect the number of elements <= each element's value
for (let i = 1; i < n; i++) {
count[i] += count[i - 1];
}

// create a result array of size array.length with all elements set to 0
let result = new Array(array.length).fill(0);

// iterate over the input array backwards and place each element in its sorted position in the result array
for (let i = array.length - 1; i >= 0; i--) {
result[count[array[i]] - 1] = array[i];
count[array[i]]--;
}

// return the sorted result array
return result;
}

// define an example array to sort
let array = [3, 1, 7, 4, 9, 2, 6, 5, 8, 0];

// sort the array using countingSort
let sortedArray = countingSort(array);

// print the sorted array
console.log(sortedArray); // expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
54 changes: 54 additions & 0 deletions Python/Algorithms/SortingAlgorithms/countingSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def countingSort(array):
"""
Implements the counting sort algorithm on an input array of integers.
Args:
- array: A list of integers to be sorted.
Returns:
- A list of integers sorted in non-descending order.
Time Complexity:
- O(n + k), where n is the length of the input array and k is the range of values in the input array.
Space Complexity:
- O(k), where k is the range of values in the input array.
"""

# determine the maximum value in the input array
n = max(array) + 1

# initialize a count array of size n with all elements set to 0
count = [0] * n

# iterate over the input array and increment the count of each element's value
for i in array:
count[i] += 1

# modify the count array to reflect the number of elements <= each element's value
for i in range(1, n):
count[i] += count[i - 1]

# create a result array of size len(array) with all elements set to 0
result = [0] * len(array)

# iterate over the input array backwards and place each element in its sorted position in the result array
for i in range(len(array) - 1, -1, -1):
result[count[array[i]] - 1] = array[i]
count[array[i]] -= 1

# return the sorted result array
return result

def main():
# define an example array to sort
array = [3, 1, 7, 4, 9, 2, 6, 5, 8, 0]

# sort the array using countingSort
sorted_array = countingSort(array)

# print the sorted array
print(sorted_array) # expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

if __name__ == "__main__":
main()

0 comments on commit 560565a

Please sign in to comment.