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

Insertion Sort: Algorithm, Working, Example, and Analysis

Insertion sort is a simple sorting algorithm that works by incrementally building a sorted array from left to right. It iterates through the array, inserting each element into its sorted position by shifting larger elements to the right. While its worst-case performance is O(n^2), it is more efficient on partially sorted lists by requiring fewer element shifts. It has an in-place implementation using a single while loop, making it space efficient with O(1) memory usage.

Uploaded by

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

Insertion Sort: Algorithm, Working, Example, and Analysis

Insertion sort is a simple sorting algorithm that works by incrementally building a sorted array from left to right. It iterates through the array, inserting each element into its sorted position by shifting larger elements to the right. While its worst-case performance is O(n^2), it is more efficient on partially sorted lists by requiring fewer element shifts. It has an in-place implementation using a single while loop, making it space efficient with O(1) memory usage.

Uploaded by

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

INSERTION SORT

ALGORITHM, WORKING, EXAMPLE, AND ANALYSIS


Insertion Sort
 It is a very simple method to sort the number in an increasing or decreasing order.
 Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
 This method follows the incremental method.
 The numbers, which are needed to be sorted, are known as keys.
 It has one of the simplest implementation.
 Insertion sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it
increases its efficiency.
 It is better than selection sort and bubble sort algorithms.
Insertion Sort Algorithm
ALGORITHM: INSERTION SORT (A)
1. For k ← 2 to length [A]
2. Do key ← A[k]
3. i = k - 1
4. while i > 0 and A[i] > key
5. do A[i+1] ← A[i]
6. i = i - 1
7. A[i+1] ← key
Another Example
 12, 11, 13, 5, 6
 Let us loop for i = 1 (second element of the array) to 4 (last element of the array)
 i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
 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
 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
 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
Another Example
Analysis of Insertion Sort
As we mentioned above that insertion sort is an efficient sorting algorithm, as it does not
run on preset conditions using for loops, but instead it uses one while loop, which avoids
extra steps once the array gets sorted.

Even though insertion sort is efficient, still, if we provide an already sorted array to the
insertion sort algorithm, it will still execute the outer for loop, thereby requiring n steps to
sort an already sorted array of n elements, which makes its best case time complexity a
linear function of n.

Worst Case Time Complexity [ Big-O ]: O(n ) 2

Best Case Time Complexity [Big-omega]: O(n)


Average Time Complexity [Big-theta]: O(n ) 2

Space Complexity: O(1)

You might also like