1 / 14

HeapSort

A refresher on sorting. HeapSort. A refresher on sorting. HeapSort - underlying datastructure: heap. Def:. A heap is a complete binary tree, with nodes storing keys, and the property that for every parent and child:

randalln
Télécharger la présentation

HeapSort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A refresher on sorting HeapSort

  2. A refresher on sorting HeapSort - underlying datastructure: heap Def: A heap is a complete binary tree, with nodes storing keys, and the property that for every parent and child: key(parent) · key(child).

  3. A refresher on sorting HeapSort - underlying datastructure: heap • Use: priority queue – a datastructure that supports: • extract-min • add key • change key value

  4. A refresher on sorting • Heap • stored in an array – how to compute: • how to add a key ? Parent Left child Right child

  5. A refresher on sorting • Heap • stored in an array – how to compute: • how to add a key ? Parent(i) = i/2 LeftChild(i) = 2i RightChild(i) = 2i+1 • HEAPIFY-UP(H,i) • while (i > 1) and (H[i] < H[Parent(i)]) do • swap entries H[i] and H[Parent(i)] • i = Parent(i) • ADD(H,key) • H.size++ • H[H.size] = key • HEAPIFY-UP(H,H.size)

  6. A refresher on sorting • Heap • stored in an array – how to compute: • what if we change the value of a key (at position i) ? • - if key decreased, then: • - otherwise ? Parent(i) = i/2 LeftChild(i) = 2i RightChild(i) = 2i+1

  7. A refresher on sorting • Heap • stored in an array – how to compute: • what if we change the value of a key (at position i) ? Parent(i) = i/2 LeftChild(i) = 2i RightChild(i) = 2i+1 • HEAPIFY-DOWN(H,i) • n = H.size • while (LeftChild(i)<=n and H[i] > H[LeftChild(i)]) • or (RightChild(i)<=n and H[i] > H[RightChild(i)]) do • if (H[LeftChild(i)] < H[RightChild(i)]) then • j = LeftChild(i) • else • j = RightChild(i) • swap entries H[i] and H[j] • i = j

  8. A refresher on sorting Heap - running times: • Use: priority queue – a datastructure that supports: • extract-min • add key • change key value

  9. A refresher on sorting HeapSort • HEAPSORT(A[1…n]) • H = BUILD_HEAP(A[1…n]) • for i=1 to n do • A[i] = EXTRACT_MIN(H) • BUILD_HEAP(A[1…n]) • initially H = ; • for i=1 to n do • ADD(H,A[i]) • EXTRACT_MIN(H) • min = H[1] • H[1] = H[H.size] • H.size-- • HEAPIFY_DOWN(H,1) • RETURN min Note (more efficient BUILD_HEAP): A different implementation of BUILD_HEAP runs in time O(n).

  10. Related datastructures

  11. A lower-bound on sorting: (n log n) Every comparison-based sort needs at least (n log n) comparisons, thus it’s running time is (n log n).

  12. Sorting faster than O(n log n) ? We know: Every comparison-based sort needs at least (n log n) comparisons. Can we possibly sort faster than O(n log n) ?

  13. Sorting faster than O(n log n) ? RadixSort – a non-comparison based sort. Idea: First sort the input by the last digit.

  14. Sorting faster than O(n log n) ? RadixSort – a non-comparison based sort. • RADIXSORT(A) • d = length of the longest element in A • for j=1 to d do • COUNTSORT(A,j) // a stable sort to sort A • // by the j-th last digit • COUNTSORT (A,j) • let B[0..9] be an array of (empty) linked-lists • n = A.length • for i=0 to n-1 do • let x be the j-th last digit of A[i] • add A[i] at the end of the linked-list B[x] • copy B[1] followed by B[2], then B[3], etc. to A Running time ?

More Related