Professional Documents
Culture Documents
Tut+6 Sol Release
Tut+6 Sol Release
Selection sort
Bubble sort
Quick sort
and copy in the data in line 1.2.4 in the (At each i, we have to copy the data lecture slide every time, in this fastest out to the variable temp, and move (i case, the number of moves is 0. 1) elements forward, and copy the Otherwise, we need 2 moves at each temp to the first position.) step. Then the number of moves is 2(n 1).) This algorithm performs the same in all cases. C = n(n 1)/2 = O(n2) M = 3(n 1) = O(n) At each iteration, we have to compare all the remained elements to select the next smallest elements, follows by an exchange (3 moves) of the current position and the smallest position. For an ordered list input, if we change the algorithm so that it does not exchange the elements if the smallest position and the current position are the same, the number of move is M = 0. The input is an ordered list. The The input is an reverse-ordered list. algorithm stops after the first iteration. C=n(n 1)/2 = O(n2) C = n 1 = O(n) M=n(n 1)/2 = O(n2). M=0 At each step, we have to compare and move the next smallest element up to the beginning. Each recursion step the partitioning Each recursion step an unbalanced produces two parts of equal length. partitioning is produced, namely that one part consists of only one element Running time: O(n log(n)). and the other part consists of the rest of the elements. This is because the recursion depth is Running time: O(n2). log(n) and on each level there The recursion depth is n-1 and on each are n elements to be treated. level there are n elements to be treated.
Question 3. Consider the sequence of integers S = {5, 8, 2, 4, 3, 6, 1, 7} For each of the following sorting algorithms, indicate the sequence S after executing each step of the algorithm as it sorts this sequence: a. insertion sort b. selection sort c. heap sort d. bubble sort e. merge sort 2
Answer 1 Smallest in quick sort 1 Pivot in quick sort 1 i index in quick sort
S 3 3 3 3 8 6 5 5 3 3 8 8 5 5 5 4 3 8 5 5 5 5 6 6 6 6 6 8 6 6 6 6 6 6 6 6 6 3 4 4 8 6 6 6 1 1 1 1 1 1 8 7 5 5 5 5 8 8 7 6 6 6 6 8 7 7 7 7 7 7 7 7 7 8 7 7 7 7 7 7 8 7 7 7 7 7 8 8
Heap sort
(Slide)
5 5 5 8 8 4 7 7 1 6 6 1 5 5 3 4 4 1 3 2 2 1
8 8 8 5 7 7 4 5 5 5 5 5 1 4 4 3 3 3 1 1 1 2
2 6 6 6 6 6 6 6 6 1 2 2 2 2 2 2 2 2 2 3 3 3
7 7 7 7 5 5 5 4 4 4 4 4 4 1 1 1 1 4 4 4 4 4
3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5
6 2 2 2 2 2 2 2 2 2 1 6 6 6 6 6 6 6 6 6 6 6
1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 7 7 7 7 7 7 7
4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
C = 2, M = 3 C = 2, M = 0 C = 2, M = 3 C = 2, M = 3 C = 1, M = 0 C = 2, M = 6 C = 2, M = 3 C = 2, M = 6 C = 1, M = 3 C = 2, M = 6 C = 2, M = 3 C = 2, M = 6 C = 1, M = 0
Question 4. Consider the sequence of integers T = {1, 9, 2, 6, 4, 8, 0, 7} Indicate the sequence T after executing each step of the Cocktail sort algorithm (see Appendix) as it sorts this sequence. Answer T 1 9 2 6 4 8 0 7 1 0 0 0 2 1 1 1 6 2 2 2 4 6 4 4 8 4 6 6 0 8 7 7 7 7 8 8 9 9 9 9 C = number of comparisons S = number of swap C = 7, S = 6 (the first for loop in do while) C = 7, S = 5 (the second for loop in do while) C = 7, S = 2 (the first for loop in do while) C = 7, S = 0 (the second for loop in do while) Break do while C = 28, S = 13
Appendix Cocktail sort Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, shuttle sort or happy hour sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than bubble sort, and solves the problem with so-called turtles in bubble sort. (Wikipedia) Pseudocode
procedure cocktailSort( A : list of sortable items ) defined as: do swapped := false for each i in 0 to length( A ) - 2 do: if A[ i ] > A[ i + 1 ] then // test whether the two elements are in the wrong order swap( A[ i ], A[ i + 1 ] ) // let the two elements change places swapped := true end if end for if swapped = false then // we can exit the outer loop here if no swaps occurred. break do-while loop end if swapped := false for each i in length( A ) - 2 to 0 do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) swapped := true end if end for while swapped // if no elements have been swapped, then the list is sorted end procedure
-- End --