Professional Documents
Culture Documents
IIT Bombay
inserti (e, P ): Scan list and insert in order to maintain increasing order of list P
I
1 + 2 + . . . n = O(n2 )
delete(P, m): Remove the (min) element m at the beginning of the list P
I
O(n)
O(n2 ) overall
IIT Bombay
IIT Bombay
At start of each iteration of ith repeat loop, the subarray S[1 . . . i 1] consists of
the elements of the old S[1 . . . i 1] but sorted in ascending order
IIT Bombay
(wi 1) times
1. S[j + 1] = S[j] =
i=2
2. j = j 1
c5
n
X
(wi 1) times
i=2
end while
c6
n
X
wi times
i=2
S[j + 1] = k
until i > n =
=
c8
c7
n 1 times
n times
IIT Bombay
(wi 1) times
1. S[j + 1] = S[j] =
i=2
2. j = j 1
c5
n
X
(wi 1) times
i=2
end while
c6
n
X
wi times
i=2
S[j + 1] = k
until i > n =
=
c8
c7
n 1 times
n times
IIT Bombay
Pn
i=2 (wi
1) + c5
Pn
i=2 (wi
1) +
IIT Bombay
i=2 (wi
1) + c5
Pn
i=2 (wi
1) +
Pn
IIT Bombay
Pn
i=2 (wi
1) + c5
Pn
i=2 (wi
1) +
IIT Bombay
Pn
i=2 (wi
1) + c5
Pn
i=2 (wi
1) +
IIT Bombay
An ordered pair of indices (i, j) into S is an inversion if i < j but S[i] > S[j]
IIT Bombay
An ordered pair of indices (i, j) into S is an inversion if i < j but S[i] > S[j]
n(n1)
4
IIT Bombay
IIT Bombay
n!
n(n1)
4
IIT Bombay
n!
n(n1)
4
Average T (n) =
I
n(n1)
4
+ n = O(n2 )
True about ANY swap based algorithm, since swap removes exactly one inversion in
a single step.
IIT Bombay
IIT Bombay
Thank you
IIT Bombay