• edited by
13,805 views
9 9 votes

If an array $A$ contains the items $10,4,7,23,67,12$ and $5$ in that order, what will be the resultant array $A$ after third pass of insertion sort?

  1. $67,12,10,5,4,7,23$
  2. $4,7,10,23,67,12,5$
  3. $4,5,7,67,10,12,23$
  4. $10,7,4,67,23,12,5$

5 Answers

17 17 votes
$\underline{\mathbf{Answer:\Rightarrow B}}$

$\underline{\textbf{Explanation:}\Rightarrow}$

 $\textbf{Given:10, 4, 7, 23, 67, 12, 5}$

Pass $1$ will be applied on the first $2$ elements.

$\mathbf{\color{red}{10,4\;\mid},7,23,67,12,5}$

$\mathbf{ \textbf{Pass 1:} \;\;\enclose{circle}{\color{red}{4,10\;}}\mid,7,23,67,12,5}$

$\mathbf{\textbf{Pass 2:}\;\; \enclose{circle}{\color{red}{4,7,10}\;}\mid,23,67,12,5}$

$\mathbf{\color{green}{\textbf{Pass 3:}}\;\;\enclose{circle}{\color{green}{4,7,10,23\;}}\mid,67,12,5}$

$\mathbf{\textbf{Pass 4:}\;\;\enclose{circle}{\color{red}{4,7,10,23,67\;}}\mid,12,5}$

$\mathbf{\textbf{Pass 5:}\;\;\enclose{circle}{\color{red}{4,7,10,12,23,67\;}}\mid,5}$

$\mathbf{\textbf{Pass 6:}\;\;\enclose{circle}{\color{red}{4,5,7,10,12,23,67\;}}}$
• edited by
2 2 votes
Answer: B) 4,7,10,23,67,12,5.

After

pass 1: 4,10,7,23,67,12,5

pass 2: 4,7,10,23,67,12,5

pass 3: 4,7,10,23,67,12,5
0 0 votes

// This will give the required array....



function insertion_Sort(arr) {
    for (let x = 1; x <arr.length; x++) {

      let j = x - 1;

      let value = arr[x];

      while (j >= 0 && arr[j] > value) {

            arr[j + 1] = arr[j];

                 j--;

  }

    arr[j + 1] = value;

    console.log(arr);

}

return arr; }

insertion_Sort([10,4,7,23,67,12,5])

 </script>

0 0 votes

option B) is correct.

First pass –  4,10,7,23,67,12,5

2nd pass –  4,7,10,23,67,12,5

3rd pass –  4,7,10,23,67,12,5

0 0 votes
Very simple logic, what we do in insertion sort is just compare the indices with neighbouring one repeatedly and in every pass ,for every value of say "i" we get a sorted arrah till that value of i.

Here we have to deal in in just three passes . So, prettily seeing the first three elements are 10,4,7. So, just sort then (4,7,10) and rewrite the other elements as same.

Answer : 4,7,10,23,67,12,5
Answer:
Position:
Show:

Related questions

6 6 votes
4 4 answers
7.7k
7.7k views
Satbir asked Jan 13, 2020
7,654 views
Of the following sort algorithms, which has execution time that is least dependant on initial ordering of the input?Insertion sortQuick sortMerge sortSelection sort
5 5 votes
4 4 answers
5.9k
5.9k views
Satbir asked Jan 13, 2020
5,891 views
Consider product of three matrices $M_1,M_2$ and $M_3$ having $w$ rows and $x$ columns, $x$ rows and $y$ columns, and $y$ rows and $z$ columns. Under what condition will ...
14 14 votes
7 7 answers
19.6k
19.6k views
Satbir asked Jan 13, 2020
19,636 views
What is the complexity of the following code?sum=0; for(i=1;i<=n;i*=2) for(j=1;j<=n;j++) sum++;Which of the following is not a valid string?$O(n^2)$$O(n\log\ n)$$O(n)$$O(...
9 9 votes
4 4 answers
9.2k
9.2k views
Satbir asked Jan 13, 2020
9,235 views
Huffman tree is constructed for the following data :$\{A,B,C,D,E\}$ with frequency $\{0.17,0.11,0.24,0.33\ \text{and} \ 0.15 \}$ respectively. $100\ 00\ 01101$ is decoded...