## Counting SortIt is a linear time sorting algorithm which works faster by not making a comparison. It assumes that the number to be sorted is in range 1 to k where k is small. Basic idea is to determine the "rank" of each number in the final sorted array. ## Counting Sort uses three arrays:- A [1, n] holds initial input.
- B [1, n] holds sorted output.
- C [1, k] is the array of integer. C [x] is the rank of x in A where x ∈ [1, k]
Firstly C [x] to be a number of elements of A [j] that is equal to x - Initialize C to zero
- For each j from 1 to n increment C [A[j]] by 1
We set B[C [x]] =A[j] If there are duplicates, we decrement C [i] after copying.
For loops of steps Step For loop of step Because element might not be distinct, so we decrement C[A[j] each time we place a value A [j] into array B decrement C[A[j] causes the next input element with a value equal to A [j], to go to the position immediately before A [j] in the output array. ## Analysis of Running Time:- For a loop of step 1 to 2 take θ(k) times
- For a loop of step 3 to 4 take θ(n) times
- For a loop of step 6 to 7 take θ(k) times
- For a loop of step 9 to 11 take θ(n) times
Overall time is
- Counting Sort has the important property that it is stable: numbers with the same value appears in the output array in the same order as they do in the input array.
- Counting Sort is used as a subroutine in Radix Sort.
## UPDATED C is:
## Note: here the item of 'A' one by one get scanned and will become a position in 'C' and how many times the item get accessed will be mentioned in an item in 'C' Array and it gets updated or counter increased by 1 if any item gets accessed again.Now, the for loop i= 2 to 7 will be executed having statement: By applying these conditions we will get C updated as i stated from 2 up to 7 C [2] = C [2] + C [1] C [3] = C [3] + C [2] C [2] = 2 + 2 C [3] = 2 + 4 ## Thus the Updated C is:
Now, we will find the new array B Now two Conditions will apply: - B[C[A[j] ← A [j]
- C[A[j] ← C[A[j]-1
We decrease counter one by one by '1' ## Step 1B [C [A [11]]] = A [11] C [A [11] = C [A [11]-1 B [C [3] = 3 C [3] = C [3] -1
## Step 2B [C [A [10]]] = A [10] C [A [10]] = C [A [10]]-1 B [C [4]] =4 C [4] = C [4] -1
## Step 3B [C [A [9]] = A [9] C [A [9] = C [A [9]]-1 B [C [2]] = A [2] C [2] = C [2]-1
## Step 4B [C [A [8]]] = A [8] C [A [8]] =C [A [8]] -1 B [C [7]] =7 C [A [8]] = C [7]-1
## Step 5B [C [A [7]]] = A [7] C [A [7]] = C [A [7]] - 1 B [C [5]] = 5 C [5] = C [5] - 1
## Step 6B [C [A [6]]] = A [6] C [A [6]] = C [A [6]] - 1 B [C [4]] = 4 C [4] = C [4] - 1
## Step 7B [C [A [5]]] = A [5] C [A [5] = C [A [5]] -1 B [C [2] =2 C [2] = C [2] - 1
## Step 8B [C [A [4]]] = A [4] C [A [4]] = C [A [4]] - 1 B [C [1] = 1 C [1] = C [1] - 1
## Step 9B [C [A [3]]] = A [3] C [A [3]] = C [A [3]] - 1 B [C [3] = 3 C [3] = C [3] - 1
## Step 10B [C [A [2]]] = A [2] C [A [2]] = C [A [2]] - 1 B [C [1]] = 1 C [1] = C [1] - 1
## Step 11B [C [A [1]]] = A [1] C [A [1]] = C [A [1]] - 1 B [C [7]] = 7 C [7] = C [7] - 1
Next TopicBucket Sort |