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

DAA-1.2 -

Uploaded by

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

DAA-1.2 -

Uploaded by

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

Taken from- Codedamn

Insertion sort is a simple sorting algorithm that builds the final


sorted array (or list) one item at a time by comparisons. It is
much less efficient on large lists than more advanced algorithms
such as quicksort, heapsort, or merge sort.

Insertion sort is a sorting algorithm that places an


unsorted element at its suitable place in each
iteration

Insertion sort is based on the idea that one element from the
input elements is consumed in each iteration to find its correct
position i.e, the position to which it belongs in a sorted array

Insertion sort works by inserting elements from an


unsorted array into a sorted subsection of the
array, one item at a time.

Working of Insertion sort Algorithm

A step-by-step explanation of the sorting process is as follows:


Insertion sort works by iterating through a list of items, comparing
each element to the ones that come before it, and inserting it into
the correct position in the list. This process is repeated until the
list is fully sorted. To illustrate this process, let’s use the example
of a list of numbers that we want to sort in ascending order: [22, 6,
15, 48, 1].
• Compare the first element, 22, to the second element, 6.
Since 15 is smaller than 22, we swap them, resulting in the
list [6, 22, 15, 48, 1].
• Compare the second element, 22, to the third element, 15.
Since 15 is smaller than 22, we swap them, resulting in the
list [6, 15, 22, 48, 1].
• Compare the third element, 22, to the fourth element, 48.
Since 48 is larger than 22, no swap is necessary.
• Compare the fourth element, 48, to the fifth element, 1. Since
1 is smaller than 48, we swap them, resulting in the list [6,
15, 22, 1, 48].
• Compare the third element, 22, to the fourth element, 1. Since
1 is smaller than 22, we swap them, resulting in the list [6,
15, 1, 22, 9].
• Compare the second element, 15, to the third element, 1.
Since 1 is smaller than 15, we swap them, resulting in the list
[6, 1, 15, 22, 48].
• Compare the first element, 6, to the second element, 1. Since
1 is smaller than 6, we swap them, resulting in the final
sorted list of [1, 6, 15, 22, 48].
Here is an illustration for you to have a better understanding of the
sorting method.
Advantages of Insertion sort:
• Simple to understand and implement
• Efficient for small lists
• Can be used with partially sorted lists
• Provides stability, since it maintains the order of
elements with the same value.
Disadvantages of Insertion sort:
• Inefficient for large lists (time complexity
increases exponentially with the size of the list)
• Not suitable for lists with many duplicate
elements
• Not adaptable to different data structures (works best with arrays)

Applications of insertion sort


Examples of where insertion sort is used in practice: Insertion sort
is used in a variety of applications, including:
• Sorting a hand of playing cards according to the
numbers and colors.
• Sorting shirts in a closet according to color and
size by a tailor.
• Sorting a list of contact information in
alphabetical order.
• Sorting the products on an e-commerce website
from high to low price or low to high price.
• Sorting items in inventory systems by item names.
• Sorting a list of documents from high-priority to
low-priority.
• Sorting elements in graphical user interfaces for a
table or spreadsheet.
• Sorting data in databases by a specific field or
attribute.
1. Time Complexity

Best Case Complexity - It occurs when there is no


sorting required, i.e. the array is already sorted. The
best-case time complexity of insertion 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 insertion sort is O(n2).
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
insertion sort is O(n2).
Insertion sort algorithm
Here is the insertion sort algorithm implemented in
Python:

def insert_srt(a):
for k in range(1, len(a)):
curr_val = a[k]
x = k - 1
while x >= 0 and a[x] >
curr_val:
a[x+1] = a[x]
x -= 1
a[x+1] = curr_val
return a
Code language: Python (python)
To use the algorithm, call the insertion_sort
function and pass in the list that you want to sort as an
argument. For example:
srt_arr=[22,6,15,48,1]
print("List before insertion sort\
n",srt_arr)
sort_list = insert_srt(srt_arr)
print("List after insertion sort\
n",sort_list) Code language: Python
(python)
Insertion Sort Time Complexity
For a general explanation of what time complexity is,
visit this page.
For a more thorough and detailed explanation of
Insertion Sort time complexity, visit this page.
Insertion Sort sorts an array of n values.
On average, each value must be compared to about n2
other values to find the correct place to insert it.
Insertion Sort must run the loop to insert a value in its
correct place approximately n
times.
We get time complexity for Insertion Sort:
O(n2⋅n)=O(n2)––––––––––––––
The time complexity for Insertion Sort can be
displayed like this:

You might also like