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

Implementation and Runtime Analysis of Merge Sort VS Insertion Sort Algorithm

This document analyzes and compares the runtime of merge sort and insertion sort algorithms. Code is provided to implement both algorithms on randomly generated lists of varying sizes. The runtime is plotted in a graph showing that merge sort scales better, with an asymptotic runtime of O(n log n), compared to insertion sort which exhibits O(n^2) behavior. Based on the analysis, merge sort is concluded to be more efficient than insertion sort for sorting large lists.

Uploaded by

Eayashen Arafat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Implementation and Runtime Analysis of Merge Sort VS Insertion Sort Algorithm

This document analyzes and compares the runtime of merge sort and insertion sort algorithms. Code is provided to implement both algorithms on randomly generated lists of varying sizes. The runtime is plotted in a graph showing that merge sort scales better, with an asymptotic runtime of O(n log n), compared to insertion sort which exhibits O(n^2) behavior. Based on the analysis, merge sort is concluded to be more efficient than insertion sort for sorting large lists.

Uploaded by

Eayashen Arafat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ANALYSIS AND DESIGN OF ALGORITHM LAB

Implementation and Runtime Analysis of Merge Sort VS Insertion Sort


Algorithm.
Introduction: Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list into
several sub-lists until each sublist consists of a single element and merging those sublists in a manner that
results into a sorted list. [1]It is an efficient, general-purpose, comparison-based sorting algorithm. Most
implementations produce a stable sort, which means that the order of equal elements is the same in the input
and output.[2]
Merge sort works as follows:
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order. [3]

Methodology:

Here is the code:

"""

@author: 18511 111

"""

import numpy as np

import time

import matplotlib

import matplotlib.pyplot as plt

from pylab import*

plt.close('all')

def InsertionSort(A):

for y in range(1,len(A)):

key=A[y]

x=y-1

while(x>=0 and key>A[x]):


A[x+1]=A[x]

x-=1

A[x+1]=key

def MergeSort(B):

if len(B)>1:

mid=len(B)//2

left=B[:mid]

right=B[mid:]

MergeSort(left)

MergeSort(right)

x=y=z=0

while x<len(left) and y<len(right):

if left[x] < right[y]:

B[z]=left[x]

x=x+1

else:

B[z]=right[y]

y=y+1

z=z+1

while x<len(left):

B[z]=left[x]

x=x+1

z=z+1

while y<len(right):

B[z]=right[y]

y=y+1
z=z+1

steps=100

time_count=np.zeros(steps)

for x in range(steps):

start_time=time.process_time()

A_size=20*x

A=np.random.randint(low=100,high=1000,size=A_size)

InsertionSort(A)

time_count[x]=time.process_time()-start_time

plt.plot(time_count)

time_count=np.zeros(steps)

for x in range(steps):

start_time=time.process_time()

A_size=20*x

A=np.random.randint(low=100,high=1000,size=A_size)

MergeSort(A)

time_count[x]=time.process_time()-start_time

plt.plot(time_count)

Data Analysis: When the run we got this graph:


This graph shows sorting time difference between insertionsort and mergesort with the change of array size.
As we can see the time complexity for merge sort is O(nlogn).It’s also same for worst cases. And for insertion
for this graph has complexity of two as it we know.If we give bigger input,the code will take more time to run
and the line will increase rapidly.

Conclusion: Insertion sort does not perform so well compared to merge sort.Insertion sort exhibits a good
performance when dealing with a small list. It does take a lot of time with big list.Whereas merge sort works
perfectly fine with big list.We can say merge sort is more efficient than insertion sort.

Reference:
[1] https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/
[2] https://en.wikipedia.org/wiki/Merge_sort
[3] https://www.tutorialspoint.com/data_structures_algorithms/merge_sort_algorithm.htm

You might also like