Question
Write a C++ function that implements the counting sort, assuming an array of integers that are in the range 0 to 100, inclusive. The counting
Write a C++ function that implements the counting sort, assuming an array of integers that are in the range 0 to 100, inclusive. The counting sort can sort an array of integers that are known to be in a given range of integers by using a second array count to count the number of occurrences of each integer in the array. As an example, let's say we have an array called scores containing the following ten integers in the range between 0 and 4, inclusive:
scores[] : 1 4 0 4 0 0 3 4 0 3
In this case, the count array would have five elements, one for each possible value 0 through 4. We initialize count to all zeros. We then make one pass through scores -- each time we see a given value x in scores, we increment the value in count[x]. When we are done, count[x] will tell us how many instances of the value x we have in scores. In this example, count would be:
count[] : 4 1 0 2 3
because there are 4 zeros, 1 one, 0 twos, 2 threes, 3 fours in scores.
Once you have this array count, it is straightforward to determine the sorted scores array:
scores[] : 0 0 0 0 1 3 3 4 4 4 and store it back into the original array.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started