Heap sort, Data Structure & Algorithms

Assignment Help:

We will start by defining a new structure called Heap. Figure 3 illustrates a Binary tree.

1345_Heap Sort.png

Figure: A Binary Tree

A complete binary tree is said to assure the 'heap condition' if the key of each node is greater than or equal to the key in its children. Therefore the root node will have the biggest key value.

Trees can be represented such as arrays, by first numbering the nodes (beginning from the root) from left to right. Then the key values of the nodes are assigned to array positions whose index is specified by the number of the node. For example, the corresponding array is depicted in Figure

444_Heap Sort.png

                                  Figure: Array for the binary tree of figure

The relationships of a node can also be found out from this array representation. If a node is at position of j, its children will be at positions 2j & 2j + 1. Its parent will be at position +J/2+.

Assume the node M. It is at position 5. Therefore, its parent node is at position

5/2+ = 2 that means the parent is R. Its children are at positions 2 × 5 & (2 × 5) + 1, that means 10 + 11 respectively that means E & I are its children.

A Heap is a complete binary tree, wherein each node satisfies the heap condition, represented as an array.

Now we will study the operations possible over a heap and see how these can be combined to produce a sorting algorithm.

The operations on a heap work into 2 steps.

1. The needed node is inserted/deleted/or replaced.

2. The above operation may cause violation of the heap condition thus the heap is traversed and modified to rectify any such kind of violations.

Examples: Assume the insertion of a node R in the heap 1.

1. Initially R is added as the right child of J & given the number 13.

2. However, R > J. hence, the heap condition is violated.

3. Move R up to position six & move J down to position thirteen.

4. R > P. thus, the heap condition is violated still.

5. Swap R & P.

4. Now the heap condition is satisfied by all nodes to get the heap of Figure

629_Heap Sort1.png

624_Heap Sort2.png

Figure: A Heap

This algorithm is guaranteed to sort n elements within (n log2n) time.

First we will see two methods of heap construction and then elimination in order from the heap to sort the list.

1. Top down heap construction

           • Add items into an initially empty heap, satisfying the heap condition at all steps.

2. Bottom up heap construction

            • Build a heap along the items in the order presented.

            • From the right most nodes modify to satisfy the heap condition. We will reveal this with an example.

 Example: construct a heap of the following using top down approach for heap construction.

                                        PROFESSIONAL

Figure illustrates different steps of the top down construction of the heap.

888_Heap Sort3.png

Figure: Heap Sort (Top down Construction)

Example: The input file is (2,3,81,64,4,25,36,16,9, 49). While the file is interpreted as a binary tree, it results in Figure. Another Figure shows the heap.

666_Heap Sort4.png

Figure: A Binary tree                   Figure: Heap of figure

Given figure illustrates several steps of the heap of above Figure as the sorting takes place.


Related Discussions:- Heap sort

State the range of operation of abstract data type, State the range of oper...

State the range of operation of ADT Operations of the Range of T ADT includes following, where a, b ∈ T and r and s are values of Range of T: a...b-returns a range value (an

Explain backtracking, Explain Backtracking The  principal idea is to co...

Explain Backtracking The  principal idea is to construct solutions single component  at a time  and evaluate such  partially constructed candidates as follows. If a partiall

Binry trees, Build a class ?Node?. It should have a ?value? that it stores ...

Build a class ?Node?. It should have a ?value? that it stores and also links to its parent and children (if they exist). Build getters and setters for it (e.g. parent node, child n

Structures for complete undirected graphs, Q. Draw  the structures of compl...

Q. Draw  the structures of complete  undirected  graphs  on  one,  two,  three,  four  and  five vertices also prove that the number of edges in an n vertex complete graph is n(n-1

Grounded header link list and a circular header link list, What is the diff...

What is the difference between a grounded header link list and a circular header link list? A header linked list is a linked list which always having a special node, known as t

State the painter algorithm, Painter's Algorithm As the name suggests, ...

Painter's Algorithm As the name suggests, the algorithm follows the standard practice of a painter, who  would paint the background (such as a backdrop) first, then the major d

Addressing modes, Compare zero-address, one-address, two-address, and three...

Compare zero-address, one-address, two-address, and three-address machines by writing programs to compute: Y = (A – B X C) / (D + E X F) for each of the four machines. The inst

Post order traversal, Post order traversal: The children of node are vi...

Post order traversal: The children of node are visited before the node itself; the root is visited last. Each node is visited after its descendents are visited. Algorithm fo

Explain class p problems, Explain class P problems Class  P  is  a  cla...

Explain class P problems Class  P  is  a  class  of  decision  problems  that  can  be  solved  in  polynomial time  by(deterministic) algorithms. This class of problems is kno

Explain the rgb model, RGB Model The RGB model is based on the assumpti...

RGB Model The RGB model is based on the assumption that any desired shade of colour can be obtained by mixing the correct amounts of red, green, and blue light. The exact hues

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd