Docsity

CISC 220 Homework 3: Sorting Algorithms and Complexity Analysis, Assignments of Data Structures and Algorithms

Partial preview of the text

Download CISC 220 Homework 3: Sorting Algorithms and Complexity Analysis and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CISC 220 Homework 3 Name _____________________________

  1. Using shell sort, sort the following array using powers of 2 for the gap (16, 8, 4, 2, 1). Now sort the array using the gaps (15, 7, 3, 1). How many total comparisons? Exchanges? (.5%)

30 20 17 22 27 11 14 6 10 16 32 7 8 33 4 15 25 18 29 19 28 5 2 23 24 12 1 31 21 26 9 3 13

  1. Trace the execution of quicksort on the following array. Make the first element be the pivot. Show the elements referenced by first and last-1 for each recursive call and the array between first and last-1 for each return from a call. Now add a step in which you find the pivot by using insertionsort on the first, middle, and last-1 element in the array and choose the middle value. Show the elements referenced by the first and last-1 for each recursive call and the array between first and last-1 for each return from a call. How many times is quicksort called and how many times is partition called for each case? (.5 %)

10 20 40 50 55 60 70 80 90 100

  1. Build a heap from the numbers in the following list. How many exchanges? How many comparisons? Now create a sorted array from this heap. How many exchanges? How many comparisons? (.5%)

55 50 10 40 80 90 60 100 70 80 20

  1. Show the state of the array [5 | 9 | 2 | 1 | 4 | 3 | 7 | 8 | 2] after each pass of: insertion sort, bubble sort, and selection sort. Keep the sorted list to the left, and sort small to large. Which retains stability? How many passes for each? How many comparisons? How many exchanges? (1%)
  2. Under what circumstances would you prefer using selection sort over insertion sort? (.2%)
  1. In insertionsort, the function Insert is written using iterator traits. (e.g., :

Template Void insert (RI first, RI next_pos) { Typename std::iterator_traits::value_type next_val = *nextpos; While (next_pos != first && next_val < *(next_pos – 1)) { *next_pos = *(next_pos -1)) ; --next_pos; } *next_pos=next_val; }

What is the point of using iterator traits in this function. (.5%)

  1. Can quicksort be written iteratively instead of recursively? Justify your answer. (.5%)