Question: Consider the sorting algorithm shown below, which is called bubblesort. Algorithm 1 . 1 6 bubblesort Input: An array A [ 1 . . n
Consider the sorting algorithm shown below, which is called bubblesort.
Algorithm bubblesort
Input: An array An of n elements.
Output: An sorted in nondecreasing order.
i; sorted false
while i n and not sorted
sorted true
for j n downto i
if Aj Aj then
interchange Aj and Aj
sorted false
end if
end for
i i
end while
a What is the minimum number of element comparisons performed
by the algorithm? When is this minimum achieved?
b What is the maximum number of element comparisons performed
by the algorithm? When is this maximum achieved?
c What is the minimum number of element assignments performed
by the algorithm? When is this minimum achieved?
d What is the maximum number of element assignments performed
by the algorithm? When is this maximum achieved?
e Express the running time of Algorithm bubblesort in terms of the
O and notations.
f Can the running time of the algorithm be expressed in terms of the
Theta notation? Explain.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
