Challenge 56

Description

The purpose of this challenge is to understand the “heapification” process of the heap sort algorithm.

Requirements

  1. Write a function bool is_max_heap(int data[], int count) that returns a true or false value depending on the status of the heap passed in as an array. The size of the actual dataset is passed in as count. To check if data is arranged in the array as a max-heap, ensure that all children nodes are less than their parent nodes.
  2. Write a function void heapify(int data[], int count). This function takes in data via the array parameter. Then it ensures that all values are placed correctly in  max-heap order. Values are swapped in-place in data as necessary.  The size of the actual dataset being heapified is passed in as count. Remember that heapification,
    1. Ensures that the subtree root is the largest of the subtree (for max-heap)
    2. Ensures that all children nodes are lesser in value than their parent nodes (for max-heap)
  3. Remember the relationships below for the left and right indices of each node. You will use these values to traverse the heap for both functions above.
    int parent;    // this holds the index of any node
    int left = (parent * 2) + 1;
    int right = (parent * 2) + 2;
  4. Write a function void show_array(int data[], int count) to show the contents of the array. This will help debugging and troubleshooting.
  5. In main, create an array of integers to contain 1000 values
  6. Write a loop to continually ask the user to enter a value. This loop will stop when the user enters -1
  7. In main, for each entered number:
    1. Add the number into the array into the next available slot
    2. Call heapify() to ensure that the array satisfies the max-heap requirement
    3. Show the contents of the array
    4. Call is_max_heap() and display a message, something like “array is a verified max-heap” 
  8. You don’t actually have to write a heap sort for this exercise. You can if you want to.
  9. You can create as many other supporting functions as you want/need.

CATALOG ID: CPP-CHAL00056

Print Requirements