Prerequisite : HeapSortĀ
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 maximum element and place the maximum element at the end. We repeat the same process for remaining element.
We implement Heap Sort here, call it for different sized random lists, measure time taken for different sizes and generate a plot of input size vs time taken.
Python
# Python Code for Implementation and running time Algorithm # Complexity plot of Heap Sort # by Ashok Kajal # This python code intends to implement Heap Sort Algorithm # Plots its time Complexity on list of different sizes Ā
# ---------------------Important Note ------------------- # numpy, time and matplotlib.pyplot are required to run this code import time from numpy.random import seed from numpy.random import randint import matplotlib.pyplot as plt Ā
Ā
# find left child of node i def left(i): Ā Ā Ā Ā return 2 * i + 1 Ā
# find right child of node i def right(i): Ā Ā Ā Ā return 2 * i + 2 Ā
# calculate and return array size def heapSize(A): Ā Ā Ā Ā return len (A) - 1 Ā
Ā
# This function takes an array and Heapyfies # the at node i def MaxHeapify(A, i): Ā Ā Ā Ā # print("in heapy", i) Ā Ā Ā Ā l = left(i) Ā Ā Ā Ā r = right(i) Ā Ā Ā Ā Ā Ā Ā Ā Ā # heapSize = len(A) Ā Ā Ā Ā # print("left", l, "Rightt", r, "Size", heapSize) Ā Ā Ā Ā if l< = heapSize(A) and A[l] > A[i] : Ā Ā Ā Ā Ā Ā Ā Ā largest = l Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā largest = i Ā Ā Ā Ā if r< = heapSize(A) and A[r] > A[largest]: Ā Ā Ā Ā Ā Ā Ā Ā largest = r Ā Ā Ā Ā if largest ! = i: Ā Ā Ā Ā Ā Ā Ā # print("Largest", largest) Ā Ā Ā Ā Ā Ā Ā Ā A[i], A[largest] = A[largest], A[i] Ā Ā Ā Ā Ā Ā Ā # print("List", A) Ā Ā Ā Ā Ā Ā Ā Ā MaxHeapify(A, largest) Ā Ā Ā Ā Ā # this function makes a heapified array def BuildMaxHeap(A): Ā Ā Ā Ā for i in range ( int (heapSize(A) / 2 ) - 1 , - 1 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā MaxHeapify(A, i) Ā Ā Ā Ā Ā Ā Ā Ā Ā # Sorting is done using heap of array def HeapSort(A): Ā Ā Ā Ā BuildMaxHeap(A) Ā Ā Ā Ā B = list () Ā Ā Ā Ā heapSize1 = heapSize(A) Ā Ā Ā Ā for i in range (heapSize(A), 0 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā A[ 0 ], A[i] = A[i], A[ 0 ] Ā Ā Ā Ā Ā Ā Ā Ā B.append(A[heapSize1]) Ā Ā Ā Ā Ā Ā Ā Ā A = A[: - 1 ] Ā Ā Ā Ā Ā Ā Ā Ā heapSize1 = heapSize1 - 1 Ā Ā Ā Ā Ā Ā Ā Ā MaxHeapify(A, 0 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā
# randomly generates list of different # sizes and call HeapSort function elements = list () times = list () for i in range ( 1 , 10 ): Ā
Ā Ā Ā Ā # generate some integers Ā Ā Ā Ā a = randint( 0 , 1000 * i, 1000 * i) Ā Ā Ā Ā # print(i) Ā Ā Ā Ā start = time.clock() Ā Ā Ā Ā HeapSort(a) Ā Ā Ā Ā end = time.clock() Ā
Ā Ā Ā Ā # print("Sorted list is ", a) Ā Ā Ā Ā print ( len (a), "Elements Sorted by HeapSort in " , end - start) Ā Ā Ā Ā elements.append( len (a)) Ā Ā Ā Ā times.append(end - start) Ā
plt.xlabel( 'List Length' ) plt.ylabel( 'Time Complexity' ) plt.plot(elements, times, label = 'Heap Sort' ) plt.grid() plt.legend() plt.show() # This code is contributed by Ashok Kajal |
Output :Ā
Input : Unsorted Lists of Different sizes are Generated Randomly Output : 1000 Elements Sorted by HeapSort in 0.023797415087301488 2000 Elements Sorted by HeapSort in 0.053856713614550245 3000 Elements Sorted by HeapSort in 0.08474737185133563 4000 Elements Sorted by HeapSort in 0.13578669978414837 5000 Elements Sorted by HeapSort in 0.1658182863213824 6000 Elements Sorted by HeapSort in 0.1875901601906662 7000 Elements Sorted by HeapSort in 0.21982946862249264 8000 Elements Sorted by HeapSort in 0.2724293921580738 9000 Elements Sorted by HeapSort in 0.30996323029421546 Complexity PLot for Heap Sort is Given Below