EE113D Project: Heap Sort - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

EE113D Project: Heap Sort

Description:

EE113D Project: Heap Sort Shuofei Geng Michael Utanes Felix Lu Heap Sort - Motivation Concept of Sorting present in everyday life: Sorting a list of grades MP3 Files ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 19
Provided by: Micha738
Learn more at: http://flu.bol.ucla.edu
Category:

less

Transcript and Presenter's Notes

Title: EE113D Project: Heap Sort


1
EE113D Project Heap Sort
  • Shuofei Geng
  • Michael Utanes
  • Felix Lu

2
Heap Sort - Motivation
  • Concept of Sorting present in everyday life
  • Sorting a list of grades
  • MP3 Files on a computer
  • Names on a phone Directory
  • More advanced engineering problems
    rely on efficient
    sorting problems
  • Heap Sort one of the best general-purpose
    sorting algorithms
  • Applications
  • Priority Queues,
  • Interval Scheduling

3
Heap Sort Project Goals
  • Implement the Max Heap Sort Algorithm
  • Utilize existing C code and convert to assembly
    language for use on using the DSK Plus Board
  • Input data a random array of integer values
  • Output data an array sorted in ascending order

4
What is Heap Sort?
  • Sorting Algorithm that first places objects into
    a heap
  • Heap organized such that the largest value can
    easily be extracted while preserving heap
    structure
  • Each time we delete (extract) the maximum, we
    place at next open position from the end of the
    sorted array
  • Keep extracting largest value of heap until none
    remains, giving the elements in order
  • Heap of remaining unsorted elements Sorted
    elements

5
The Heap
  • Heap can be represented as a balanced binary tree
  • Each element is stored at a node.
  • Max-Value at root of tree
  • Heap Order A Parent node can
    have two Child Nodes
  • The value of the Parent node always
    larger than the Child Nodes

6
Heap Sort SiftDown Operation
  • The siftDown() function is used to build and
    reconstruct the heap
  • function sift(a, start, count)
    //Organize Heap
  • var int root start, child //node
    in position i is parent of the nodes in positions
    2i and (2i1)
  • while root 2 1 lt count
    //compare with two children,
    make swap
  • child root 2 1
    //if chlidren larger than value if not keep
    traversing
  • if child lt count - 1 and achild lt
    achild 1
  • child child 1
  • if aroot lt achild
  • swap(aroot, achild)
  • root child
  • else
  • return

7
Heap Sort Pseudocode
  • function heapSort(a, count)
  • var int start count 2 - 1,
  • end count - 1
  • while start 0 //Build Heap
  • sift(a, start, count)
  • start start - 1
  • while end gt 0
  • swap(aend, a0)
    //Root Deletion
  • sift(a, 0, end) //Heap
    Reorganized
  • end end - 1
  • function sift(a, start, count)
    //Organize Heap
  • var int root start, child
    //node in position i is parent of the nodes in
    positions 2i and (2i 1)
  • while root 2 1 lt count
    //compare with two children, make
    swap
  • child root 2 1 //if chlidren
    larger than value if not keep traversing

8
Heap Sort Pseudocode
  • Heap Sort

9
Comparison with Other Sorts
  • Merge Sort Quick Sort
  • Splits the items to be sorted into two groups,
    recursively sorts each group, and combines them
    into a final, sorted sequence
  • Generally faster than heap sort, due to better
    data cache performance

10
Comparison with Other Sorts
11
Applications of Heap Sort
  • Interval Scheduling
  • List of tasks with various start/finish times
  • Aim to perform tasks within specified time
    interval without overlapping
  • Sort finish time of tasks then take earliest
    finish time until all tasks performed
  • Priority Queue - Supports the following three
    operations
  • Add an element to the queue with an associated
    priority
  • Remove the element from the queue that has
    highest priority, and return it
  • Peek at the element with highest priority without
    removing it
  • Ex. Bandwidth Management

12
Heap Sort Implementation
  • Project Development
  • First decide on topic for implementation
  • Challenge No previous Heap Sort project exists
  • Write Everything From Scratch
  • Then Model Sorting algorithm with High Level
    Language (C or matlab)
  • Internet contains many topics/code on Heap Sort
  • Lastly, Program/Debug Max Heap Sorting Algorithm
  • Utilize existing C code and convert to assembly
    language for use on using the DSK Plus Board
  • Program consists of 2 files randomno.asm,
    heapsort.asm

13
Heap Sort Implementation
  • Program consists of two files
  • randomno.asm - list, containing 100 random s
  • To create list, we wrote a short C program
    RandomNoGenerator.c that outputs random s in
    assembly format i.e. .word 4
  • Then we copied the input data to the correct file
    type to represent random array
  • heapsort.asm Executable code
  • Implements heap sort algorithm

14
Heap Sort Implementation
  • heapsort.asm Design Challenges
  • Use of Pointers
  • When to use
  • Limited amount of Registers (AR0-AR7)
  • Comparison Operations
  • Flags TC NTC
  • Function Usage
  • Implementation
  • Passing Parameters
  • Returning from Function
  • Adding nop instruction

15
Heap Sort Implementation
  • heapsort.asm Debugging Challenge
  • Adding nop Instruction
  • While executing program in whole program
    misbehaves
  • Due to certain instructions taking longer than a
    cycle to complete, thus not properly registering
    values in time
  • Solution Add nop instruction between lines
    where this is true
  • Found by use of Breakpoints in code

16
Heap Sort Discussion of Results
  • Program correct in implementing Heap Sort
  • Output a sorted array
  • Also Concerned about Program Efficiency
  • Does it meet the time requirement O(nlog2(n)),
    where n is the of elements in the array?

17
Heap Sort Discussion of Results
  • Does it meet the time requirement O(nlog2(n))?
  • 1st- Check of times siftdown2() is called
  • After function returns, the 1st and last element
    of array is swapped
  • Process stops until function calls Max n times
  • 2nd- See iterations performed inside function
  • After every iteration, multiply the current index
    by 2 then use as current index
  • Since only checking index which is in power of 2
    of the original root passed in as parameter, we
    can do at most log2(n) operations
  • Therefore O(nlog2(n)) is satisfied

18
Heap Sort - Summary
  • Positives
  • Does not require multiple arrays
  • Does not include recursion
  • Optimal for large data sets
  • Runs well with computers with slow data caches
  • Negatives
  • Generally Slower than Merge Sort/Quick Sort
  • Unstable
Write a Comment
User Comments (0)
About PowerShow.com