# algorithm

Q1)

Show, by applying the limit test, that each of the following is true.

a) The functions f(n)= n(n-1)/2 and g(n)= n^2 grow asymptotically at equal rate.

b) The functions f(n)=log n grow asymptotically at slower rate than g(n)=n.

Q2)

Show that log (n!) = Θ (nlog n);

Q3)

Design an algorithm that uses comparisons to select the largest and the

second largest of n elements. Find the time complexity of your algroithm

(expressed using the big-O notation).

Q4)

Given an a binary array or list of n elements, where each element is either

a 0 or 1, we would like to arrange the elements so that all of those that

are equal to 0's appear first followed by all the elements that are equal

to 1's.

a) Write an algroithm or a function that uses comparisons to arrange the

elements as given above. Do not use any extra arrays in your algorithm.

b) Find the time, T(n), needed by your algorithm in the worst-case and

then express it using the big-O notation.

c) Find the time, T(n), needed by your algorithm in the best-case and

then express it using the big-Ω notation.

d) Find the time, T(n), needed by your algorithm in the average-case

and express it using the big-Θ notation.

## Answered all questions

body preview (529 words)

xxx xxxxx by applying the limit xxxxx that xxxx xx the xxxxxxxxx xx xxxxx

xx xxx xxxxxxxxx xxxxx n(n-1)/2 xxx g(n)= n^2 xxxx xxxxxxxxxxxxxx at equal rate.

xxxxx xxxxxxxxx = Limit n(n-1)/2)/n^2

x - ∞ n xx ∞

x = xxxxx xxxxxxxx

n - xxx

= Limit 1/2 x 1/2n

x x xx xxx

= 1/2 x 1/2 * xxxxx xxx

n xx ∞

= xxx x xxx * 0

= 1/2 x xxxxxxxx

xx Limit xxxxxxxxx = xxxxxxxxx F(n) xxx g(n) xxxx xxxx xxxxxxxxxxxxxx xx xxxxx xxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

b) xxx functions f(n)=log n grow xxxxxxxxxxxxxx xx slower rate than xxxxxxx

xxxxx f(n)/g(n) x Limit xxxxxxxx x xxxxx xxxxxxx By I’Hopital’s xxxx

x xx xxx x xx ∞ x xx ∞

= x

xx Lim f(n)/g(n) = 0, xxxx xxxx asymptotically at slower rate than g(n)

n xx ∞

-------------------------------------------------------------------

Q2) Show that log (n!) x Θ (nlog n);

xxxxxxx x Log x + Log 2 x Log 3 + xxxxxxxx Log x

Taking xxxxx xxxxx

<=Log n + Log n + ………………..+ Log n

= x xxx x

x O(n xxx xx

xxxxxxx = xxx x + xxx 2 x Log x + …….+ Log x

Taking lower bound

= xxx x + …..+ xxx xxx x ……..+ log x

= Log xxx + …….+ Log x (taking xxxx xxx half xx xxxxxxx

x Log xxx x xxxxxxxxx Log n/2

x xxx xxx n/2

x Ω xx xxx xx

As Log(n!) upper bound xxx lower bound are xxxx xx Log(n!)= xxxx log n )

-----------------------------------------------------------

Q3)Design an algorithm xxxx uses comparisons xx select xxx largest and the second xxxxxxx xx n

- - - more text follows - - -

Try it before you buy it |