Anda di halaman 1dari 3

Heap Sort

1. Sorting

Sorting is the process of placing elements from a collection in some kind of Order.

2. Types of sorts :

Many Types of sorts. We’ll discuss heap Sort.

3. Meaning of heap in real life: Shape, like a mountain

4. heap in computer science?:Assume that data is in the form of group like heap(In “tree
Data structure” data is in the form of heap)

5.tree

non-premitive DS

non-linear DS

Tree

data is in the form of hierarchy(like Parent, Child relations), explain parent and child with
example..

Data is stored in node

Attributes of Tree

Root node: top most element tree

Leaf node: Leafs on last layer:

Types of tree:

many types tree, but we are implementing heap sort using BINARY TREE

Why Tree:

To store information that naturally forms a hierarchy.

Binary Tree:

A tree whose elements have at most 2 children is called a binary tree. Since each element in a
binary tree can have only 2 children, we typically name them the left and right child.

Tree node have three part(data, pointer to left child, pointer to right child)
(Give explanation with diagram)

Complete binary tree

A complete binary tree is just like a full binary tree, but with two major differences

1. Every level must be completely filled


2. All the leaf elements must lean towards the left.
3. The last leaf element might not have a right sibling i.e. a complete binary tree
doesn’t have to be a full binary tree.

Binary Tree Representation in C:


A Tree node contains following parts.
1. Data
2. Pointer to left child
3. Pointer to right child

Heap Sort: Heap sort is a comparison based sorting technique based on Binary Heap
data structure.

A Binary Heap is a Complete Binary Tree where items are stored in a special order
such that value in a parent node is greater than the values in its two children nodes.

There are two types of Heap

MAX Heap:Every parent is Greater or equal then its Childs

MIN Heap:Every parent is Smaller or equal then its Childs

Representation
Binary Tree, it can be easily represented as array and array based representation is space
efficient. If the parent node is stored at index i, the left child can be calculated by 2 * i + 1 and
right child by 2 * i + 2 (assuming the indexing starts at 0).

(Explain Heap Sort with Diagram Example)

Heap Sort Algorithm for sorting in descending order:

Start

1: Put Data in Array

2: Put Data in Binary Tree.


3. Build a max heap
4: Replace the Root with the last leaf of the heap
5: Store the last element in the Array and delete the last leaf
6. Repeat from step 3 to step 5 until the 1 element remains in tree.

Stop

Note: the Heap data structure itself is not usually used.

Anda mungkin juga menyukai