Question: Given the following procedure, analyze its worst case time complexity in big-O notation by only considering the comparison operations. The algorithm takes as input a
Given the following procedure, analyze its worst case time complexity in big-O notation by only considering the comparison operations.
The algorithm takes as input a list of n integers in non decreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is non decreasing if each integer in the list is at least as large as the previous integer in the list.)
Comment: The outer while loop checks each element in the list. For each element, when a_j occurs more than once is identified, then the inner while loop will skip all element in the list equal to a_j so that move to the next element not equal to a_j (works because the list is sorted).
procedure duplicates(a1, a2,, an: integers in non decreasing order)
k := 0 {this counts the duplicates}
j := 2
while j n
if a_j = a_j1 then
k := k + 1
c_k := a_j
while j n and a_j = c_k
j := j + 1
j := j + 1
{c1, c2,, ck is the desired list}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
