Heapsort
1. Constructs an array satisfying the max-heap property.
2. Sorts the array in ascending order.
Note: A.length is the length of array A, which does not change.
A.heap-size is the size of the heap, which does change.
1 A.heap-size A.length
Heapsor
Binary tree - recursively defined on a finite set of nodes that either
contains no nodes
is composed of three disjoint sets of nodes:
root node
binary tree called the left subtree
binary tree called the right subtree
Examples
(b)
(a)
(c)
(d)
2
/
3
/
4
/
6
Build-Max-Heap
"Max" refers to the fact that this algorithm calls Max-Heapify, which guarantees that the
object the with the largest value is at the top of the heap.
Build-Max-Heap (A)
- alters A
- post: the tree rooted at location 1 in A satisfies the he
Uses:
Use recursion tree to determine a good asymptotic bound on the recurrence T(n) =
o
Sum the costs within each level of the tree to obtain a set of per-level costs.
o
Sum all the per-level costs to determine the total cost of all levels of recursion.
Definition
Asymptotic
a line that continually approaches a given curve but does not meet it at any finite
distance.
Example
x is asymptotic with x + 1
limit -
f(x) = k
Roughly translated might read as:
x approaches , f(x) approaches k
for x close to , f(x
Max heaps maintain the maximum heap value at array location 1.
A priority queue requires that the priority value be accessible at the head of the queue, location 1.
Example - The highest priority (max value) is array location 1.
Question 6.8
Suggest an al
Max-heap property
The max-heap property:
for every node i other than the root
A[ Parent( i ) ] A[ i ]
Note that the root is excluded as it has no parents.
The max-heap property means that the parent value child value
Max-Heapify
Max-Heapify function, give
4.3 OVERVIEW - Don't confuse with forward or backward substitution
Substitution as used here, refers to substituting a guess for a closed-end recurrence solution into
an induction proof.
To prove asymptotic bounds, or O, of the recurrence T(n) must show h
Sorting_Machine can be implemented by the sorting algorithms listed in the table below.
The actual work done to get the items sorted will be performed in one of the three
different sorting machine operations:
o
Insert
o
Extract
o
Change_To_Extraction_Pha
How fast can we sort?
We will prove a lower bound.
Why ( n ) as a lower bound?
Comparison sorting
The only operation that may be used to gain order information about a
sequence is comparison of pairs of elements.
if A[ i ] < A[ i + 1 ]
A[ i ] A[ i + 1]
Al
Definition
A heap can be defined as a binary tree with one key (value) assigned to each node provided the
following two requirements are met:
Tree shape - the binary tree is essentially complete; all levels are full
except possibly the last level where on
Note that tree here means binary tree.
A complete tree is a full tree.
1. There are at most 2h leaves in a binary tree of height h.
That is the nodes at the bottom level.
Base
h=1, no more than 21 = 2 leaves since a binary tree
IH
2h-1 leaves at most in b
Computing the limit of the ratio of two functions is another way to classify the growth of a
function.
What f(n) is in terms of O, , , o, and
f(n)/g(n
)
o
0
O
f(n)
O(g(n)
f(n)
(g(n)
f(n)
(g(n)
f(n)
o(g(n)
c
f(n)
(g(n)
Definition of O, , , o, and by
Assignment
1. Write down for each statement the number of times the statement is executed in the worst
case.
2. Compute T(n), i.e., the running time of Find_Largest, and simplify (if possible) by
combining terms.
3. What data produces the worst case? The
Assignment
1. Write down for each statement the number of times the statement is executed in the worst
case.
2. Compute T(n), i.e., the running time of Find_Largest, and simplify (if possible) by
combining terms.
3. What data produces the worst case? The
Homework 7
Document last modified: 03/07/2014 04:44:57
Overview
The text presents algorithms and analysis of an Insertion and Quick sort. The assignment seeks to
verify empirically the accuracy of those analysis's by profiling the execution of each algori
Homework 8
Document last modified: 01/06/2012 10:09:12
1. (3) Explain under what circumstances, if any, redundant DISK-WRITE or DISK-READ
operations are performed during the course of executing a call to B-TREE-INSERT. A
redundant DISK-READ is reading a p
Homework 1
Document last modified: 01/16/2013 06:27:00
1. Text 2.1-3.
From text:
Consider the searching problem:
Input: A sequence of n numbers A=<a1,a2,.,an> and a value v.
Output: An index i such that v=A[i] or the special value NIL if v does not appear
Homework 3
Document last modified: 01/06/2012 10:08:45
1. Exercise 3.1-1 - The following is half the proof.
Prove max(f(n), g(n) is (f(n) + g(n)
That means determine two constants c1 and c2 such that:
c1(f(n) + g(n) max(f(n), g(n) c2(f(n) + g(n)
To clarif
Homework 9
Document last modified: 01/06/2012 10:08:11
1. (6) Show the data structure p that results and the answers returned by the following
program.
i
1 2 3 4 5 6 7 8
Line 1 p[i]
Line 2 p[i]
Line 3 p[i]
Line 4 p[i]
Line 5 p[i]
2.
1.1.
Line 5 answer: _
Homework 10
Document last modified: 04/09/2014 00:24:07
The following homework performs empirical analysis of an algorithm to determine the worst
case run time performance.
The general idea is to:
Determine by experimentation (i.e. running multiple experi
C455 Test 1 Sample and Study Guide
You'll be given any necessary reference material such as algorithms from the
text or notes, definitions of and summation formulas.
You can bring one page of notes on the front and back of an 8.5x11 inch
paper and a calcu
Homework 6
Document last modified: 01/06/2012 10:08:15
1. (3) Exercise 11.1-3 Hint: Assume that a successful search can choose to return the
address of any node having the matching, duplicate key.
2. (4) In a hash table where chaining is used the chain li
Homework 2
Document last modified: 01/06/2012 10:08:42
1. Exercises 2.3-3, 2.3-4, 2.3-6
For the recurrence:
T(2) = 2
T(n) = 2T(n/2) + n if n=2k, for k>1
Hint: 2.3-3
Assume: T(2k) = 2k lg 2k
Show:
T(2k+1) = 2k+1 lg 2k+1
2. Suppose you take out a mortgage f
Homework 5
Document last modified: 01/06/2012 10:08:54
1. (4) Exercise 6.5-7 - A Java Heap can be downloaded for testing.
2. (2) Determine the cost for deleting the key at index i in Question 1.
3. (5) Design an efficient algorithm for finding and deletin
Homework 4
Document last modified: 01/31/2013 20:23:43
1. Draw the recursion tree and verify a good asymptotic upper bound (i.e. O) is correct
using substitution for the following recurrence. Substitution Subtlety will be required.
|c
T(n) =
| 3T(n/2) +