• edited by
941 views
2 2 votes

Consider the problem of sorting the given array in ascending order:

\[
P=[1,2,3,5,4]
\]
Consider two sorting algorithms Bubble Sort $\text{(BS)}$ and Insertion Sort $\text{(IS)}$.
Let $\text{N1}$ be the total number of comparisons done by $\text{BS}$ on the elements of $\text{P}$ and $\text{N2}$ be the total number of comparisons done by $\text{IS}$ on the elements of $\text{P}$.

Which of the following options is/are correct?

  1. $\mathrm{N} 1=10, \mathrm{~N} 2=4$
  2. $\mathrm{N} 1>\mathrm{N} 2$
  3. $\text{IS}$ on $\text{P}$ will perform only one swap
  4. Both $\text{BS}$ and $\text{IS}$ on $\text{P}$ will make at least one unnecessary comparison (i.e., comparing elements that are already in correct order)

2 Answers

5 5 votes

Given array:

\[

P = [1,2,3,5,4]

\]

Only the last two elements are misplaced.

Number of elements:

\[

n = 5

\]

Bubble Sort:

For standard Bubble Sort (without early stopping), the total number of comparisons is

\[

N_1 = \frac{n(n-1)}{2}

\]

\[

N_1 = \frac{5 \cdot 4}{2} = 10

\]

Insertion Sort:

We count comparisons pass by pass.

\[

[1,2,3,5,4]

\]

  • \(i=1\): compare \(2\) with \(1\) → 1 comparison
  • \(i=2\): compare \(3\) with \(2\) → 1 comparison
  • \(i=3\): compare \(5\) with \(3\) → 1 comparison
  • \(i=4\): compare \(4\) with \(5\) → 1 comparison
  • \(i=4\): compare \(4\) with \(3\) → 1 comparison

Total comparisons:

\[

N_2 = 1 + 1 + 1 + 2 = 5

\]

Thus,

\[

N1 = 10, \quad N2 = 5

\]

Checking options:

(A)

\[

N1 = 10, \quad N2 = 4

\]

False since

\[

N_2 = 5

\]

(B)

\[

N1 > N2

\]

Since

\[

10 > 5

\]

True.

(C) Insertion Sort performs only one swap.

There is exactly one inversion:

\[

(5,4)

\]

Element \(4\) shifts once, so only one swap.

Small technical note: In the standard implementation of insertion sort, we usually shift elements rather than literally swap them. But this question is clearly using the exchange/swap interpretation, so C is intended to be correct.

The question is likely using "swap" loosely to mean "one element movement/exchange," which is common in many textbooks and exam-prep material that don't rigorously distinguish the two terms.

True.

(D) Both BS and IS make at least one unnecessary comparison.

Bubble Sort compares already sorted pairs like \((1,2)\), \((2,3)\), etc.

Insertion Sort also compares already sorted elements before reaching the misplaced element.

True.

Final Answer:

\[

\boxed{(B), (C), (D)}

\]

Shortcut Tricks to Save Time in This Type of Questions:

       1.Bubble Sort comparisons (without early break):

\[

\frac{n(n-1)}{2}

\]

       2. Insertion Sort comparisons:

\[

\text{Comparisons} = \text{Number of inversions} + (n-1)

\]

       3.If array is nearly sorted:

                                               \[ 

                                               \text{ Insertion Sort is close to linear time.}

                                               \]

 

• edited by
1 flag:
✌ Edit necessary (i.kshitij “option c is NOT CORRECT!”)
0 0 votes

Insertion sort does not swap elements. Instead, it picks up a key element, stores it in a temporary variable, and shifts larger adjacent items one position to the right to clear a spot.


Therefore, C is not correct.


Only B,D are correct

Answer:
Position:
Show:

Related questions

10 10 votes
6 6 answers
4.0k
4.0k views
Arjun asked Feb 27, 2025
3,997 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
4 4 votes
2 2 answers
557
557 views
gatecse asked Feb 23
557 views
You are given the following Pre-order and In-order traversals of a Binary Tree $T$ with nodes $E, F, G, P, Q, R, S$.\[\begin{array}{llllllll}\text{Pre-order:} & P & Q & S...
0 0 votes
2 2 answers
601
601 views
gatecse asked Feb 23
601 views
Consider that $20$ stories of Author $\text{X}$ and $10$ stories of Author $\text{Y}$ were kept together without mentioning the names of the authors. $\text{A}$ classifie...
2 2 votes
2 2 answers
662
662 views
gatecse asked Feb 23
662 views
Let $P(x)$ be a predicate.Which of the following statements is/are NOT valid in first-order logic?$\forall x P(x) \Rightarrow \exists x P(x)$$\exists x P(x) \Rightarrow \...