0% found this document useful (0 votes)
8 views3 pages

Insertion Sort Explained with Example

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

Insertion Sort Explained with Example

Insertion
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA STRUCTURES

INSERTION SORT
➢ Insertion sort is one of the easiest sorting algorithm
➢ Insertion sort is used to sort elements in either ascending or descending order
➢ In insertion sort, we maintain a sorted part and unsorted part
➢ With every iteration, one item is moved from the unsorted section to the sorted
section
➢ The first element is picked and is considered sorted
➢ After this, we start picking from the second element onward and compare with
elements in the sorted section
➢ We keep shifting the elements from the sorted section one by one until an
appropriate location is found for that element
➢ This process is continued until all elements have been exhausted

EXAMPLE:
Suppose the list of numbers A[0], A[1], A[2], A[3]

50 10 25 3
A[0] A[1] A[2] A[3]

int A[4]={50,10,25,3};
N=4
Total No of pass = N-1
= 4-1
Total No of pass = 3
i => represents no of pass
j => represents no of comparison
First Pass ( i=1 )
j=i
J=1
No of comparison = 1
ARRAY COMPARISON RESULT

50 10 25 3
A[0] A[1] A[2] A[3] A[ j ] < A[ j-1 ]
A[1] < A[0] INTERCHANGE
10 < 50, TRUE

10 50 25 3
A[0] A[1] A[2] A[3]

After first pass – first two elements of an array are in sorted order

10 50 25 3
A[0] A[1] A[2] A[3]

SECOND PASS ( i=2 )


J=i
J=2
No of comparison = 2
ARRAY COMPARISON RESULT

10 50 25 3
A[ j ] < A[ j-1 ]
A[0] A[1] A[2] A[3]
A[2] < A[1]
25 < 50, TRUE INTERCHANGE

A[ j ] < A[ j-1 ]
10 25 50 3
A[0] A[1] A[2] A[3] A[1] < A[0]
25 < 10, FALSE NOCHANGE

After second pass – first three elements of an array are in sorted order

10 25 50 3
A[0] A[1] A[2] A[3]
THIRD PASS ( i=3 )
J =i
J=3
No of comparison=3
ARRAY COMPARISON RESULT

10 25 50 3
A[ j ] < A[ j-1 ]
A[0] A[1] A[2] A[3]
A[3] < A[2]
3 < 50, TRUE INTERCHANGE

10 25 3 50
A[0] A[1] A[2] A[3] A[ j ] < A[ j-1 ]
A[2] < A[1] INTERCHANGE
3 < 25, TRUE

10 3 25 50
A[ j ] < A[ j-1 ]
A[0] A[1] A[2] A[3]
A[1] < A[0] INTERCHANGE
3 < 10, TRUE

3 10 25 50
A[0] A[1] A[2] A[3]

After THIRD PASS – ALL the elements of an array are in a sorted order

3 10 25 50
A[0] A[1] A[2] A[3]

Common questions

Powered by AI

In one pass of the insertion sort algorithm, the following steps occur: 1) Start from the element right after the sorted section. 2) Compare the current element with the last element of the sorted section. 3) If the current element is smaller, shift the compared element one position to the right. 4) Repeat the comparison process until the appropriate position is found or the start of the sorted section is reached. 5) Insert the current element into its correct position within the sorted section .

Insertion sort maintains separate 'sorted' and 'unsorted' sections by initially considering the first element as sorted. It increments the sorted section one element at a time as elements from the unsorted section are iteratively inserted at the correct position within the sorted section. This strategy is effective because it incrementally builds the solution, allowing the sorted section to grow in an orderly fashion while ensuring each element is correctly positioned before proceeding to the next, thus providing a stable sorting process .

Insertion sort determines where to place an element by comparing it with elements in the sorted section of the array, starting from the end of the sorted section. If the current element is smaller than the element it is being compared with, the compared element is shifted one position to the right. This process continues until an appropriate position is found where the current element is larger than the element it is being compared with or the beginning of the sorted section is reached .

Insertion sort treats the initial element of the array as already sorted. This provides the starting point for building the sorted section of the array. By considering this element sorted, insertion sort has a basis to begin comparing subsequent elements and determining where they should be placed relative to this initial element, facilitating the incremental construction of the full sorted array .

Comparisons in insertion sort are critical for maintaining order in the sorted section of the array. Each comparison checks if the current element from the unsorted section is smaller than elements in the sorted section. These comparisons determine the position where the current element should be inserted. Fewer comparisons indicate the list is closer to being sorted, leading to fewer element shifts, faster sorting, and less resource usage .

Insertion sort might be preferred for small datasets because of its simple implementation and efficient handling of already nearly-sorted data, resulting in minimal movement of elements. The overhead is low compared to more complex algorithms, making it faster for small tasks. It’s also an adaptive sort, meaning its performance improves significantly with partial datasets that are already partially sorted .

Element shifting in insertion sort occurs when an element from the unsorted section is smaller than elements in the sorted section. Each element in the sorted section that is larger than the current element is shifted one position to the right, creating space for the current element to be inserted at the correct position. This method contributes to the maintenance of order within the sorted section, ensuring that all elements are placed in their final sorted order by the end of the sorting process .

Insertion sort achieves its ideal performance when the input array is already sorted. In this best-case scenario, insertion sort runs with a time complexity of O(n) because each element is only compared once and no shifting of elements is required, resulting in a highly efficient linear time performance .

In the best-case scenario, where the array is already sorted, insertion sort has a time complexity of O(n) because each element only needs to be compared once. However, in the worst-case scenario, where the array is sorted in reverse order, the time complexity is O(n^2) because each element must be compared with every other element before it can be inserted into its correct position, leading to a quadratic number of total comparisons and shifts .

The first iteration of insertion sort is distinguished by the initial setup where the first element of the array is considered sorted on its own. Subsequent elements are then iteratively picked from the unsorted section beginning from the second element, which are then inserted into the correct position within the growing sorted section of the array. Each subsequent iteration involves comparison and possible shifting of elements already sorted .

You might also like