Best to worst

A bubble sort algorithm is used to sort an array containing 20 elements.
Let l l be the number of comparisons made in the best case, and
p p be the number of comparisons made in the wort case.
What is the value of l + p l+p ?


The answer is 209.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

展豪 張
Jul 12, 2016

The best case is when the array is already sorted.
One iteration and no swaps is needed.
Number of comparisons = 19 =19 .

The worst case is when the array is completely reversed.
19 19 iteration is needed.
Any pairs of numbers have been compared.
Number of comparisons = ( 20 2 ) = 190 =\binom{20}2=190

Answer = 190 + 19 = 209 =190+19=209

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...