# InsertionSort

• Last Updated : 28 Jun, 2021

Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.

Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1 … More on Insertion Sort

 Question 1
Which of the following is not a stable sorting algorithm in its typical implementation.
 A Insertion Sort B Merge Sort C Quick Sort D Bubble Sort
Sorting    QuickSort    InsertionSort    MergeSort
Discuss it

Question 1 Explanation:
 Question 2
Which of the following sorting algorithms in its typical implementation gives best performance when applied on an array which is sorted or almost sorted (maximum 1 or two elements are misplaced).
 A Quick Sort B Heap Sort C Merge Sort D Insertion Sort
Sorting    QuickSort    InsertionSort    HeapSort
Discuss it

Question 2 Explanation:
Insertion sort takes linear time when input array is sorted or almost sorted (maximum 1 or 2 elements are misplaced). All other sorting algorithms mentioned above will take more than lienear time in their typical implementation.
 Question 3
Consider a situation where swap operation is very costly. Which of the following sorting algorithms should be preferred so that the number of swap operations are minimized in general?
 A Heap Sort B Selection Sort C Insertion Sort D Merge Sort
Sorting    SelectionSort    InsertionSort    MergeSort
Discuss it

Question 3 Explanation:

Selection sort makes O(n) swaps which is minimum among all sorting algorithms mentioned above.

 Question 4
What is the worst case time complexity of insertion sort where position of the data to be inserted is calculated using binary search?
 A N B NlogN C N^2 D N(logN)^2
Analysis of Algorithms    Sorting    InsertionSort    BinarySearch
Discuss it

Question 4 Explanation:
Applying binary search to calculate the position of the data to be inserted doesn't reduce the time complexity of insertion sort. This is because insertion of a data at an appropriate position involves two steps: 1. Calculate the position. 2. Shift the data from the position calculated in step #1 one step right to create a gap where the data will be inserted. Using binary search reduces the time complexity in step #1 from O(N) to O(logN). But, the time complexity in step #2 still remains O(N). So, overall complexity remains O(N^2).
 Question 5
Which sorting algorithm will take least time when all elements of input array are identical? Consider typical implementations of sorting algorithms.
 A Insertion Sort B Heap Sort C Merge Sort D Selection Sort
Sorting    SelectionSort    InsertionSort    MergeSort
Discuss it

Question 5 Explanation:
The insertion sort will take (n) time when input array is already sorted.
 Question 6
In a permutation a1.....an of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj. What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of 1.....n with at most n inversions?
 A Θ (n2) B Θ (n log n) C Θ (n1.5) D Θ (n)
Analysis of Algorithms    Sorting    GATE-CS-2003    InsertionSort
Discuss it

Question 6 Explanation:
Insertion sort runs in Θ(n + f(n)) time, where f(n) denotes the number of inversion initially present in the array being sorted. Source: http://cs.xidian.edu.cn/jpkc/Algorithm/down/Solution%20to%202-4%20Inversions.pdf
 Question 7
Consider an array of elements arr= {5,4,3,2,1} , what are the steps of insertions done while doing insertion sort in the array.
 A 4 5 3 2 1 3 4 5 2 1 2 3 4 5 1 1 2 3 4 5 B 5 4 3 1 2 5 4 1 2 3 5 1 2 3 4 1 2 3 4 5 C 4 3 2 1 5 3 2 1 5 4 2 1 5 4 3 1 5 4 3 2 D 4 5 3 2 1 2 3 4 5 1 3 4 5 2 1 1 2 3 4 5
Sorting    InsertionSort
Discuss it

Question 7 Explanation:
In the insertion sort , just imagine that the first element is already sorted and all the right side Elements are unsorted, we need to insert all elements one by one from left to right in the sorted Array. Sorted : 5                                unsorted : 4 3 2 1 Insert all elements less than 5 on the left (Considering 5 as the key ) Now key value is 4 and array will look like this Sorted : 4 5                             unsorted : 3 2 1 Similarly for all the cases the key will always be the newly inserted value and all the values will be compared to that key and inserted in to proper position.
 Question 8
Which is the correct order of the following algorithms with respect to their time Complexity in the best case ?
 A Merge sort > Quick sort >Insertion sort > selection sort B insertion sort < Quick sort < Merge sort < selection sort C Merge sort > selection sort > quick sort > insertion sort D Merge sort > Quick sort > selection sort > insertion sort
Sorting    QuickSort    SelectionSort    InsertionSort
Discuss it

Question 8 Explanation:
In best case,

Quick sort: O (nlogn)
Merge sort: O (nlogn)
Insertion sort: O (n)
Selection sort: O (n^2)  
 Question 9
Which of the following statements is correct with respect to insertion sort ?
*Online - can sort a list at runtime
*Stable - doesn't change the relative
order of elements with equal keys. 
 A Insertion sort is stable, online but not suited well for large number of elements. B Insertion sort is unstable and online C Insertion sort is online and can be applied to more than 100 elements D Insertion sort is stable & online and can be applied to more than 100 elements
Sorting    InsertionSort
Discuss it

Question 9 Explanation:
Time taken by algorithm is good for small number of elements, but increases quadratically for large number of elements.
 Question 10
Consider the array A[]= {6,4,8,1,3} apply the insertion sort to sort the array . Consider the cost associated with each sort is 25 rupees , what is the total cost of the insertion sort when element 1 reaches the first position of the array  ?
 A 50 B 25 C 75 D 100
Sorting    C Arrays    InsertionSort
Discuss it

Question 10 Explanation:
When the element 1 reaches the first position of the array two comparisons are only required hence 25 * 2= 50 rupees. *step 1: 4 6 8 1 3 . *step 2: 1 4 6 8 3.
There are 13 questions to complete.

## Coding practice for sorting.

My Personal Notes arrow_drop_up