• edited by
3,973 views

6 Answers

7 7 votes

The total number of inversions in an array is equal to the total number of swaps performed in insertion sort.
Given that the number of swaps required to sort the array is 2, and for the elements x = 10 and 14, the total number of inversions is also 2,
therefore, 10 and 14 are the correct answers.

6 6 votes

We are given the initial array  

\[
[1, 3, 5, 7, 9, 11, x, 15, 13]
\]

and told that Insertion Sort performs exactly two element shifts (swaps). In Insertion Sort, a “swap” refers to a single right-shift of an element during the insertion of the key. The total number of such shifts equals the number of inversions resolved during the process.

The array has 9 elements, indexed \(0\) through \(8\):

\[
\begin{array}{|c|c|c|c|c|c|c|c|c|c|}
\hline
\text{Index} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\hline
\text{Value} & 1 & 3 & 5 & 7 & 9 & 11 & x & 15 & 13 \\
\hline
\end{array}
\]

The prefix \([1,3,5,7,9,11]\) is sorted, so swaps can only occur during the insertion of elements at indices 6 (\(x\)), 7 (15), and 8 (13). 

Option A: \(x = 10\)

Step 1 : Insert \(x = 10\) (index 6):  
Compare with 11 → 10 < 11 → shift 11 right → 1 swap

\[
\begin{array}{|c|c|c|c|c|c|c|c|c|c|}
\hline
\text{Index} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\hline
\text{Value} & 1 & 3 & 5 & 7 & 9 & 10 & 11 & 15 & 13 \\
\hline
\end{array}
\]

Step 2 – Insert 15 (index 7):  
15 > 11 → no shift → 0 swaps

Step 3 – Insert 13 (index 8):  
13 < 15 → shift 15 → 1 swap; 13 > 11 → stop.

\[
\begin{array}{|c|c|c|c|c|c|c|c|c|c|}
\hline
\text{Index} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\hline
\text{Value} & 1 & 3 & 5 & 7 & 9 & 10 & 11 & 13 & 15 \\
\hline
\end{array}
\]

Total swaps = 1 + 1 = 2

Option B: \(x = 12\)

Insert 12: 12 > 11 → no shift  
Insert 15: 15 > 12 → no shift  
Insert 13: 13 < 15 → shift 15 → 1 swap; 13 > 12 → stop.

Final array:

\[
\begin{array}{|c|c|c|c|c|c|c|c|c|c|}
\hline
\text{Index} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\hline
\text{Value} & 1 & 3 & 5 & 7 & 9 & 11 & 12 & 13 & 15 \\
\hline
\end{array}
\]

Total swaps = 1

Option C: \(x = 14\)

Insert 14: 14 > 11 → no shift  
Insert 15: 15 > 14 → no shift  
Insert 13:  

  • 13 < 15 → shift 15 → 1 swap  
  • 13 < 14 → shift 14 → 1 swap  
  • 13 > 11 → stop.

\[
\begin{array}{|c|c|c|c|c|c|c|c|c|c|}
\hline
\text{Index} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\hline
\text{Value} & 1 & 3 & 5 & 7 & 9 & 11 & 13 & 14 & 15 \\
\hline
\end{array}
\]

Total swaps = 2

Option D: \(x = 16\)

Insert 16: no shift  
Insert 15: 15 < 16 → shift 16 → 1 swap  
Insert 13:  

  • 13 < 16 → shift 16 → 1 swap  
  • 13 < 15 → shift 15 → 1 swap  
  • 13 > 11 → stop.

Total shifts = 1 (for 15) + 2 (for 13) = 3 swaps

Total swaps = 3

Only Option A (\(x = 10\)) and Option C (\(x = 14\)) result in exactly two swaps.

\[
\boxed{\text{A and C}}
\]

0 0 votes

An inversion is a pair of indices (i,j) such that  i < j  such that A[i] > A[j]. Since insertion sort fixes the array by eliminating these order violations one-by-one, the total number of shifts (or swaps) performed by insertion sort is exactly equal to the number of inversions present in the array.

 

Option A: x = 10

Array:

[1,3,5,7,9,11,10,15,13]

Inversions:

  • (11,10)

  • (15,13)

Total inversions = 2

Insertion Sort swaps:

  1. Insert 10:

    • 11 > 10 → shift → 1 swap

  2. Insert 13:

    • 15 > 13 → shift → 1 swap

Total swaps = 2

✔ Option A (x = 10) is VALID


Option B: x = 12

Array:

[1,3,5,7,9,11,12,15,13]

Inversions:

  • Only (15,13)

Total inversions = 1

Insertion Sort swaps:

  1. Insert 12:

    • No shifts (12 > 11)

  2. Insert 13:

    • 15 > 13 → shift → 1 swap

Total swaps = 1

✘ Option B (x = 12) is NOT VALID

(Only 1 swap, but we need 2.)


Option C: x = 14

Array:

[1,3,5,7,9,11,14,15,13]

Inversions:

  • (14,13)

  • (15,13)

Total inversions = 2

Insertion Sort swaps:

  1. Insert 14:

    • No shifts (14 > 11)

  2. Insert 13:

    • 15 > 13 → shift → 1 swap

    • 14 > 13 → shift → 1 swap

Total swaps = 2

✔ Option C (x = 14) is VALID


Option D: x = 16

Array:

[1,3,5,7,9,11,16,15,13]

Inversions:

  • (16,15)

  • (16,13)

  • (15,13)

Total = 3 inversions

Insertion Sort swaps:

Insert 16 → no swaps
Insert 13 → shifts 15 and 16 → 2+ swaps → > 2 total

✘ Option D (x = 16) is NOT VALID

0 0 votes
Loop invariant of insertion sort is: Before $k$th iteration the subarray $A[1:k-1]$ contains the first $k-1$elements from original array in sorted order

Here we have an array $A = [ 1, 3, 5, 7, 9, 11, x, 15, 13]$, after applying insertion sort on $A$, there are exactly two swaps. By seeing the 13 at the end of array we can conclude that there is atleast one swap happens in the last iteration and another thing here is the subarray $A[0: 5]$ is already sorted.

Case 1: all the two swaps happens in the last iteration$(8th \text{ iteration } )$. Then it means before the $8\text{th}$ iteration the array $A[0:7]$ is sorted then implies $x \leq 15$ and since last two swaps must happen in last iteration, one swap happens with $15$ and $13$  and other swap must be $13$ and $x$, which means $x > 13 $. so there are two conditions $x > 13 \text{ and }x \leq 15$, the possible $x's $  are $14, 15$.

Case 2: Exactly one swap happens in the last iteration$\implies x \leq 13$, the remainging swap must happen in $6\text{th}$ iteration, because if there is swap in $\text{7th}$ iteration then implies $x >15$, which is a contradiction. So we need exactly one swap in $\text{6th}$ iteration $\implies x < 11 \text{ and } x \geq 9 \implies $ the possible $x's$ are $9, 10 $.

In the given options, the answers are $A , C$
• edited by
0 0 votes

In insertion sort, the number of swaps equals the number of "inversions"—how many elements currently to the left of a number are strictly greater than it.

The initial sequence [1, 3, 5, 7, 9, 11] is already sorted, requiring 0 swaps.

Testing the given options for $x$:

  • x = 10: 11 > 10 (1 swap). Inserting 15 requires 0 swaps. When inserting 13, 15 > 13 (1 swap). Total = 2 swaps.

  • x = 12: Inserting 12 and 15 requires 0 swaps. When inserting 13, 15 > 13 (1 swap). Total = 1 swap.

  • x = 14: Inserting 14 and 15 requires 0 swaps. When inserting 13, both 14 and 15 > 13 (2 swaps). Total = 2 swaps.

  • x = 16: Inserting 16 requires 0 swaps. When inserting 15, 16 > 15 (1 swap). When inserting 13, both 15 and 16 > 13 (2 swaps). Total = 3 swaps.

The values that yield exactly two swaps are 10 and 14, which correspond to options A and C.

Answer:
Position:
Show:

Related questions

2 2 votes
1 answers 1 answer
1.1k
1.1k views
Himanshu1 asked Dec 16, 2015
1,089 views
A) 192 B) 120 c) 188 D) 176
3 3 votes
2 2 answers
569
569 views
Shubham Sharma 2 asked Jun 16, 2025
569 views
Recall the insertion sort algorithm: procedure INSERTIONSORT(A) for i ← 1 to A.length - 1 do key ← A[i] j ← i - 1 while j ≥ 0 and A[j] key do A[j + ...
2 2 votes
2 2 answers
224
224 views
GO Classes asked Aug 12
224 views
Suppose Binary Search is used in Insertion Sort to locate where the $i$th element should be inserted among the first $i-1$ elements.What is the worst-case running time of...
1 1 vote
2 2 answers
169
169 views
GO Classes asked Aug 11
169 views
When is insertionsort a good choice for sorting an array?Each component of the array requires a large amount of memory. Each component of the array requires a small amoun...