Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 3 Second Phase: 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 Heap Ordered elements

4 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 5 Formally: heap segment an array segment a[ i..k ] ( 1  i  k <=n ) is said to be a heap segment 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 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)2 j = 2 k  {i=0,…,k} i/2 i =2 2 k = O(n).

7 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(mlog n) steps. total cost: O( n + mlog n).

8 8 Addendum: Sorting with search trees Algorithm: 1.Construction of a search tree (e.g. AVL-tree) with the elements to be sorted by n insert opeartions. 2.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!


Download ppt "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."

Similar presentations


Ads by Google