binary search, insertion sort, quick select

16
Binary Search, Insertion Sort, Quick Select and Complexity Analysis

Upload: rajan-jaiprakash

Post on 23-Nov-2015

18 views

Category:

Documents


4 download

DESCRIPTION

Data structures and algorithm Lecture series

TRANSCRIPT

  • Binary Search, Insertion Sort, Quick Select and Complexity Analysis

  • Binary Search

    // invariants:// A[i] < x for all i < low// x < A[i] for all i > high

    BINARY_SEARCH(A, x, low, high) mid = (low + high) / 2

    if (high < low) return NOT_FOUND else if (A[mid] == x) return mid

    if (A[mid] > x) return BINARY_SEARCH(A, x, low, mid-1) else if (A[mid] < x) return BINARY_SEARCH(A, x, mid+1, high) else return NOT_FOUND

  • Complexity of Algorithms

    Time Complexity: Running time of the program as a function of the size of input

    Space Complexity: Amount of computer memory required during the program execution, as a function of the input size

  • Big Oh Notation

    O(n)

    The "Oh" notation specifies asymptotic upper bounds

  • Worst Case, Average Case, and Best Case

    The worst-case complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size n.

    The best-case complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size n.

    Finally, the average-case complexity of the algorithm is the function defined by the average number of steps taken on any instance of size n.

  • Big Omega and Big Theta Notations

    The notation specifies asymptotic lower bounds.

    The notation specifies asymptotic tight bounds.

  • Binary Search

  • Complexity

    checking b = ecomputing m = (b+e)/2searching a sublist of n/2 elements.

  • 2k, 2k-1, 2k-2, ... , 21, 20

  • ComplexityIt is easy to see that the sequence is of length k+1. This means that the loop could possibly be executed k times.

    So, the complexity is O(k).

    But, we need now to know exactly what k is equal to.

    Since n is between 2k-1 and 2k, k is just slightly larger than log2n.

    Since logs to different bases only differ by constants, we write log2n without the base as logn.

    Thus the complexity of binary search is O(logn) and this is quicker than sequential search since it does not grow as fast as O(n).

  • Complexityhttp://cs.engr.uky.edu/~lewis/essays/algorithms/complexity/complex.html

  • Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

  • Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

  • Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

  • Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

  • Thank You