0% found this document useful (0 votes)
136 views19 pages

Group 1 - Heap Sort and Timsort

Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure. It has average, best, and worst case time complexities of O(n log n). It works by first constructing a max heap from the input array and then removing the maximum element and placing it in the sorted position in each step, re-heapifying the remaining elements. Timsort is a hybrid stable sorting algorithm that uses insertion sort for small sublists and merge sort for merging the sorted sublists. It has time complexities of O(n) in the best case, O(n log n) on average and worst cases, and is used as the default sorting algorithm in Python.

Uploaded by

Mariebeth Seno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views19 pages

Group 1 - Heap Sort and Timsort

Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure. It has average, best, and worst case time complexities of O(n log n). It works by first constructing a max heap from the input array and then removing the maximum element and placing it in the sorted position in each step, re-heapifying the remaining elements. Timsort is a hybrid stable sorting algorithm that uses insertion sort for small sublists and merge sort for merging the sorted sublists. It has time complexities of O(n) in the best case, O(n log n) on average and worst cases, and is used as the default sorting algorithm in Python.

Uploaded by

Mariebeth Seno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

HEAP SORT

INTRODUCTION

Heap Sort is a popular and efficient sorting algorithm in computer programming. It is a


comparison-based algorithm based on the binary heap (complete binary tree) data structure,
where every level of the tree contains values that are stored in a defined and definitive order,
such that the value in the parent nodes must be greater or smaller than those in the two
children nodes. Two types of heap sort are Max heap where parent node is higher than its
child node, and Min heap if the parent node is smaller than the child node. The overall time
complexity of Heap Sort is O(nLogn).

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.

Complexity of Heap Sort


Heap Sort Complexity
Time Complexity
Average O(nlog n)
Best O(nlog n)
Worst O(nlog n)
Stability No
Complexity O1

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 1 - Construct a Binary Tree with given list of unsorted elements.

Step 2 - Transform the Binary Tree into Max Heap.

Step 3 - Delete the root element from Max Heap using Heapify method.

Step 4 - Put the deleted element into the Sorted list.

Step 5 - Repeat the same until Max Heap becomes empty.

Step 6 - Display the sorted list.

EXAMPLE SIMULATION OF HEAP SORT:

https://www.algostructure.com/sorting/heapsort.php

VIDEO TUTORIAL OF HEAP SORT:

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;

printf("\n Enter number of elements : ");


scanf("%d", &number);
printf("\n Enter the numbers to be sorted : ");

for (i = 0; i < number; i++)


scanf("%d", &heap[i]);

for (i = 1; i < number; i++)


{
c = i;
do
{
root = (c - 1) / 2;
if (heap[root] < heap[c])
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}
printf("\nHeap array : \n");

for (i = 0; i < number; i++)


printf(" %d ", heap[i]);

for (j = number - 1; j >= 0; j--)


{
temp = heap[0];
heap[0] = heap[j];
heap[j] = temp;
root = 0;

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

Case Time Complexity

Best Case O(n)

Average Case O(n log n)

Worst Case O(n log n)

◦ 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 2 - Consider the size of run, either 32 or 64.

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.

Step 5 - Double the size of merged sub-arrays after every iteration.

EXAMPLE SIMULATION OF TIMSORT:

https://www.chrislaux.com/timsort.html

VIDEO TUTORIAL OF TIMSORT:

https://www.youtube.com/watch?v=_dlzWEJoU7I
EXAMPLE:
Flowchart of Timsort
Timsort Code

#include<stdio.h>
const int RUN = 32;

int min(int a, int b)


{

if(a<b)
return a;
else
return b;
}

void insertionSort(int a[], int beg, int end)


{
int i, j, temp;
for (i = beg + 1; i <= end; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j])


{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int LeftArray[n1], RightArray[n2];

for (int i = 0; i < n1; i++)


LeftArray[i] = a[beg + i];

for (int j = 0; j < n2; j++)


RightArray[j] = a[mid + 1 + j];
i = 0;
j = 0;
k = beg;
while (i < n1 && j < n2)
{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
void timSort(int a[], int n)
{

for (int i = 0; i < n; i+=RUN)


insertionSort(a, i, min((i+RUN-1), (n-1)));

for (int size = RUN; size < n; size = 2*size)


{
for (int beg = 0; beg < n; beg += 2*size)
{

int mid = beg + size - 1;


int end = min((beg + 2*size - 1),(n-1));
if(mid < end)
merge(a, beg, mid, end);
}
}
}
void printArr(int a[], int n)

{
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy