Al No Content Page No
Al No Content Page No
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:
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
https://en.wikipedia.org/wiki/insertionsort
https://www.geeksforgeeks.org/what-is-insertion-sort-tutorial/
https://chatgpt.com/
THANK YOU