Sonoma State University
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.

Assignment on Heaps, Sorting and Using Algebra to Improve Efficiency

This assignment is in three parts: Heaps, Heap Sort and Horner's Rule.

Heaps

This section is divided into

Discussion

A max-heap is a binary tree with the property that the value at each node is larger (according to some ordering) than the values stored in either child. Similarly, a min-heap is a binary tree with the property that the value at each node is smaller (according to some ordering) than the values stored in either child.

Further, we restrict our attention to trees which are completely balanced. All nodes have 2 children, except at the bottom of the tree. The last row of the tree may not be full, but any items on the last level are moved left as much as possible. For example, the following tree illustrates the shape of a tree and the ordering of values within the tree for a min-heap.

typical heap

A Simple Labeling of Nodes: Connecting a Heap with an Array

Since nodes in a heap fit a straight forward identification system, there is a natural mapping between the logical nodes in a heap and the indexed elements of an array. To identify the nodes in a tree, the simplest approach is to number the nodes level-by-level from the root downward. Within a level, nodes are numbered left to right. Also, for simplicity, many textbooks assign the index 1 to the root node.

standard labeling

In examining the label nodes, a pattern emerges: for each node labeled i, the left child has label 2*i and the right node has label 2*i+1.

detail for standard labeling

Note: As previously mentioned, some textbooks, such as Data Structures and Problem Solving Using Java, Fourth Edition by Mark Allen Weiss, use this numbering scheme in writing code, with the top node numbered 1. If one considers an array element 0, Weiss suggests filling that position with -∞ for a min-heap or ∞ for a max-heap.

0-based Labeling of Nodes

  1. In the context of 0-based labeling, identify a pattern for moving from a node with label j to its left child and its right child. What labels would be found on the left and right nodes of a node with label j?

detail for 0-based labeling

Insertion into a Heap

In class we considered how to insert an item into a heap and maintain the structure. In each case, we first place the item at the end of a tree (as far left as possible in the last row). The item then is worked up from its position, until the data in the tree are appropriately ordered.

  1. The following min-heap repeats the structure given at the beginning of this lab:

    typical heap

    Using this min-heap as a start and following the standard procedure to add an element to a heap, insert the values 10, 14, 6, and 12. Show the structure of the tree and the values in each node after each insertion.

Deletion from a Heap

In class, we also considered how to remove the top-priority item from a heap: remove the root as the item to be returned, move the last item from the end of the heap to the root, and work that item down in the heap until the data are properly ordered.

  1. Starting with the original heap from the previous problem, use the standard procedure for deleting items from a heap to remove four items in sequence. Show the structure of the tree and the values in each node after each deletion.

Heap Sort

The Heap Sort draws upon the heap data structure, which is discussed in Section 6.4 of the textbook.

  1. The key procedure percDown for the Heap Sort has the following specification

    /** *******************************************************************************
     * percDown function                                                              *
     * @param  array  the array to be made into a heap, starting at hold              *
     * @param  hole:  the node index (or base) of subtree for start of processing     *
     * @param  size  the size of the array                                            *
     * @pre   all nodes in left and right subtrees of the hole node are heaps         *
     * @post  all nodes in the tree from the hole node downward form a heap           *
     *********************************************************************************/
    void percDown(int array [ ], int hole, int size)
    

    Implement this procedure, paying attention to the following notes.

  2. Using the percDown procedure, implement a heapSort procedure, with the following specification. (Details may be found in Levitin's text, Section 6.4.)

     /** ******************************************************************************
     * heap sort, main function                                                       *
     * @param  a  the array to be sorted                                              *
     * @param  n  the size of the array                                               *
     * @post  the first n elements of a are sorted in non-descending order            *
     *********************************************************************************/
    
  3. Include the heapSort procedure within a complete program that

For the Heap Sort part of the assignment, turn in both a listing of the complete program (including all of Step 4) and the output produced by running the code on several arrays.

Horner's Rule

  1. A polynomial function has the form

    p(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0

    Write a function compute_poly that takes three parameters:

    and returns the value of the polynomial p(x).

    Notes:

For this part of the assignment, turn in both a listing of the complete program and the output produced by running the code for several different arrays with varying sizes, including the example given above. Further, for each of your tests, write a short paragraph or present a table that indicates the polynomial being tested (e.g., 2x3 + 5) and indicates the correct value (e.g., result=137) when a specified value (e.g., x=3) is used in the polynomial evaluation. Effectively, this commentary demonstrates that you have looked at the output and can certify that the program works properly. (An answer without this testing/paragraph will be subject to a 10-point penalty. Also, claiming a test is correct when an output value matches an incorrectly computed "correct" value is subject to a 5 point penalty per test case.)

created December 1, 2018
revised December 2, 2018
revised December 27-30, 2021
revised February 4, 2022
reformatted and heap material added July 28, 2022
reorganized with brute force/merge sort added October 3-6, 2022
revised December 30, 2022
reorganized and revised Summer, 2023
reorganized and revised November 30, 2024
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2025 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.