Starting from:

$30

CH231A-Homework 3 Asymptotic and Analysis Selection Sort Solved

Problem 3.1 Asymptotic Analysis                                                                         
Considering the following pairs of functions f and g, show for each pair whether or not it belongs to each of the relations f ∈ Θ(g), f ∈ O(g), f ∈ o(g), f ∈ Ω(g), f ∈ ω(g), g ∈ Θ(f), g ∈ O(f), g∈o(f), g∈ Ω(f), or g∈ω(f).

(a)      f(n) = 9n and g(n) = 5n3,



(b)      f(n) = 9n0.8 + 2n0.3 + 14logn and g(n) = n,

(c)      f(n) = n2/logn and g(n) = nlogn,

(d)      f(n) = (log(3n))3 and g(n) = 9logn.

Problem 3.2 Selection Sort                                                                                   
Insertion Sort was discussed during the lecture. Selection Sort is similar to Insertion Sort and works as follows. Given an array of elements, you always take the current element and exchange it with the smallest element that can be found on the right hand side of the current element. In doing so, you will gradually build up a sorted sequence on the left side (like in Insertion Sort), and in each iteration ”attach” the smallest element from the remaining unsorted right side to it.

(a)     Implement Selection Sort.

(b)     Show that Selection Sort is correct (Hint: consider the loop invariant).

(c)     Generate random input sequences of length n as well as sequences of length n that represent Case A and Case B for the Selection Sort algorithm. Case A: the case which involves the most swaps (Hint: it is not a decreasingly ordered array). Case B: the case with the least swaps. Briefly describe how you generated the sequences (e.g., with a random sequence generator using your chosen language).

(d) Run the algorithm on the sequences from (c) with length n for increasing values of n and measure the computation times. Plot curves that show the computation time of the algorithm in Case A, Case B, and average case for an increasing input length n. Note that in order to compute reliable measurements for the average case, you have to run the algorithm multiple times for each entry in your plot. You can use a plotting tool/software of your choice (Gnuplot, R, Matlab, Excel, etc.).

(e)    Interpret the plots from (d) with respect to asymptotic behavior and constants.

More products