AOA Experiment 2
AOA Experiment 2
2
To implement Insertion Sort
Date of Performance: 22/01/2025
Theory:
Insertion sort is a simple sorting algorithm that works similar to the way you sort the
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part.
Example:
• Let size of the input array is n. Total time taken by algorithm is the summation of
time taken by each of its instruction.
• The best case offers the lower bound of the algorithm’s running time.
• When data is already sorted, the best scenario for insertion sort happens.
• In this case, the condition in the while loop will never be satisfied, resulting in
tj = 1.
• The worst-case running time gives an upper bound of running time for any input.
• The running time of algorithm cannot get worse than its worst-case running time.
• Worst case for insertion sort occurs when data is sorted in reverse order.
• So we must have to compare A[j] with each element of sorted array A[1 … j – 1].
So, tj = j
#include <stdio.h>
insertionSort(arr, n);
return 0;
}
Conclusion:
Insertion Sort is a simple and intuitive sorting algorithm that efficiently builds a sorted
sequence by inserting each element into its correct position. It performs well for small or
nearly sorted datasets, with an average and worst-case time complexity of O(n²). Unlike more
complex sorting algorithms, it is stable and operates in-place with O(1) extra space, making it
memory efficient. Though not ideal for large datasets, its adaptive nature allows it to perform
efficiently when dealing with partially sorted data. Overall, Insertion Sort is an essential
algorithm for understanding sorting principles and algorithmic design.