1 / 8

Heapsort: Efficient In-Situ Sorting Algorithm

Heapsort is a two-phase sorting algorithm that uses a heap data structure to efficiently order a sequence of numbers in ascending order. The algorithm has a time complexity of O(n*log(n)) and is ideal for sorting large datasets.

lkeller
Download Presentation

Heapsort: Efficient In-Situ Sorting Algorithm

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. 6.3.1 Heapsort Idea:two phases: 1. Construction of the heap 2. Output of the heap For ordering number in an ascending sequence: use a Heap with reverse order: the maximum number should be at the root (not the minimum). Heapsort is an in-situ-Procedure

  2. Remembering Heaps: change the definition Heap with reverse order: • For each node x and each successor y of x the following holds: m(x)  m(y), • left-complete, which means the levels are filled starting from the root and each level from left to right, • Implementation in an array, where the nodes are set in this order (from left to right) .

  3. Second Phase: Heap 2. Output of the heap: take n-times the maximum (in the root, deletemax) and exchange it with the element at the end of the heap.  Heap is reduced by one element and the subsequence of ordered elements at the end of the array grows one element longer. cost: O(n log n). Heap Ordered elements Ordered elements

  4. First Phase: 1. Construction of the Heap: simple method: n-times insert Cost: O(n log n). making it better: consider the array a[1 … n ] as an already left-complete binary tree and let sink the elements in the following sequence ! a[n div 2] … a[2] a[1] (The elements a[n] … a[n div 2 +1] are already at the leafs.) HH The leafs of the heap

  5. Formally: heap segment an array segment a[ i..k ] ( 1 ik <=n ) is said to be a heapsegment when following holds:         for all j from {i,...,k}     m(a[ j ]) m(a[ 2j ])     if 2j k and   m(a[ j ]) m(a[ 2j+1])  if 2j+1 k If a[i+1..n] is already a heap segment we can convert a[i…n] into a heap segment by letting a[i] sink.

  6. Cost calculation Be k = [log n+1]+ - 1. (the height of the complete portion of the heap) cost: For an element at level j from the root: k – j. alltogether: {j=0,…,k} (k-j)•2j = 2k•{i=0,…,k} i/2i =2 • 2k = O(n).

  7. advantage: The new construction strategy is more efficient ! Usage: when only the m biggest elements are required: 1. construction in O(n) steps. 2. output of the m biggest elements in O(m•log n) steps. total cost: O( n + m•log n).

  8. Addendum: Sorting with search trees Algorithm: • Construction of a search tree (e.g. AVL-tree) with the elements to be sorted by n insert opeartions. • Output of the elements in InOrder-sequence.  Ordered sequence. cost: 1. O(n log n) with AVL-trees, 2. O(n). in total: O(n log n). optimal!

More Related