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

A series of homework problems related to sorting algorithms, including shell sort, quicksort, heap sort, mergesort, insertion sort, bubble sort, selection sort, and their complexity analysis. Students are required to sort arrays using different methods, trace the execution of quicksort, build heaps, analyze the number of comparisons and exchanges in various sorting algorithms, and compare the stability and efficiency of different sorting algorithms.

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-vaz
koofers-user-vaz 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
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
pf3
pf4
pf5

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