二进制搜索算法
Binary Search is the most popular Searching Algorithm which is most asked in coding interviews. Its popularity is because of it’s time complexity, where the linear search algorithm takes O(N) time, the Binary Search takes O(log N) time. The only condition in Binary Search is that the array should be sorted.
二进制搜索是最流行的搜索算法,在编码面试中要求最多。 它的流行是因为它具有时间复杂性,其中线性搜索算法需要O(N)时间,二进制搜索需要O(log N)时间。 Binary Search中的唯一条件是应对数组进行排序。
In this article, I am going to solve a tricky problem of Binary Search asked in LeetCode called “Find the minimum element in a Rotated and Sorted array” or “Find the Pivot Element in the Rotated and Sorted array”
在本文中,我将解决一个棘手的问题,即在LeetCode中提出的二进制搜索问题称为“在旋转和排序数组中查找最小元素”或“在旋转和排序数组中查找枢轴元素”
This is a tricky problem because it cannot be solved by the standard Binary Search Algorithm since the array is rotated.
这是一个棘手的问题,因为标准的二进制搜索算法无法解决此问题,因为该数组已旋转。
问题陈述: (Problem Statement:)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).Find the minimum element.Input: [3,4,5,1,2]
Output: 1
问题的方法: (Problem’s Approach:)
The reason why Binary Search provides O(log N) time complexity is that on each iteration it eliminates half of the array and searches the element in the remaining half.
二进制搜索提供O(log N)时间复杂度的原因是,每次迭代都会消除数组的一半,并在剩余的一半中搜索元素。
We need to implement the same approach here and find a way to eliminate half of the array in each iteration.
我们需要在此处实现相同的方法,并找到一种在每次迭代中消除一半数组的方法。
问题的解决方案: (Problem’s Solution:)
Let us look at this rotated array example carefully:
让我们仔细看一下这个旋转数组的例子:
Example: [4,5,6,7,0,1,2] // minimum element 0 at index 5
By observing the array we can conclude two points:
通过观察数组,我们可以得出两点结论:
- The index of the minimum element is the same as the number of rotations of the array 最小元素的索引与数组的转数相同
- The minimum element is smaller than its adjacents最小元素小于其相邻元素
But How do we eliminate one half of the array through Binary Search?
但是,如何通过二进制搜索消除数组的一半呢?
The approach is similar to the standard binary search where we find the middle element of the array but the condition changes. Hence by modifying the standard Binary Search Algorithm we get the solution.
该方法类似于标准二进制搜索,在标准二进制搜索中,我们找到了数组的中间元素,但条件发生了变化。 因此,通过修改标准的二进制搜索算法,我们得到了解决方案。
When we find the middle element ( in the above example number 7 is the middle element ) we can observe that the array is divided into two halves where one half is sorted and the other half is unsorted. Another interesting observation is that the minimum element is always in the unsorted half.
当我们找到中间元素(在上面的示例中,数字7是中间元素)时,我们可以观察到数组被分为两半,其中一半被排序而另一半未被排序。 另一个有趣的发现是,最小元素始终位于未排序的一半中。
Algorithm:
算法:
- Find the middle element of the array. 找到数组的中间元素。
- Check if the middle element is the minimum element 检查中间元素是否为最小元素
- If the middle element does not satisfy the minimum element condition then apply binary search on the unsorted half of the array.如果中间元素不满足最小元素条件,则对数组的未排序部分应用二进制搜索。
Code Implementation in Javascript:
Java代码实现:
Step 1: Set left and right values. In this problem, the left value is 0 and the right value is the length of the array.
步骤1:设定左右数值。 在此问题中,左值为0,右值为数组的长度。
let left = 0;
let right = nums.length - 1;
Step 2: Find the middle element of the array and check for the unsorted half of the array.
步骤2:找到数组的中间元素,然后检查数组的未排序部分。
const mid = Math.floor((left + right) / 2);if (arr[mid] > arr[right]) {
left = mid + 1;
} else {
right = mid;
}
When the middle value is greater than the rightmost value of the array, it shows that the right half of the array is unsorted, or else the left side is unsorted.
当中间值大于数组的最右值时,表明数组的右半部分未排序,否则左侧未排序。
Step 3: Return the answer. The loop breaks when the left is greater than or equal to the right value. This happens when we have found the minimum element in the array as both left and right are equal we return the left index.
步骤3:返回答案。 当left大于或等于right值时,循环中断。 当我们在数组中找到最小元素时(左和右相等),就会发生这种情况,因为我们返回左索引。
return arr[left];
最终解决方案: (Final Solution:)
var findMinelement = function(arr) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] > arr[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return arr[left];
};
Thank you for reading the article till the end, hope this article will help you in your coding preparations. All the best!!!
感谢您阅读本文直到最后,希望本文对您的编码准备有所帮助。 祝一切顺利!!!
二进制搜索算法