0% found this document useful (0 votes)
32 views

Heap sort

Uploaded by

Trap Trap
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Heap sort

Uploaded by

Trap Trap
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Independent work

• Students: Taleh Mirzaliyev, Muhammed Poladov, Ali Mammadov


• Faculty: High Technologies And Innovative Engineering
• Specialization: Computer Engineering
• Qroup: 232k Ing
• Subject: Basics of progamming
• Topic: Heap sort
• Teacher: Parvin Huseynov
What is Heap Sort
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar
to the selection sort where we first find the minimum element and place the minimum element at
the beginning. Repeat the same process for the remaining elements.

Important points:
• Heap sort is an in-place algorithm.
• Its typical implementation is not stable, but can be made stable (See this)
• Typically 2-3 times slower than well-implemented QuickSort. The reason for slowness is a lack of
locality of reference.
Advantages of heapsort:
• Efficiency – The time required to perform Heap sort increases logarithmically while other
algorithms may grow exponentially slower as the number of items to sort increases. This sorting
algorithm is very efficient.

• Memory Usage – Memory usage is minimal because apart from what is necessary to hold the
initial list of items to be sorted, it needs no additional memory space to work

• Simplicity – It is simpler to understand than other equally efficient sorting algorithms because it
does not use advanced computer science concepts such as recursion.
Disadvantages and applications of Heap
Sort:
Disadvantages of Heap Sort:
• Costly: Heap sort is costly.
• Unstable: Heap sort is unstable. It might rearrange the relative order.
• Efficient: Heap Sort are not very efficient when working with highly complex data.

Applications of HeapSort:
• Heapsort is mainly used in hybrid algorithms like the IntroSort.
• Sort a nearly sorted (or K sorted) array
• k largest(or smallest) elements in an array
What is meant by Heapify?
Algorithm for Heapify:

Heapify is the process of creating a heap data


structure from a binary tree represented using an
array. It is used to create Min-Heap or Max-heap.
Start from the last index of the non-leaf node whose
index is given by n/2 – 1. Heapify uses recursion.
Relationship between Array Indexes and Tree
Elements
A complete binary tree has an interesting property that we can use to find the children and parents
of any node.
If the index of any element in the array is i, the element in the index 2i+1 will become the left child
and element in 2i+2 index will become the right child. Also, the parent of any element at index i is
given by the lower bound of (i-1)/2
Let's test it out,

Understanding this mapping of array indexes to tree


positions is critical to understanding how the Heap Data
Structure works and how it is used to implement Heap Sort
What is Heap Data Structure?

Heap is a special tree-based data structure. A


binary tree is said to follow a heap data structure if

• It is a complete binary tree


• All nodes in the tree follow the property that
they are greater than their children i.e. the
largest element is at the root and both its
children and smaller than the root and so on.
Such a heap is called a max-heap. If instead, all
nodes are smaller than their children, it is called
a min-heap
How to "heapify" a tree
Starting from a complete binary tree, we can modify it to become a Max-Heap by running a
function called heapify on all the non-leaf elements of the heap.

Since heapify uses recursion, it can be difficult to grasp. So let's first think about how you would
heapify a tree with just three elements.
How to "heapify" a tree

The example right here shows two


scenarios - one in which the root is the
largest element and we don't need to
do anything. And another in which the
root had a larger element as a child and
we needed to swap to maintain max-
heap property.
How to heapify root element when its subtrees are
already max heaps

Now let's think of another scenario in which there is more than one level.

The top element isn't a max-heap but all the sub-trees are max-heaps.
How to heapify root
element when its
subtrees are already
max heaps

Thus, to maintain the max-heap property


in a tree where both sub-trees are max-
heaps, we need to run heapify on the
root element repeatedly until it is larger
than its children or it becomes a leaf
node.
How to heapify root element when its subtrees are already
max heaps
We can combine both these conditions in one heapify function as

This function works for both the base


case and for a tree of any size. We can
thus move the root element to the
correct position to maintain the max-
heap status for any tree size as long as
the sub-trees are max-heaps.
Heapify in C

The swap function is defined to swap the positions of


two elements in an array. It takes two integer pointers (a
and b) as arguments and uses a temporary variable temp
to perform the swap.

The heapify function is defined to heapify the array. It takes


three arguments: the array arr, the size of the heap n, and the
index of the current node i. Inside the heapify function, three
variables are initialized: largest, left, and right. largest is
initialized with the index i, left is calculated as 2 * i + 1, and
right is calculated as 2 * i + 2. These variables represent the
indices of the current node, its left child, and its right child in
the heap.
Heapify in C

The if statements compare the values of the left child


and the right child with the current node to find the
largest among them. If the left child is larger and within
the bounds of the heap (left < n) then largest is updated
to left. Similarly, if the right child is larger and within the
bounds of the heap (right < n), then largest is updated to
right.
Heapify in C
The heapSort function takes two
arguments: the array arr to be sorted
and the size of the array n.

The first loop for (int i = n / 2 - 1; i >= 0;


i--) is responsible for building the max
heap. It starts from the middle index of
the array (rounded down using integer
division) and goes up to the first index
(0). For each iteration, it calls the
heapify function to ensure that the
subtree rooted at index i satisfies the
max heap property.
Heapify in C
After the max heap is built, the second loop for (int i =
n - 1; i >= 0; i--) performs the heap sort operation. It
starts from the last index of the array and goes down
to the first index (0). In each iteration, it swaps the
root element (arr[0]) with the current element (arr[i])
which represents the last unsorted element.

After the swap, the heapify function is called on the


root element (arr[0]) to restore the max heap
property. This is done with the arguments arr, i (which
represents the size of the unsorted portion of the
array), and 0 (the index of the root).

The second loop continues until all elements are


sorted in ascending order. At the end of the loop, the
original array arr is sorted in non-decreasing order.
Heapify in C
The printArray function is defined to print the
elements of an array. It takes two arguments: the
array arr and the size of the array n.

Inside the printArray function, a for loop is used to


iterate over each element of the array. It starts from
index 0 and goes up to index n - 1. In each iteration,
the printf function is called to print the value of the
current element, followed by a space.

After the for loop, a newline character (\n) is printed


using the printf function. This ensures that the next
output appears on a new line.

The main function is the entry point of the program.

In the main function, an array arr is defined with the


values {1, 12, 9, 5, 6, 10}.
Heapify in C
The variable n is assigned the size of the array
arr. This is calculated by dividing the size of the
whole array (in bytes) by the size of a single
element (in bytes) using the sizeof operator.

The heapSort function is called, passing the


array arr and its size n as arguments. This
performs the Heap Sort algorithm on the array.

After the heapSort function completes, the


printf function is used to print the message
"Sorted array is" followed by a newline
character.

Finally, the printArray function is called to print


the sorted array arr along with its size n.

You might also like