Question

What is the big-Ω (best-case) running time of each of the following? Briefly justify your answers....

What is the big-Ω (best-case) running time of each of the following? Briefly justify your answers. You should reference the optimal algorithm for each problem.

(a) (2 points) Finding the max of a sorted list

(b) (2 points) Finding the median (middle number) of a sorted list of odd length

(c) (2 points) Finding the range of an unsorted list

(d) (2 points) Finding out if a list of strings has duplicates

(e) (2 points) Finding the square root of a number that is a perfect square

Homework Answers

Answer #1

(a) (2 points) Finding the max of a sorted list

If the array is sorted the max element is always present at either at index 0 (in ascending order) or at last(descending order). We can directly find it in constant time.

Best Case Time Complexity: O(1)

(b) (2 points) Finding the max of a sorted list

If the array is sorted list of odd length median is always present at middle of the array which will be found by

array [ len_of_arr / 2 ]

It takes only a constant time.

Best Case Time Complexity: O(1)

(c) (2 points) Finding the range of an unsorted list.

To find the range of an unsorted list we have to traverse from begging to end and find the min and max value of list. This is how it looks like.

range(arr){
    min = arr [0]
    max = arr [0]
    n = len(arr)
    for i=1 to n{
        if(arr[i]<min){
            min = arr[i]
        }
        if(arr[j]> max){
            max = arr[j]
        }
    }
    return [min,max]
}

Since we have to traverse until end the t.c would be O(n)

Best case time complexity: O(n)

e) (2 points) Finding the square root of a number that is a perfect square

Let  's' be the answer.  We know that 0 <=  s <= x.
Consider any random number r. 
    If r*r = x
    If r*r > x, s < r. 
Approach ::

1) Start with ‘start’ = 0, end = ‘x’,
2) Do following while ‘start’ is smaller than or equal to ‘end’.
a) Compute ‘mid’ as (start + end)/2
b) compare mid*mid with x.
c) If x is equal to mid*mid, return mid.
d) If x is greater, do binary search between mid+1 and end. In this case, we also update ans (Note that we need floor).
e) If x is smaller, do binary search between start and mid

int floorSqrt(int x)
{
    // Base cases
    if (x == 0 || x == 1)
        return x;

    int start = 1, end = x, ans;
    while (start <= end)
    {
            int mid = (start + end) / 2;
            if (mid*mid == x)
                return mid; 
            if (mid*mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
            else{
                end = mid-1;
            }
    }
    return ans;
} 


IT is almost similler to Binary Search

Best Case Time Complexity : O(Log N)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT