Hi I have found this question in the one site which was about counting sort algorithm and this question was an exercise ! I solved this and I want to know that is this correct?
Exercise: Use The idea of Counting Sort algorithm and provide an algorithm with n integer in the range 0 to k, in time O (1) replied that how many of these n elements are in the interval [a, b] ? Of course, your algorithm should process n elements that can help you take the numbers between a and b (0 ≤ a, b ≤ k) . This basic process should be in time Θ (n + k) .
this is my algorithm:
Algorithm Range(A,k,a,b)
{Consider C is the output array of PartOfCountingSort algorithm}
C<--PartOfCountingSort(A,k)
//finding the number of elements between a and b
numberOfElements<-- C[b]-C[a]+1
return numberOfElements
//end of the Range algorithm
------------------------------------
PartOfCountingSort(A,k) // Theta(n+k)
OutPut: array C is the output of this algorithm
for i<-- 1 to k
C[i]<-- 0
for j<-- 1 to A. length
C[A[j]]<--C[A[j]]+1
for i<--2 to k
C[i]<--C[i]+C[i-1]