Binary Search

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Search

  • The most common operation in Computer Science
  • Search in an Sorted Array
  • How to search? You can always go through the whole Array
  • Is there a way to search easier?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

What is Binary Search

  • Example: Dictionary

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Binary Search

  • The fastest way to search in an sorted array
  • Time Complexity: O(logN)
T(n) = T(n/2) + c
T(n)=T(n/2)+cT(n) = T(n/2) + c

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example Code

public int BinarySearch(int[] A, int target) 
    int begin = 0;
    int end = A.length ;
    while(begin < end) {
        int p = (end + begin) / 2;
        if(A[p] == target) return p;
        else if(A[p] < target) {
            begin = p + 1;
        }
        else {
            end = p;
        }
    }
    return -1;
}

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example Code 2

public int BinarySearch(int[] A, int target) 
    int begin = 0;
    int end = A.length-1;
    while(begin <= end) {
        int p = (end + begin) / 2;
        if(A[p] == target) return p;
        else if(A[p] < target) {
            begin = p + 1;
        }
        else {
            end = p - 1;
        }
    }
    return -1;
}

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Which one is better?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

There are no major differences between each other. The only thing you need to remember is the condition in the while loop

Time Complexity

Copyright © 直通硅谷

http://www.zhitongguigu.com/

T(n) = T(\frac{n}{2}) + k
T(n)=T(n2)+kT(n) = T(\frac{n}{2}) + k
T(n) = O(lgn)
T(n)=O(lgn)T(n) = O(lgn)

Insertion Position

Instead of giving -1 when search fails, give the index that the number could be inserted and maintain the order

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example: 1,2,4,6,7,8,9

input 4 return 2

input 5 return 3 (Previously is -1)

Insertion Position

public int BinarySearch(int[] A, int target) 
    int begin = 0;
    int end = A.length ;
    while(begin < end) {
        int p = (end + begin) / 2;
        if(A[p] == target) return p;
        else if(A[p] < target) {
            begin = p + 1;
        }
        else {
            end = p;
        }
    }
    return begin;
}

Search In Rotated Sorted Array

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example: 6 7 8 9 1 2 3 4 5

Search In Rotated Sorted Array

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Search In Rotated Sorted Array

public int search(int[] A, int target) {
    int begin = 0;
    int end = A.length;
    while(begin < end) {
        int p = (end + begin) / 2;
        if(A[p] == target) return p;
        else if(A[p] > A[begin]){
            if(target >= A[begin] && target < A[p]) {
                end = p;
            }
            else begin = p + 1;
        }
        else {
            if(target > A[p] && target <= A[end - 1] ) {
                begin = p + 1;
            }
            else end = p;
        }
    }
    return -1;
}

Search In Rotated Sorted Array II

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Search In Rotated Sorted Array II

public boolean search(int[] nums, int target) {
    int begin = 0;
    int end = nums.length;
    while(begin < end) {
        int p = (begin + end) / 2;
        if(nums[p] == target) return true;
        if(nums[p] > nums[begin]) {
            if(target >= nums[begin] && target < nums[p]) {
                end = p;
            }
            else begin = p + 1;
        }
        else if(nums[p] < nums[begin]) {
            if(target > nums[p] && target <= nums[end - 1] ) {
                begin = p + 1;
            }
            else end = p;                
        }
        else {
            begin ++;
        }        
    }
    return false;
}

Sqrt(x)

How does it relate to Binary Search?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Sqrt(x)

public int mySqrt(int x) {
    if(x <= 1) return x;
    Long begin = (long)0;
    Long end = (long)x / 2 + 1;
    while(begin < end) {
        Long p = (end + begin) / 2;
        if(p * p == x) return p.intValue();
        else if(p * p < x) {
            begin = p + 1;
        }
        else {
            end = p;
        }
    }
    return end.intValue() - 1;
}

Homework

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Homework

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Homework (Optional)

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Made with Slides.com