Lab 1 Fall 2012

This lab is about coding and understanding an algorithm and doing problems to review the mathematics useful for analysis of algorithm . Please submit on a separate piece of paper (printout of your code and a sample output) with your homework number 1 to get your full credit for the lab. Out of the four problems you need to do only (any three among the four) three problems to get full credit.
Reading Material: Chapter 0 of DG book and Section: 1.2 of CLRS book
  1. At least 5 problems in Exercise 0.1 (in Das Gupta et al's nook)
  2. The following algorithm computes the square root of a positive integer.
    input: Positive integer x
    Output: Square root of an integer y
    
    Step 1: y = 1
    Step 2: while (y * y <= x)
    step 3: y = y+1
    Step 4: return y
    
    What is the running time of the above algorithm. Code is located in lab1-2-2012.cc Can you improve the running time of this algorithm to O(log(n)) and write a code for that.
  3. From the array of integers from 0 to 2 n-1, you are given all but one number. Your task is to write an algorithm and implement it to find which integer was not given, What is the running time of your algorithm. What is the memory requirement. (Eg. if n =2, give integers may be 0,1,3 and your output will be 2.)
  4. From an array of integers, write an algorithm and implement to determine whether the sum of any two distinct elements is 0. What is the running time of your algorithm. (Hint: Can you come up with O(n log2 n) algorithm in the worst case.) (You may use STL libraries - Assume STL algorithms are the most efficient)