Docsity

CS 361 Lecture 15: Midterm Preparation - Quicksort and Heaps, Study notes of Computer Science

Toggle sidebar
bg1
CS 361, Lecture 15
Jared Saia
University of New Mexico
Outline
•Midterm
•Quicksort
1
Midterm
•5 questions, 20 points each
•Hard but fair
•There will be some time pressure, so make sure you can e.g.
solve recurrences both quickly and correctly.
•I expect a class mean of between 50 :( and 65 :) points
2
Question 1
Collection of true/false questions and short answer on:
•sorting algorithms (mergesort, heapsort, bubblesort)
•heaps (heights, number of nodes, heap algorithms, where is
the max?, where is the min?)
•theta notation (i give you a bunch of functions and ask you
to give me the simplest possible theta notation for each)
3
Question 2
•A question on annihilators and recurrence trees (like problems
1-3 of hw)
•You’ll need to know the formula for sum of an infinite con-
vergent series
4
Question 3
•A question on using annihilators to solve a recurrence with
both homogeneous and non-homogeneous parts
5
pf3
pf4
pf5

Partial preview of the text

Download CS 361 Lecture 15: Midterm Preparation - Quicksort and Heaps and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 15

Jared Saia University of New Mexico

Outline

  • Midterm
  • Quicksort

Midterm

  • 5 questions, 20 points each
  • Hard but fair
  • There will be some time pressure, so make sure you can e.g. solve recurrences both quickly and correctly.
  • I expect a class mean of between 50 :( and 65 :) points

Question 1

Collection of true/false questions and short answer on:

  • sorting algorithms (mergesort, heapsort, bubblesort)
  • heaps (heights, number of nodes, heap algorithms, where is the max?, where is the min?)
  • theta notation (i give you a bunch of functions and ask you to give me the simplest possible theta notation for each)

Question 2

  • A question on annihilators and recurrence trees (like problems 1-3 of hw)
  • You’ll need to know the formula for sum of an infinite con- vergent series

Question 3

  • A question on using annihilators to solve a recurrence with both homogeneous and non-homogeneous parts

Question 4

  • A question on writing recurrences for both the result of a function and the time cost of the function, and solving both of these recurrences using annihilators

Question 5

  • A question asking you to prove the correctness of an algo- rithm using loop invariants
  • I’ll give you the loop invariant and ask you to prove initial- ization, maintenance and termination
  • Will be for an algorithm on heaps

Questions

  • Any questions?

Review Session

There will be a review session Today at 1pm

Other Review Session Options:

  • Today at 5pm
  • Today at 7pm
  • Tomorrow at 3pm
  • Tomorrow at 5pm

In-Class Exercise

  • Imagine you have a min-heap with the following operations defined and taking O(log n): - (key,data) Heap-Extract-Min (A) - Heap-Insert (A,key,data)
  • Now assume you’re given k sorted lists, each of length n/k
  • Use this min-heap to give a O(n log k) algorithm for merging these k lists into one sorted list of size n.

In-Class Exercise

  • Q1: What is the high level idea for solving this problem?
  • Q2: What is the pseudocode for solving the problem?
  • Q3: What is the runtime analysis?
  • Q4: What would be an appropriate loop invariant for proving correctness of the algorithm?

Correctness

Basic idea: The array is partitioned into four regions, x is the pivot

  • Region 1: Region that is less than or equal to x
  • Region 2: Region that is greater than x
  • Region 3: Unprocessed region
  • Region 4: Region that contains x only

Region 1 and 2 are growing and Region 3 is shrinking

Correctness

Basic idea: The array is partitioned into four regions, x is the pivot

  • Region 1: Region that is less than or equal to x (between p and i)
  • Region 2: Region that is greater than x (between i + 1 and j − 1)
  • Region 3: Unprocessed region (between j and r − 1)
  • Region 4: Region that contains x only (r)

Region 1 and 2 are growing and Region 3 is shrinking

Example

  • Consider the array (2 6 4 1 5 3)

Loop Invariant

At the beginning of each iteration of the for loop, for any index k:

  1. If p ≤ k ≤ i then A[k] ≤ x
  2. If i + 1 ≤ k ≤ j − 1 then A[k] > x
  3. If k = r then A[k] = x

In Class Exercise

  • Show Initialization for this loop invariant
  • Show Termination for this loop invariant
  • Show Maintenance for this loop invariant:
    • Show Maintenance when A[j] > x
    • Show Maintenance when A[j] ≤ x

Scratch Space

Scratch Space

Analysis

  • The function Partition takes O(n) time. Why?
  • Q: What is the runtime of Quicksort?
  • A: It depends on the size of the two lists in the recursive calls

Best Case

  • In the best case, the partition always splits the original list into two lists of half the size
  • Then we have the recurrence T (n) = 2T (n/2) + Θ(n)
  • This is the same recurrence as for mergesort and its solution is T (n) = O(n log n)

Worst Case

  • In the worst case, the partition always splits the original list into a singleton element and the remaining list
  • Then we have the recurrence T (n) = T (n − 1) + T (1) + Θ(n), which is the same as T (n) = T (n − 1) + Θ(n)
  • The solution to this recurrence is T (n) = O(n^2 ). Why?

Todo

  • Read Chapter 7
  • Finish HW
  • Study for Midterm!