0% found this document useful (0 votes)
2 views

2. Binary Search Sorted Array

The document provides an overview of binary search techniques applied to sorted arrays, including classical binary search and its implementation. It includes examples of how to find elements within a sorted array and discusses various related problems such as searching in a 2D matrix and finding the minimum in a rotated sorted array. Additionally, it offers links to further resources and problems for practice.

Uploaded by

Vincenzo Frank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2. Binary Search Sorted Array

The document provides an overview of binary search techniques applied to sorted arrays, including classical binary search and its implementation. It includes examples of how to find elements within a sorted array and discusses various related problems such as searching in a 2D matrix and finding the minimum in a rotated sorted array. Additionally, it offers links to further resources and problems for practice.

Uploaded by

Vincenzo Frank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

课程尚未开始

请大家耐心等待
关注微信公共账号,获得最新面试题信息及解答

Facebook: http://www.facebook.
com/ninechapter

Weibo:
http://www.weibo.com/ninechapter
Binary Search & Sorted Array
九章算法IT求职面试培训课程 第2章
www.ninechapter.com
Binary Search
Classical Binary Search
Given an sorted integer array - nums, and an
integer - target. Find the any/first/last
position of target in nums, return -1 if target
doesn’t exist.

public int binarySearch(int[] nums, int target)


How we do binary search?
start end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5
How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4
How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!


How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!

2. Find 8, mid=4, 2, 3. Find it!


How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!

2. Find 8, mid=4, 2, 3. Find it!

3. Find 14, mid=4


How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!

2. Find 8, mid=4, 2, 3. Find it!

3. Find 14, mid=4, 6


How we do binary search?
start mid end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!

2. Find 8, mid=4, 2, 3. Find it!

3. Find 14, mid=4, 6, 5


How we do binary search?
start, mid
end

index 0 1 2 3 4 5 6 7 8

nums 2 3 5 8 13 21 34 55 89

1. Find 5, mid=4, 2. Find it!

2. Find 8, mid=4, 2, 3. Find it!

3. Find 14, mid=4, 6, 5, 4. Return -1


Recursion or While-Loop?
Binary Search
A generic binary search template
http://www.lintcode.com/en/problem/binary-search/
http://www.ninechapter.com/solutions/binary-search/
Keypoints:
1. start + 1 < end
2. start + (end - start) / 2
3. A[mid] ==, <, >
4. A[start] A[end] ? target
Search for a range
http://lintcode.com/en/problem/search-for-a-range/

http://www.ninechapter.com/solutions/search-for-a-
range/
Search Insert Position
http://lintcode.com/en/problem/search-insert-
position/
http://www.ninechapter.com/solutions/search-insert-
position/
Search a 2D Matrix
http://www.lintcode.com/en/problem/search-a-2d-
matrix/

http://www.ninechapter.com/solutions/search-a-2d-
matrix/
Search a 2D Matrix II
http://lintcode.com/en/problem/search-a-2d-matrix-
ii/
[ 0, 1, 2, 4]
[ 1, 2, 6, 9]
[ 3, 5, 7,10]
[ 7, 8, 9,11]
Search a 2D Matrix II

Quadrate Search
[ 2, 4 ]
[ 0, 1, 2, 4] [ 0, 1, 2, 4] [ 5, 7 ]
[ 1, 2, 5, 7] find 7 [ 1, 2, 5, 7]
[ 3, 5 ]
[ 3, 5, 7,10] [ 3, 5, 7,10]
[ 7, 8 ]
[ 7, 8, 9,11] [ 7, 8, 9,11]
[ 7, 10]
[ 9, 11]
Search in a 2D Matrix
Search in a 2D Matrix
Search in a 2D Matrix
First Bad Version
http://lintcode.com/en/problem/first-bad-version/
Find Peak Element
http://lintcode.com/en/problem/find-a-peak/
5 minutes break
Search in Rotated Sorted Array
http://lintcode.com/en/problem/search-in-rotated-
sorted-array/

http://www.ninechapter.com/solutions/search-in-
rotated-sorted-array/
Search in Rotated Sorted Array II

http://lintcode.com/en/problem/search-in-rotated-
sorted-array-ii/

Linear algorithm, go for-loop!


Find Minimum in Rotated
Sorted Array
http://www.lintcode.com/zh-cn/problem/find-
minimum-in-rotated-sorted-array/
Find Minimum in Rotated
Sorted Array II
http://www.lintcode.com/zh-cn/problem/find-
minimum-in-rotated-sorted-array-ii/
Sorted Array
Remove Duplicates from Sorted Array
I, II
http://lintcode.com/en/problem/remove-duplicates-from-sorted-array/
http://lintcode.com/en/problem/remove-duplicates-from-sorted-array-
ii/
Merge Sorted Array
http://lintcode.com/en/problem/merge-sorted-array/
Merge Sorted Array II
http://lintcode.com/en/problem/merge-sorted-array-
ii/

http://www.ninechapter.com/solutions/merge-sorted-
array/
Median of Two Sorted Arrays
http://lintcode.com/en/problem/median-of-two-
sorted-arrays/

http://www.ninechapter.com/solutions/median-of-
two-sorted-arrays/
Recover Sorted Array
http://lintcode.com/problem/recover-rotated-
sorted-array/
Rotate String
http://lintcode.com/en/problem/rotate-string/
Reverse Words in a String
http://lintcode.com/en/problem/reverse-words-in-a-
string/
Conclusion
Binary Search
-- Exclude half every time

Sorted Array
-- If array is sorted, try binary search
-- If array is not sorted, try sort it first

You might also like