C Exercises: Return the counting sort on an array
59. Counting Sort of an Array
Write a program in C to return the counting sort on an array.
Expected Output :
The given array is : 4 14 8 0 2 5 2 1 0 17 9 0 5
After sorting the elements in the array are: 0 0 0 1 2 2 4 5 5 8 9 14 17
To perform counting sort on an array, the program first determines the range of the input values. It then creates a count array to store the frequency of each element. By accumulating the counts and then using them to place elements in their correct positions, the program efficiently sorts the array. This algorithm is particularly effective for arrays with a limited range of integer values.
Sample Solution:
C Code:
Output:
The given array is : 4 14 8 0 2 5 2 1 0 17 9 0 5 After sorting the elements in the array are: 0 0 0 1 2 2 4 5 5 8 9 14 17
Flowchart:/p>
For more Practice: Solve these Related Problems:
- Write a C program to implement counting sort on an array and then verify the output by comparing with qsort().
- Write a C program to perform counting sort on an array of non-negative integers and display the frequency of each element.
- Write a C program to implement counting sort and then modify it to sort characters in a string.
- Write a C program to apply counting sort on an array and then reverse the sorted array.
C Programming Code Editor:
Previous: Write a program in C to move all zeroes to the end of a given array.
Next: Write a program in C to find the row with maximum number of 1s.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.