C Program for Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.
Step 1: Start
Step 2 : Find Largest Data From Array.
Step 3 : Set All Array Element Value with 0.
Step 4 : Increase Count Data Of Each Number Which Found In The Array .
Step 5 : Find Cumulative Frequency
Step 6: Store The Number In The Output Array.
Step 7 : Print The Output Array
Step 8 : Stop.
COUNTINGSORT(A) FOR I = 0 TO K DO C[I] = 0 FOR J = 0 TO N DO C[A[J]] = C[A[J]] + 1 FOR I = 1 TO K DO C[I] = C[I] + C[I-1] FOR J = N-1 TO 0 DO B[ C[A[J]]-1 ] = A[J] C[A[J]] = C[A[J]] - 1 END FUNCTION
Sorted character array is eeeefggkkorssn
Time Complexity Analysis
When Data Is Skewed And Range Is Large Then Time Complexity Is O(N+K)
When All Elements Are Same Time Complexity Is O(N+K)
Average Time Complexity Is O(N+K)
Space Complexity :
SPACE COMPLEXITY FOR THIS SORT IS O(K).
- Count Sort Process Fast in less time .
- It Is A useful for Small size of Data Set .
- It Is Not useful Large Data.
- It Is Not useful for sorting string values .
- Not An In-Place Sorting Algorithm
- It Requires Extra Additional Space for sorting operation. like O(K)
Please refer complete article on Counting Sort for more details!
Please Login to comment...