Demonstrate the steps of the following algorithm by sorting the array: [1, 4, 1, 2, 9, 5, 2]. Show that the worst case running time for the algorithm is T(n) = Θ(n + M) and in the case where M = Θ(n), T(n) = Θ(n).
ALGORITHM: Sort
INPUT: data[] - array size n with max element M, where 0 < i < n and data[i] < M.
OUTPUT: sorted_data[] - sorted array
1. Initialize an array of M + 1 zeros called ele_count = new int[M+1].
2. For every element j in data[], update the array of counts at data[j] by 1. ele_count[data[j]] += 1.
3. Create a second array of size M + 1 with the first element being the same as the first created array, and the j-th element being the sum of the j-th element in the first array and the (j - 1)-st element in the second array. positions[0] = ele_count[0], positions[j] = ele_count[j] + positions[j-1].
4. Initialize an array of size n for sorted elements called sorted_array = new int[n].
5. For i = 0, 1, ..., n - 1, place data[i] into the position returned by one less than the value of the second array evaluated at data[i]. Then decrement the value accessed in the second array by 1. sorted_array[positions[data[i]]-1] = data[i], positions[data[i]] -= 1.