- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Implementing counting sort in JavaScript

In the given task, our aim is to create a counting sorting technique and implement this problem with the help of Javascript functionalities.

## What is the Counting Sort?

The counting sort is a process for sorting a data set of objects as per the keys. It works by counting the number of items that have different key values and finding the position of each object in the sorted output. The idea of counting sort is to place the items directly into its correct place in the output array.

In the beginning of the algorithm we need to find the maximum and minimum values in the input array. And with the help of these values we need to create a counting array with the equal length of values in the input array. The counting array will store the number of occurrences of every distinct value in the array.

The algorithm uses the prefix sum of the count array. So the algorithm iterates through the input array in the reverse order and places every item in the output array in the correct position based on its count and decrements the count accordingly.

## The logic for the given problem

In the given problem statement we have to do the task of sorting the items of the given array with the help of counting sort mentioned above. So first we will take an input array and return the output as a new array as sorted with the same length of the input array.

As per the counting sort technique, we need to find out the max and min values in the input array. Then we will iterate the array elements and count the number of occurrences for every item. Then calculate the prefix sum of the count array. Then again iterate through the array and place every item at its correct position.

## Algorithm

**Step 1** − Check the condition that the given array has elements or not.

**Step 2** − Declare the arrays for count and output. The count array will be utilized to count the number of times each value has appeared. The sorted array will be used to hold an output array.

**Step 3** − Use a for loop to traverse through the given array, and count the number of repetition for each value in it.

**Step 4** − Compute the array's prefix sum as per the counting sort algorithm.

**Step 5** − Traverse through the given array in opposite order, placing each item in the correct place.

**Step 6** − Finally, we must display the output as a sorted array.

## Code for the algorithm

function countingSort(arr) { if (arr.length === 0) { return arr; } // Define the range of values in the input array let min = arr[0]; let max = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; } if (arr[i] > max) { max = arr[i]; } } // initialize the required arrays const count = new Array(max - min + 1).fill(0); const output = new Array(arr.length); // count the number of occurrences for (let i = 0; i < arr.length; i++) { count[arr[i] - min]++; } // calculate the prefix sum of the count array for (let i = 1; i < count.length; i++) { count[i] += count[i - 1]; } // the output array in sorted order for (let i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } return output; } const arr = [30, 10, 40, 10, 50, 90, 20, 60, 50, 30, 50]; console.log(countingSort(arr));

## Complexity

The time complexity for the counting sort function is O(n+m), here n is the length of the input array and m is the range of values in the input array. As seen in the code we have iterated the array once to count the repeating of every value. And then we have iterated the count array one time to calculate the prefix sum. So overall the time taken to execute the code is O(n+m). And the space complexity is also O(n+m). As we are creating two arrays one is count and another is output array.

## Conclusion

The code we've seen to sort the element in the array utilizes fundamental JavaScript operations to accomplish the task. The algorithm uses counting sort as per the given problem statement.