Preview

Algorithm: Big O Notation and Log

Satisfactory Essays
Open Document
Open Document
350 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Algorithm: Big O Notation and Log
Assignment #2
- Deadline to submit is June 1, 2012 at 4:30 pm local (Halifax, NS) time. 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.

lim f(n)/g(n) = lim f’(n)/ g’(n) = lim (n-0.5) /2n = 1/2 n->∞ n->∞ n->∞ f(n) and g(n) grow at equal rate

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

lim f(n)/g(n) = lim f’(n)/ g’(n) = lim (1/n)/ 1 = 0 n->∞ n->∞ n->∞ so f(n)grows at a slower rate than g(n).

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

Show that log (n!) = Θ (nlog n)
First
log (n!) = log 1+ log 2+...+log n log n+ log n+ …+log n log (n!) n log n log (n!) = O(n log n)
Then
log (n!) = log 1 +log 2 +... + log n log (n/2)+ log (n/2+1)+...+log (n) log (n!) log (n/2) + log (n/2) +...+ log (n/2) log (n!) (n/2) log (n/2) , n/2>0 for sufficiently large n log (n!) = (n log n)
So
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 algorithm (expressed using the big-O notation).

String MaxAndSecond(int a[],int n)
{
int max =0, second =0; max = a[0]; for (i = 1; i < n; i++) { if (a[i] >= max) { second = max; max = a[i]; } else if (a[i] > second) { second = a[i]; } }
}

T(n)=O(n)

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 algorithm or a function that uses comparisons to arrange the elements as given above. Do not use any extra arrays in your algorithm.

Use merge sorting algorithm to

You May Also Find These Documents Helpful

Related Topics