Problem 4 (20 points). Consider the following insertion-sort-like algorithm: Sort the elements whose index is equivalent to 1 (mod 3) using insertion sort. Sort the elements whose index is equivalent to 2 (mod 3) using insertion sort. Sort the elements whose index is equivalent to 0 (mod 3), using insertion sort. Sort all of the elements using (standard) insertion sort.
(a) Here is the pseudocode for Insertion Sort without a sentinel.
for i = 2 to n do
temp = A[i]
j = i-1
while j > 0 and A[j] > temp do
A[j+1] = A[j]
j = j-1
end while
A[j+1] = temp
end for
Write the pseudocode for the above insertion-sort-like algorithm, without a sentinel. The algorithm should NOT be recursive.
(b) Assume n = 12. What is the best-case number of comparisons? Just state the number and show your input. Otherwise, no justification needed.
(c) Assume n = 12. What is the worst-case number of comparisons? Just state the number and show your input. Otherwise, no justification needed.
(d) Calculate the number of comparisons the algorithm uses in the worst case for n as a multiple of 3. Show your work.