diff --git a/C++/Algorithms/SortingAlgorithms/countingSort.cpp b/C++/Algorithms/SortingAlgorithms/countingSort.cpp new file mode 100644 index 00000000..de13b7be --- /dev/null +++ b/C++/Algorithms/SortingAlgorithms/countingSort.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +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 countingSort(vector& 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 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 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 array = {3, 1, 7, 4, 9, 2, 6, 5, 8, 0}; + + // sort the array using countingSort + vector 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; +} diff --git a/Java/Algorithm/SortingAlgorithm/CountingSort.java b/Java/Algorithm/SortingAlgorithm/CountingSort.java new file mode 100644 index 00000000..48a05275 --- /dev/null +++ b/Java/Algorithm/SortingAlgorithm/CountingSort.java @@ -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] +} diff --git a/Javascript/Algorithms/counting-sort.js b/Javascript/Algorithms/counting-sort.js index 9fc63d89..ca52eae3 100644 --- a/Javascript/Algorithms/counting-sort.js +++ b/Javascript/Algorithms/counting-sort.js @@ -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; -} \ No newline at end of file +/** + * 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] diff --git a/Python/Algorithms/SortingAlgorithms/countingSort.py b/Python/Algorithms/SortingAlgorithms/countingSort.py new file mode 100644 index 00000000..0d044035 --- /dev/null +++ b/Python/Algorithms/SortingAlgorithms/countingSort.py @@ -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()