0% found this document useful (0 votes)
11 views13 pages

Al No Content Page No

The document provides an overview of the Insertion Sort algorithm, detailing its process, examples, and pseudocode. It discusses the algorithm's time complexity, advantages such as simplicity and stability, and disadvantages including poor performance on large datasets. The conclusion suggests that while Insertion Sort is useful for small arrays, more efficient algorithms are recommended for larger datasets.

Uploaded by

yashkol44
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)
11 views13 pages

Al No Content Page No

The document provides an overview of the Insertion Sort algorithm, detailing its process, examples, and pseudocode. It discusses the algorithm's time complexity, advantages such as simplicity and stability, and disadvantages including poor performance on large datasets. The conclusion suggests that while Insertion Sort is useful for small arrays, more efficient algorithms are recommended for larger datasets.

Uploaded by

yashkol44
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/ 13

CONTENTS

Serial No Content Page No


1 INSERTION SORT 3
2 INSERTION SORT 4-5
EXAMPLE
3 INSERTION SORT 6-7
ALGORITHMS
4 INSERTION SORT 8
PSEUDOCODE
5 INSERTION SORT 9
COMPLEXITY
6 ADVANTAGES 10
7 DISADVANTAGES 11
8 CONCLUSION 12

9 BIBLIOGRAPHY 13
INSERTION SORT
 Insertion sort is the most simplest form of sorting.
 Insertion sort is a simple sorting algorithm that builds the final
sorted array one item at a time. It iterates through the input array,
removing one element from the input array and finding the correct
position for it within the sorted array, and puts it there. This process
continues until all elements from the input array have been inserted
into the sorted array.
 Insertion sort keeps making the left side of the array sorted until the
whole array is sorted.it sorts the values seen far away and repeatedly
insert unseen values in the array into the left sorted array.
 Insertion Sort works like playing cards in hand. It also has
complexity but not as much as Bubble Sort.
INSERTION SORT EXAMPLE
 In playing cards in hand the first card is already sorted in
the card game ,then select the unsorted cards.
 If the selected unsorted card is greater then the first
card ,it will be placed at the right side ,otherwise it will be
placed at the left side .
 Similarly all unsorted cards are taken and put in their
exact place. This idea is used in the insertion sort.
INSERTION SORT EXAMPLE
 Given array [5, 2, 9, 1, 5, 6]:
 Initial array: [5, 2, 9, 1, 5, 6]
 Step 1: Compare 2 with 5, shift 5 → [2, 5, 9, 1, 5, 6]
 Step 2: 9 is already in place → [2, 5, 9, 1, 5, 6]
 Step 3: Compare 1 with 9, 5, 2, shift them → [1, 2, 5, 9, 5,
6]
 Step 4: Compare 5 with 9, shift 9 → [1, 2, 5, 5, 9, 6]
 Step 5: Compare 6 with 9, shift 9 → [1, 2, 5, 5, 6, 9]
 Sorted array: [1, 2, 5, 5, 6, 9]
INSERTION SORT ALGORITHMS
 Step 1:
If the element is the first element, assume that it is already
stored then it returns 1.
 Step2:

Pick the next element and store is separately in a key.


 Step 3:
Now Compare the key with all elements in a sorted array.
INSERTION SORT ALGORITHMS
 Step 4:
If the element in the sorted array is smaller than the
current element ,then move the next element. Else, shift
generator elements in array towards right.
 Step 5:
Insert the value.
 Step 6:
Repeat until the array is sorted.
INSERTION SORT PSEUDOCODE

1. Function InsertionSort(Array)
2. For i = 1 to length(Array) - 1 // Start from the second element
3. key = Array[i] // Store the current element in key
4. j = i - 1 // Start comparing with the previous element
// Compare key with all elements in the sorted portion of the array
5. While j >= 0 and Array[j] > key
6. Array[j + 1] = Array[j] // Shift element to the right
7. j=j-1 // Move to the next element on the left

8. Array[j + 1] = key // Insert the key at the correct position


9. End For
10. End Function
INSERTION SORT COMPLEXITY
 Time Complexity:
 Best Case: O(n)
This occurs when the input array is already sorted. In this scenario, the outer loop iterates
through the array once (n times), but the inner loop (which does the comparisons and shifts)
barely executes because each element is already in its correct position.
 Average Case: O(n²)
When the input array is in a random order, on average, each element will need to be
compared and potentially shifted with roughly half of the elements in the already sorted
portion. This leads to a nested loop structure where both loops iterate roughly n times,
resulting in an overall time complexity of O(n * n) = O(n²).
 Worst Case: O(n²)
The worst case happens when the input array is sorted in reverse order. In this situation, for
each element, the inner loop has to compare and shift it with all the preceding elements in
the sorted portion. This again leads to the nested loop structure with both loops running
roughly n times, resulting in O(n²) time complexity.
ADVANTAGES
 Simple Implementation: Insertion sort is very easy to understand
and implement. The code is concise and straightforward, making it
less prone to errors and easier to debug.
 Adaptive: Insertion sort performs exceptionally well on nearly
sorted data. If the input array is already partially sorted, insertion
sort's time complexity can approach O(n).
 Stable: Insertion sort is a stable sorting algorithm. This means that
elements with equal values maintain their relative order in the sorted
output. If two elements have the same value, their original order in
the input array will be preserved in the sorted array.
DISADVANTAGES
 Poor Performance on Large Datasets: The primary disadvantage of
insertion sort is its O(n²) time complexity in the average and worst
cases. This means that as the size of the input array grows, the
execution time increases quadratically.
 Quadratic Time Complexity: The O(n²) time complexity arises from
the nested loops within the algorithm. In the worst-case scenario (when
the input array is sorted in reverse order), the inner loop has to iterate
through almost the entire sorted portion for each element being
inserted.
 Not Efficient for Random Data: Insertion sort's performance
degrades significantly when the input data is randomly ordered.
CONCLUSION
Insertion Sort is useful for specific scenarios like sorting
small arrays or when you need a simple algorithm with
minimal memory usage. However, for large datasets or
situations where performance is critical, more efficient
algorithms like Merge Sort, Quick Sort, or Heap Sort are
recommended
BIBLIOGRAPHY
Finally my presentation would not have been completed without the
help of these resources

 Gajendra Sharma, Design & Analysis of Algorithms, Khanna


Publishing House, Delhi

 https://en.wikipedia.org/wiki/insertionsort

 https://www.geeksforgeeks.org/what-is-insertion-sort-tutorial/

 https://chatgpt.com/
THANK YOU

You might also like