Implementation and Runtime Analysis of Insertion Sort Algorithm
Implementation and Runtime Analysis of Insertion Sort Algorithm
Implementation and Runtime Analysis of Insertion Sort Algorithm
Introduction: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list)
one item at a time.[1]
Let’s have a look at insertion sort algorithm:
1. insertionSort(array)
2. mark first element as sorted
3. for each unsorted element X
4. 'extract' the element X
5. for j <- lastSortedIndex down to 0
6. if current element j > X
7. move sorted element to the right by 1
8. break loop and insert X here
9. end insertionSort [2]
Let us loop for i = 1 (second element of the array) to 4 (last element of the array)
2)i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
3)i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position
ahead of their current position.
5, 11, 12, 13, 6
4)i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of
their current position.
5, 6, 11, 12, 13 [3]
Running time is an important thing to consider when selecting a sorting algorithm since efficiency is often
thought of in terms of speed. Insertion sort has an average and worst-case running time of O(n2)O(n^2)O(n2).
[4]
Methodology:
"""
@author: 18511 111
"""
import numpy as np
import time
start_time=time.clock()
def insertionSort(arr):
for j in range(1,len(arr)):
key=arr[j]
i=j-1
arr[i+1]=arr[i]
i-=1
arr[i+1]=key
#arr=[55,2,3,31,19,21,10,25,67,45,89,56,21,324,13,25,70,80,90,60,55,97,98,99,109]
arr=np.random.randint(low=1,high=10000,size=100)
insertionSort(arr)
for k in range(len(arr)):
print("%d" % arr[k])
time=time.clock() - start_time
print(time)
At 1st we have imported numpy library.Here in this code, we have used time.clock() to measure the execution
time of this code and assigned it to start_name.We have used random.randit() function to generate a random
series of data upto 10000. We have used time=time.clock() - start_time to generate total runtime.
"""
"""
import numpy as np
import time
start_time=time.clock()
def insertionSort(arr):
for j in range(1,len(arr)):
key=arr[j]
i=j-1
arr[i+1]=arr[i]
i-=1
arr[i+1]=key
steps=100
timeCount=np.zeros(steps)
for i in range(steps):
start_time=time.clock()
arraySize=25*i
arr=np.random.randint(low=1,high=10000,size=arraySize)
insertionSort(arr)
timeCount[i]=time.clock()-start_time
plt.plot(timeCount)
At 1st we have imported numpy library.Here in this code, we have used time.clock() to measure the execution
time of this code and assigned it to start_name.We have used random.randit() function to generate a random
series of data upto 10000. We have used time=time.clock() - start_time to generate total runtime.We have used
plt.plot(timeCount) to plot the time that was counted.
Data Analysis: When the 1st code was run ,we got total runtime.
When the 2nd code was run,we got a graph:
Here we got the graph that we plotted.This graph shows how sorting time is changing with the change of array
size.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 other sorting algorithms.It exhibits a good
performance when dealing with a small list.It’s complexity shows that other sorting systems can be more
useful than this sorting system.It does take a lot of time with big list.
Reference:
[1] https://en.wikipedia.org/wiki/Insertion_sort
[2] https://www.programiz.com/dsa/insertion-sort
[3] https://www.geeksforgeeks.org/insertion-sort/
[4] https://brilliant.org/wiki/insertion/