Comp 1672, sections 1 and 2 Homework 8 Solutions 1a. List: 2 10 17 45 49 55 68 85 92 98 110 (start number at 1) interation # key: first (value) middle (value) last (value) 1 15 1 (2) 6 (55) 11 (110) 2 15 1 (2) 3 (17) 5 (49) 3 15 1 (2) 1 (2) 2 (10) 4 15 2 (10) 2 (10) 2 (10) search key 15 is not in the list, verified after 4 compares. 1b. interation # key: first (value) middle (value) last (value) 1 49 1 (2) 6 (55) 11 (110) 2 49 1 (2) 3 (17) 5 (49) 3 49 4 (45) 4 (45) 5 (49) 4 49 5 (49) 5 (49) 5 (49) search key 49 is at position 5 of the list, verified after 4 compares 1c. interation # key: first (value) middle (value) last (value) 1 98 1 (2) 6 (55) 11 (110) 2 98 7 (68) 9 (92) 11 (110) 3 98 10 (98) 10 (98) 11 (110) search key 98 is at position 10 of the list, verified after 3 compares 1d. interation # key: first (value) middle (value) last (value) 1 99 1 (2) 6 (55) 11 (110) 2 99 7 (68) 9 (92) 11 (110) 3 99 10 (98) 10 (98) 11 (110) 4 99 11 (110) 11 (110) 11 (110) search key 99 is not in the list, verified after 4 compares 2. Here's a list of integers: 20 14 2 34 23 Give all of the intermediate re-orderings of the list if we're sorting it using an insertion sort. 14 20 2 34 23 2 14 20 34 23 2 14 20 23 34 3. How many comparisons and how many moves are required for an array based insertion sort of n distinct elements if a. the list is already in ascending order. n-1 comparisons, no moves b. the list is in desending order. Let's start with an example - say n=5, list contains 5 4 3 2 1 We skip over 5, and look at 4. We do one compare, then 3 moves to swap the 4 and the 5. We continue on to the 3. This takes 2 compares (compares to 4 and 5) and 4 moves (2 to move the 3 to temp and back to its proper position, and 2 to move the 4 and 5 down the list). To move the 2 to the beginning of the list, we need 3 compares and 5 moves. To move the 1 to the beginning of the list, we need 4 compares and 6 moves. Summing, we have 1 + 2 + 3 + 4 = 10 compares and 3 + 4 + 5 + 6 = 18 moves. More generally, if we have a list of n elements, we require 1 + 2 + ... + n-1 = n(n-1)/2 compares 3 + 4 + ... + n+1 = n(n-1)/2 + 2(n-1) moves 4. Here's a list of integers: 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 Draw a map of the divide and merge steps that would result if we're sorting it using a merge sort. (see figure 18-54) 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 34 56 10 4 18 2 90 5 45 12 80 78 1 19 25 67 34 56 4 10 2 18 5 90 12 45 78 80 1 19 25 67 4 10 34 56 2 5 18 90 12 45 78 80 1 19 25 67 2 4 5 20 28 34 56 90 1 12 19 25 45 67 78 80 1 2 4 5 12 19 20 25 28 34 45 56 67 78 80 90 5. How many comparisons are necessary for an array-based merge sort of 32 items if a. the list is already in ascending order. After the divisions have occurred down to the bottom level, we have to do 5 levels of merging. The first level of merging takes 16 compares (one for each pair). The second level takes (at most) 3 compares for each of 8 groups of 4, or a total of 24 compares. The next level takes 7 compares for each of 4 groups of 8, or 28 compares, The fourth level takes 15 compares for each of the two groups, or 30 compares. The fifth level of merging takes 31 compares. We have a total of 16+24+28+30 + 31 = 129 compares b. the list is in desending order. The order of the list doesn't matter. The answer is the same as for part a. 6.Name the sorting technique innocently being used in the scenarios below. a. Insert sort. b. Merge sort. c. Selection sort (not required for final exam) 7. T/F: In every search instance, a binary search will execute faster than a linear search. False. If the search key happens to be the first item, then the linear search is faster. T/F: You should never use a linear search instead of a binary search, because a binary search is O(log n) and a linear search is O(n). False. The elements in the list must be sorted for the binary search to work. If the list is sorted, then the binary search is faster. T/F: You should never use a bubble sort. True. Bubble sort exists only for teaching purposes.