edited by
7,673 views
5 votes
5 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$
edited by

5 Answers

7 votes
7 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
1 votes
1 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 votes
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 votes
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

Answer:

Related questions

3 votes
3 votes
4 answers
1
Satbir asked Jan 13, 2020
3,340 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
3 votes
3 votes
3 answers
2
10 votes
10 votes
3 answers
3
Satbir asked Jan 13, 2020
8,563 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(...
5 votes
5 votes
2 answers
4
Satbir asked Jan 13, 2020
4,532 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...