Download presentation
Presentation is loading. Please wait.
Published byLindsey Holland Modified over 5 years ago
1
Algorithms CSCI 235, Spring 2019 Lecture 26 Midterm 2 Review
2
Problem 1 1. a) Use Heapify to turn the following into a heap by moving the root node (with value 4) into its correct position. Show the tree after each swap. List each comparison made between pairs of elements, and count the total number of comparisons. 4 23 15 6 17 18 14 10 12 1 5 b) Write the resulting Heap, represented as an array.
3
Problem 2 A= 13 19 9 5 12 8 21 10 Lomuto-Partition(A, p, r)
x <- A[r] i <- p -1 for j <- p to r-1 do if A[j] <= x then i <- i + 1 swap(A, i, j) swap(A, i+1, r) if i = (r - 1) then return i else return i +1 Show how this Partition algorithm partitions the following array for one pass through the array. Show the array after each swap, and indicate the positions of i and j immediately after each swap. Assume p is 1 and r is the length of the array (8). A=
4
Problem 3 3. What is the running time for QuickSort if the array is partitioned into parts with sizes 1/3 and 2/3 of the original array in every partition step? Draw the recurrence tree and solve the recurrence equation to justify your answer.
5
Problem 4 4. Draw the decision tree for selection sort for an array of three elements <a, b, c>.
6
Problem 5 5. Given an array of integers ranging from 1 to k, write an algorithm that determines which integer occurs the maximum number of times (in statistics, this is known as the mode of the values). Your algorithm should run in Q(n) time.
7
Problem 6 6. Write the order of numbers after each pass through Radix sort on the following list of numbers: 427 625 126 871 452 439 516 385
8
Problem 7 7. a) Insert the following numbers one at a time in the order given into a binary search tree: b) Traverse the tree in post-order traversal and give the order of nodes visited.
9
Problem 8 8. Draw the tree that results from performing a right rotation about the root node of the tree given below. Indicate red nodes with no fill and black nodes with a light fill or hatch-marking. 26 17 41 14 21 30 NIL NIL NIL NIL NIL NIL NIL
10
Possible types of Essay Questions
In your own words, describe how X-Sort works (where X=Selection, Insertion, Bubble, Merge, Quick, Heap, Counting, Radix, Bucket). What are the advantages (or disadvantages) of QuickSort? (or any of the other sorts) Under what circumstances would you use Counting Sort? (or any of the other sorts) D. Why is it important to know whether an algorithm is stable or in place?
11
More Questions E. Explain why, if a node in a binary search tree has two children, then its immediate successor has no left child. What is the purpose of finding the median of medians in an array? Why would you use a red-black tree instead of an ordinary binary search tree? What are the running times in best and worst cases of: Heapify, Tree-Successor (for Binary Search Tree), Find the kth order statistic of an array, Find the median of an array, Finding the median of medians of an array, etc. Justify your answer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.