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)
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/
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;
}
Find first occurrence of a target in a sorted array
idx | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
Array[] | 6 | 7 | 7 | 7 | 7 | 7 |
Find last occurrence of a target in a sorted array
first iteration: L = 0, R = 6, M = 3 => R = M = 3
second iteration: L = 0, R = 3, M = 1 => R = M = 1
L is neighbor of R, break;
Post processing
int binarySearch(vector<int>& a, int target){
int left = 0, right = a.size() - 1,
int mid;
while(left < right - 1){
int mid = left + (right - left)/2;
if(a[mid] == target){
left = mid;
}
else if(a[mid] < target){
left = mid;
}
else {
right = mid;
}
}
if(left == target) return left;
if(right == target) return right;
return -1;
}
int binarySearch(vector<int>& a, int target){
int left = 0, right = a.size() - 1,
int mid;
while(left < right - 1){
int mid = left + (right - left)/2;
if(a[mid] == target){
right = mid;
}
else if(a[mid] < target){
left = mid;
}
else {
right = mid;
}
}
if(right == target) return right;
if(left == target) return left;
return -1;
}
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/
[GoValley-201612] Binary Search
By govalley201612
[GoValley-201612] Binary Search
- 685