edited by
1,328 views
2 2 votes

47. An array ' $A$ ' contain ' $I$ ' number of Inversions then what is the Time Complexity of insertion sort algorithm to sort ' $n$ ' element of array ' $A$ '?

  1. $\mathrm{O}(\mathrm{n}+\mathrm{I})$
  2. $\mathrm{O}\left(\mathrm{n}^{2}+\mathrm{I}\right)$
  3. $\mathrm{O}\left(\mathrm{n}^{3}+\mathrm{I}\right)$
  4. $\mathrm{O}\left(\mathrm{n}^{4}+\mathrm{I}\right)$

2 Answers

Best answer
2 2 votes

If the given array is already sorted and maximum n inversion is possible then insertion sort gives best case for that array because array is already sorted.i.e O(n)

e.g. = 10 20 30 40 50 60 1 : 
here 6 inversion and array is almost sorted so it will take 6 comparison and 6 swaps 

if array is of n element and 1 element is not sorted then :      O(n) for searching the position 
                                                                                         n swap 
                                                                                         O(2n) 
for n element n*2n = O(n2)

So we can say if in array there are I inversion is there then Time Complexity = O(n + I) 
                                                                                         O(n) for best case for I= n 
                                                                                         O(n2) for worst case if I is more than n
​optiion A         

selected by
2 2 votes

Insersion sort will take O( n2  )  using merge sort complexity will be O(nlogn)

Position:
Show:

Related questions

4 4 votes
5 answers 5 answers
23.4k
23.4k views
worst_engineer asked Oct 7, 2015
23,445 views
The running time of an algorithm is given by\[T(n)=T(n-1)+T(n-2)-T(n-3) \text {, if } n \geqslant 3\]n, otherwise.The order is :n$\log n$$\mathrm{n}^{n}$$n^{2}$
0 0 votes
1 answers 1 answer
681
681 views
Nandkishor3939 asked Jan 16, 2019
681 views
I searched on internet but got noting .
0 0 votes
2 2 answers
715
715 views
vaishali jhalani asked Jan 19, 2017
715 views
Consider the following program segment? main ()Iint }t=0\mathrm{ ; int }c=0\mathrm{ ;For (i=1;i\leqn;++i) |For (j=1;j\leqi*}t;++j)If ((j%i)== 0)For (k=1;k\leqj,++k)|c=c...
0 0 votes
2 answers 2 answers
2.2k
2.2k views
venky.victory35 asked Dec 19, 2015
2,166 views
We are given a sequence of $n$ positive numbers $a_{1}, a_{2}, \ldots, a_{n}$ and a fixed number $k>0$. We want to find a pair of numbers $a_{i}$ and $a_{j}$ such that $j...