Group3 Insertion Sort Algorithm
Group3 Insertion Sort Algorithm
Insertion Sort
Algorithm
2
Example:
3
Cont..
4
Insertion
Sort
Outer loop: controls how
FOR i = 1 to length of array - 1 many elements we process.
SET key = array[i] Inner loop: handles the
SET j = i - 1 shifting process.
WHILE j >= 0 AND array[j] > key
SET array[j + 1] = array[j] Optimization: aim to reduce
DECREMENT j by 1 the time for finding the
correct position without
affecting the overall
SET array[j + 1] = key performance.
5
Implementation
• Function to sort array using insertion sort
6
Implementation Cont..
• Utility function to print array of size n
Output:
2457
7
Properties of Insertion Sort
8
Time Complexity
9
Space Complexity
• (O(1))
10
Uses and Applications
11
When Should it be used?
• Stable Sorting
• Simple Implementations
12
Limitations of Insertion Sort
• Quadratic Time Complexity: O(n²) in worst/average case, inefficient for large
datasets.
• Inefficient for Random Arrays: Requires many comparisons and shifts, making it
slow for unordered data.
13
Visual Example
14
Advantages and Disadvantages
15
Comparison with other Algorithms
16
Reference
GeekforGeeks. (n.d.). Insertion sort algorithm. GeeksforGeeks. Retrieved
December 3, 2024, from https://www.geeksforgeeks.org/insertion-sort-
algorithm/?ref=lbp
Bro Code. (2020, April 10). Insertion sort algorithm | Learn Insertion Sort |
BroCode. YouTube. Retrieved December 3, 2024, from
https://www.youtube.com/watch?v=8mJ-OhcfpYg&t=243s
subtitle