You are on page 1of 5

Faculty of Computer Science and Engineering Department of Computer Science

DATA STRUCTURES & ALGORITHMS


Tutorial 6 Questions SORTING ALGORITHMS
Required Questions Question 1. Many operations can be performed faster on sorted than on unsorted data. For which of the following operations is this the case? a. checking whether one word is an anagram of another word, e.g., plum and lump b. finding the minimum value. c. computing an average of values d. finding the middle value (the median) e. finding the value that appears most frequently in the data Answer a. Yes b. Yes c. No d. Yes e. Yes Question 2. In which case, the following sorting algorithm is fastest/slowest and what is the complexity in that case? Explain. a. insertion sort b. selection sort c. bubble sort d. quick sort Answer Assume that the algorithms are performed on contiguous lists (i.e. array). Fastest Slowest Algorithm The input is a reverse-ordered list. At Insertion The input is an ordered list. At every step, the element at current is only every step, the element at current is sort compared to the element at (current 1). compared to every elements preceded it. Comparisons: C = n 1 = O(n) Moves: M = 0 or M = 2(n 1) = O(n) Comparisons: C = n(n 1)/2 = O(n2) Moves: M = (3+4+5+...(n1+2)) = (n(If we change the algorithm so that it 1)(n+4)/2 = O(n2) does not copy out the data in line 1.2.1 1

Faculty of Computer Science and Engineering Department of Computer Science

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

Faculty of Computer Science and Engineering Department of Computer Science

Answer 1 Smallest in quick sort 1 Pivot in quick sort 1 i index in quick sort

Algorithm Insertion sort (Slide 13) 5 5 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 8 5 4 3 3 2 2 8 2 2 2 2 2 2 5 2 2 2 2 2 2 2 2 8 5 4 4 3 3 2 8 3 3 3 3 3 8 5 3 3 3 3 3 4 4 4 8 5 5 4 4 4 4 4 4 4 4 4 2 8 5 4 4 4 4

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

Selection sort (Slide 32)

Bubble sort (Slide 42)

C = number of comparisons M = number of moves C = 0, M = 0 C = 1, M = 2 C = 2, M = 4 C = 3, M = 4 C = 4, M = 5 C = 2, M = 3 C = 6, M = 8 C = 2, M = 3 C = 20, M = 29 C = 7, M = 3 C = 6, M = 3 C = 5, M = 3 C = 4, M = 3 C = 3, M = 3 C = 2, M = 3 C = 1, M = 3 C = 28, M = 31 C = 7, M = 18 C = 6, M = 9 C = 5, M = 6 C = 4, M = 6 C = 3, M = 3 C = 2, M = 3 C = 1, M = 0 C = 28, M = 45

Merge sort (Slide 56-60)

Heap sort

5824 3617 58 24 36 17 5 8 2 4 3 6 1 7 58 24 36 17 C=1+1+1+1=4 2458 1367 C=2+3=5 12345678 C=7 C = 16, M = 0 5 8 2 4 3 6 1 7 C = 1, M = 3 3

Faculty of Computer Science and Engineering Department of Computer Science

(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

C = 2, M = 6 C = 1, M = 3 C = 0, M = 3 Tng C = 25, tng M = 57

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

Faculty of Computer Science and Engineering Department of Computer Science

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 --