KIET Group of Institutions
Department of Computer Science & Information Technology
Design and Analysis of Algorithm
Insertion Sorting
Q1. Consider an array of elements arr[5]= {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
34521
23451
12345
(B) 5 4 3 1 2
54123
51234
12345
(C) 4 3 2 1 5
32154
21543
15432
(D) 4 5 3 2 1
23451
34521
12345
Answer: (A)
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.
Q2. 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
Answer: (A)
Explanation: Time taken by algorithm is good for small number of elements, but increases
quadratically for large number of elements.
Q3. 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
Answer: (A)
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.
Q4. The auxiliary space of insertion sort is O(1), what does O(1) mean ?
(A) The memory (space) required to process the data is not constant.
(B) It means the amount of extra memory Insertion Sort consumes doesn’t depend on the input.
The algorithm should use the same amount of memory for all inputs.
(C) It takes only 1 kb of memory
(D) It is the speed at which the elements are traversed.
Answer: (B)
Explanation: The term O(1) states that the space required by the insertion sort is constant i.e.,
space required doesn’t depend on input.
Q5. Which of the following sorting algorithms has the minimum running time complexity
in the best and average case?
(A) Insertion sort, Quick sort
(B) Quick sort, Quick sort
(C) Quick sort, Insertion sort
(D) Insertion sort, Insertion sort
Answer: (A)
Explanation: Insertion sort has a best case complexity of O(n), if the array is already sorted
while it has an average case complexity of O(n 2)
Quick sort has a best case complexity of O(n log n), while it has an average case complexity of
O(n log n) also.
So, option (A) is correct.
Q6. The usual Θ(n2) implementation of Insertion Sort to sort an array uses linear search
to identify the position where an element is to be inserted into the already sorted part of
the array. If, instead, we use binary search to identify the position, the worst case running
time will
1)
2)
3)
become Θ(n log n)
4) become Θ(n)
(A) remain Θ(n2)
(B) become Θ(n (log n)2)
(C) become Θ(n log n)
(D) become Θ(n)
Answer: (A)
Q7. The worst case time complexity of insertion sort is O(n2). What will be the worst case
time complexity of insertion sort if the correct position for inserting element is calculated
using binary search?
a) O(nlogn)
b) O(n2)
c) O(n)
d) O(logn)
Answer: b
Explanation: The use of binary search reduces the time of finding the correct position from O(n)
to O(logn). But the worst case of insertion sort remains O(n2) because of the series of swapping
operations required for each insertion.
Q8. Consider the code given below, which runs insertion sort:
void insertionSort(int arr[], int array_size)
{
int i, j, value;
for (i = 1; i < array_size; i++)
{
value = arr[i];
j = i;
while (________ )
{
arr[j] = arr[j − 1];
j = j − 1;
}
arr[j] = value;
}
}
Which condition will correctly implement the while loop?
a) (j > 0) || (arr[j − 1] > value)
b) (j > 0) && (arr[j − 1] > value)
c) (j > 0) && (arr[j + 1] > value)
d) (j > 0) && (arr[j + 1] < value)
Answer: b
Explanation: In insertion sort, the element is A[j] is inserted into the correct position in the sorted
sequence A[1… j – 1]. So, condition given in (j > 0) && (arr[j − 1] > value) will implement
while loop correctly.
Q9. In insertion sort, the average number of comparisons required to place the 7th element
into its correct position is ____
a) 9
b) 4
c) 7
d) 14
Answer: b
Explanation: On average (k + 1) / 2 comparisons are required to place the kth element into its
correct position. Therefore, average number of comparisons required for 7th element = (7 + 1)/2
= 4.
Q10. Which of the following is not an exchange sort?
a) Bubble Sort
b) Quick Sort
c) Partition-exchange Sort
d) Insertion Sort
Answer: d
Explanation: In Exchange sorts, we compare each element of an array and swap those elements
that are not in their proper position. Bubble Sort and Quick Sort are exchange sorts. Quick Sort is
also called as Partition-exchange Sort. Insertion sort is not an exchange sort.