Group 1 - Heap Sort and Timsort
Group 1 - Heap Sort and Timsort
INTRODUCTION
In learning how to write the heap sort algorithm requires knowledge of two types of data
structures - arrays and trees. The initial set of numbers that we want to sort is stored in an
array e.g. [10, 3, 76, 34, 23, 32] and after sorting, we get a sorted array [3,10,23,32,34,76]. In
addition, heap sort is a comparison-based sorting technique based on Binary Heap data
structure. It is similar to selection sort where we first find the minimum element and place the
minimum element at the beginning. We repeat the same process for the remaining elements.
It also applies the divide and conquer algorithm. It creates the subarrays and compares them
to form a specific order, which could be ascending or descending. In heap sort, we use a heap
tree data structure to hold the elements of the array.
A heap sorts have time complexities of 0(nlog n) for average, best, and worst cases.
To understand the reason behind this, first thing to understand is that the height of a complete
binary tree containing a number of elements is equivalent to “log n”.
To completely “heapify” an element with subtrees that are already max-heaps, first visualize
the “parent” and its “children”. In an element that have max-heaps already, comparing the
element with its left and right and moving it downwards until it reaches the point which the
parent is the largest amongst the three.
In the event that a worst-case scenario has occurred, it would be required to move an element
from the root to the left node creating a multiple of “log (n)” comparisons and swaps.
During sorting, the root element is exchanged with the last element and then “heaptifies” the
root. For every element, it again takes “log n” the worst time as it might need to take the
element from the root to the leaf. And since this repeated for a number of times (n), the
heap_sort step is also “nlog n”.
Unlike a quick sort, a heap sort has a better worst case. And with “introsort”, a balance
between quick sort and heap sort is achieved with the worst-case speed of heapsort with the
average speed of quicksort.
Step-by-step Procedure
Step 3 - Delete the root element from Max Heap using Heapify method.
https://www.algostructure.com/sorting/heapsort.php
https://www.youtube.com/watch?v=2DmK_H7IdTo
EXAMPLE:
Flowchart of Heap Sort
Start
i = array.length ;
root = 0;
False
i--; i > root End
True
maxIndex = i;
True False
i % 2 ==0 i--; array[i] > array[maxIndex]
False True
maxIndex = i;
parentIndex= (i-1) / 2;
array[maxIndex]
False
>
array[minIndex]
True
temp = array[parentIndex];
array[parentIndex] = array[maxIndex];
array[maxIndex] = temp;
SiftDown(array,maxIndex,array.length -1)
Heap Sort Code
#include <stdio.h>
int main()
{
int heap[10], number, i, j, c, root, temp;
do
{
c = 2 * root + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j)
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
printf("\nThe sorted array is : \n");
for (i = 0; i < number; i++)
printf(" %d", heap[i]);
}
Output
References:
https://www.programiz.com/dsa/heap-sort
http://www.btechsmartclass.com/data_structures/heap-sort.html
http://syllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/SortingTool/heap_sort_info.html
https://www.sanfoundry.com/c-program-heap-sort-algorithm/
https://www.c-programming-simple-steps.com/heap-sort.html
TIMSORT
INTRODUCTION
Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort,
designed to perform well on many kinds of real-world data.
It was implemented by Tim Peters in 2002 for use in the Python programming language.
Timsort has been Python's standard sorting algorithm since version 2.3.
Timsort is a stable algorithm and beats every other sorting algorithm in time. It has O(nlogn)
time complexity for worst case unlike quick sort and O(n) for best case scenarios unlike
merge sort and heap sort.
The algorithm finds subsequences of the data that are already ordered (runs) and uses them to
sort the remainder more efficiently.In real world scenarios, most of the times input array is
naturally ordered array hence merge sort and quicksort aren’t the efficient choice. Tim sort
shines when data is ordered and of course when data is random.
Complexity of Timsort
◦ Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of Tim sort is O(n).
◦ Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. The average case time
complexity of Tim sort is O(n log n).
◦ Worst Case Complexity - It occurs when the array elements are required to be sorted
in reverse order. That means suppose you have to sort the array elements in ascending
order, but its elements are in descending order. The worst-case time complexity of
Tim sort is O(n log n).
Step-by-step Procedure
Step 1 - Divide the array into the number of blocks known as run.
Step 3 - Sort the individual elements of every run one by one using insertion sort.
Step 4 - Merge the sorted runs one by one using the merge function of merge sort.
https://www.chrislaux.com/timsort.html
https://www.youtube.com/watch?v=_dlzWEJoU7I
EXAMPLE:
Flowchart of Timsort
Timsort Code
#include<stdio.h>
const int RUN = 32;
if(a<b)
return a;
else
return b;
}
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 40, 12, 31, 27, 25, 8, 1, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
timSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output
References:
https://www.javatpoint.com/tim-sort
https://hackernoon.com/timsort-the-fastest-sorting-algorithm-youve-never-heard-of-36b28417f399