Lecture  23 – Design of HeapSort Program

Steps to carry out to build heap

 

   Initialize PositionCounter

   While heapArray not full and there is more data

    Get number

    Increment PositionCounter

    Put  number in heapArray at PositionCounter

    If number being inserted is not at rootPosition — while n != 1

         Determine parentPosition

         Compare the childValue to its parentValue

          If childValue is less than the parentValue

          Set childPosition to parentPosition

 

Elements Needed

Need an array

PositionCounter --last entered element

N -- moving target to get to parent

 

Subprograms—

Print array

Get number

Swap  -- Swap parent and child

Show swap

Get Child

Get Parent

-------------------------------------------------------------------------------------

2nd part of problem

Sort  (i.e. heapsort)  Algorithm

LOOP

Swap first and last element in the array

Decrement the last element counter

Find smaller child  of 2n and 2n + 1

Swap parent with smaller child

Update parent’s position to smaller child position

Find smaller child

UNTIL the parent’s children’s positions are greater then the last element counter

 

------------------------------------------------------------------------------------------

Due for Tuesday, November 21st:

          Heap sort program (i.e.  specification, body, test) zipped together

Binary Search Tree program –

myHeap2.exe is available at  https://users.cs.jmu.edu/adamses/public 

    it shows you the output from my BUILDHEAP portion of program.

          Sample data I used with myHeap2.exe is here  heapInput.txt

          Here is the package specification I used   .HeapUsefulStuff2.ads

                 If you have trouble opening it, here’s a txt version