Sort the following numbers using the counting Sort method: 3, 7, 8, 4, 6, 9, 8, 10, 2, 1, 5.
Step 1:-
Counting sort :-
For simplicity, consider the data in the range 0 to 10
Input data: 3,7,8,4,6,9,8,10,2,1,5
1)count array is
Index: 0 1 2 3 4 5 6 7 8 9 10
Count: 0 0 0 0 0 0 0 0 0 0 0
2) Take a count array to store the count of each unique object.
Index: 0 1 2 3 4 5 6 7 8 9 10
Count: 0 1 1 1 1 1 1 1 1 1 1
3) Modify the count array such that each element at each index
stores the sum of previous counts.
Index: 0 1 2 3 4 5 6 7 8 9 10
Count: 0 1 2 3 4 5 6 7 8 9 10
The modified count array indicates the position of each object in
the output sequence.
4) Output each object from the input sequence followed by
decreasing its count by 1.
Process the input data and Decrease count by 1 to place
next data 1 at an index 1 smaller than this index and so on.
Step 2:-
Program Approach:-
1.Declare the array of data type integer and Initialize this array
2.Declare the user-defined function whose name is void countSort(int array[], int size) and void printArray(int array[], int size) . The data type of both the function is void and take two parameters first is an array and second is the size of the array.
A user-defined function is defined as a set of statements that can perform a specific task.
The array is defined as a collection of similar type of data.
For Example:-int A[2]
A is the name of the array and its data type is an integer.
The first element of the array is A[0] and the second element is A[1]
Step by step
Solved in 4 steps with 1 images