二分法搜索_分治和二分法搜索

二分法搜索

Divide and conquer is an approach in some common programming problems. One such case is finding the longest common prefix which involves developing a solution to find the longest common prefix string in an array of strings.

分而治之是解决一些常见编程问题的一种方法。 一种这样的情况是找到最长的公共前缀,这涉及开发一种解决方案以在字符串数组中找到最长的公共前缀字符串。

["international", "interstellar", "interruption"]inter

However, we will not dive into solving this problem in this article. We will explore an approach to solving this problem; divide and conquer and use an algorithm; binary search to illustrate how it works.

但是,在本文中,我们不会深入解决这个问题。 我们将探索解决这个问题的方法; 分治法 二进制搜索以说明其工作方式。

The Approach

该方法

Divide and Conquer is a technique of breaking a problem into simpler problems that are easier to solve and combining the solutions to solve the original problem.

分而治之是一种将问题分解为更容易解决的简单问题,并将解决方案组合起来以解决原始问题的技术。

Let’s take a problem such as arranging two decks of cards. After playing a game of cat and mouse with a friend, it’s time to split your two decks of cards. In order to arrange the cards at a fast rate, you split the pile of cards with your friend and both of you sort out the mess. The original problem in this case, is arranging the pile of cards and the simpler problems are arranging smaller piles of cards between the players.

让我们来解决一个问题,例如安排两副牌。 与朋友一起玩猫和老鼠游戏后,该分割两副纸牌了。 为了快速整理卡片,您需要与您的朋友分开堆卡片,然后俩人都弄得一团糟。 在这种情况下,最初的问题是排列一堆纸牌,而较简单的问题是在玩家之间排列较小的纸牌。

Divide and conquer is usually described as involving steps.

分而治之通常被描述为涉及步骤。

Divide step, breaking a problem into simpler problems.

划分步骤,将问题分解为更简单的问题。

Conquer step, solving the simpler problems usually referred to as sub problems. In this step we repeat a process of solving the problem until a condition is met, this condition is referred to as the base case.

征服步骤,解决通常称为子问题的较简单问题。 在此步骤中,我们重复解决问题的过程,直到满足条件为止,此条件称为基本情况。

Combine step, involving putting the solved sub problems together.

合并步骤,涉及将已解决的子问题放在一起。

One variation, of this approach is Decrease and Conquer. In this variant, the original problem is broke up into one simpler problem.

这种方法的一种变体是“ 减少并征服”。 在此变体中,原始问题分解为一个更简单的问题。

Let’s say you have your deck arranged at this point in the article and you want to show your friend your favorite card. Holding the deck of cards, you could start from the first card on top in whichever direction, looking for your favorite card. This would work, but it would be quite inefficient if you have to go through all cards. This is linear search. How would you do it better?

假设您在本文的这一点上已经安排好牌组,并且想向您的朋友展示您最喜欢的卡片。 握住纸牌组,您可以从顶部的第一张纸牌开始向任意方向寻找自己喜欢的纸牌。 这会起作用,但是如果您必须仔细检查所有卡片,效率将会非常低下。 这是线性搜索。 您会如何做得更好?

If your favorite card is seven spades. You could split the deck in half, check to see your current position. If the cards on one side are less than seven you would continue searching on the other split deck. Repeating this process would eventually lead you to find your card in fewer steps. That’s if your card isn’t mixed up in the previous deck or lost! This is binary search.

如果您最喜欢的卡是七个黑桃。 您可以将卡座一分为二,查看当前位置。 如果一侧的纸牌少于七张,您将继续在另一张分离的纸牌上搜索。 重复此过程最终将使您以更少的步骤查找卡片。 就是说您的卡没有在上一盘中混在一起或丢失了! 这是二进制搜索。

The Algorithm

算法

Binary search is an algorithm that searches in a sorted collection for a target. It works by comparing the target to the middle element in a collection. If the target is greater than the middle element, the left elements are discarded. If the target is less than the middle element the right elements are discarded. This process is repeated until the middle element is equal to the target and if no further splitting can be done. The target does not exist in the list.

二进制搜索是一种在排序的集合中搜索目标的算法。 它通过将目标与集合中的中间元素进行比较来工作。 如果目标大于中间元素,则丢弃剩余元素。 如果目标小于中间元素,则丢弃正确的元素。 重复此过程,直到中间元素等于目标,并且如果无法进行进一步拆分。 目标在列表中不存在。

This process of repeating the same algorithm in sub problems is commonly referred to as recursion.

在子问题中重复相同算法的过程通常称为递归。

Assuming we have a collection of elements, from a popular sequence. The target is 21. We would split the collection as shown below.

假设我们有一个流行序列中的元素集合。 目标是21。我们将如下所示拆分集合。

+---------------------------------+--------------+---------+
| Collection | Middle Index | Element |
+---------------------------------+--------------+---------+
| 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 | 4 | 3 |
+---------------------------------+--------------+---------+
| 8,13,21,34 | 2 | 13 |
+---------------------------------+--------------+---------+
| 21,34 | 1 | 21 |
+---------------------------------+--------------+---------+

You will notice when we split an odd number, we round it down. To eliminate elements on a side we change the position of our indexes. Think of it this way, if our target will be in our less than section, the end index should be one position in front of the middle position. In-front because we want to ignore all elements behind our current position and one position more s we already know the middle position is not equal to the target. The same logic applies when the target is in greater than split, we bring the start index one position behind the middle position.

您会注意到,当我们分解一个奇数时,我们会将其舍入。 为了消除一侧的元素,我们更改了索引的位置。 这样想,如果目标位于小于部分,则结束索引应该在中间位置前面一个位置。 在前端,因为我们要忽略当前位置后面的所有元素,并且还要忽略一个位置,所以我们已经知道中间位置不等于目标位置。 当目标大于拆分时,将应用相同的逻辑,即我们将起始索引置于中间位置之后一个位置。

The Code

代码

Binary search can be done implemented in an iterative approach or a recursive approach.

二进制搜索可以通过迭代方法或递归方法来实现。

Iterative Binary Search
迭代二进制搜索
Recursive Binary Search
递归二进制搜索

The Prestige

威望

Divide and conquer is an approach of breaking down a problem into sub problems and combining the solutions. In binary search, on a sorted array of numbers, we divide (decrease) the array of numbers(search space), conquer the sub problems by recursively looking at the middle point for our target and splitting until we find our target the base case condition.

分而治之是一种将问题分解为子问题并结合解决方案的方法。 在二分查找中,在排序的数字数组上,我们划分(减少)数字数组(搜索空间),通过递归地查看目标的中点并拆分直到找到目标基本情况,从而克服了子问题。 。

Note binary search works on a sorted collection of elements. This means it’s performance is good when the lost is sorted and terrible otherwise. Let’s wrap up with some complexity analysis.

请注意,二进制搜索适用于元素的排序集合。 这意味着当丢失的物品经过分类处理时,否则性能会很好。 让我们结束一些复杂性分析。

In our example on retrieving our favorite card from a deck of card, we reduce the number of cards we have to search through each time we split the deck. In a standard deck of 52 cards, on our first split we reduce the deck to 26 cards. On our second split we reduce the cards to 13 cards and proceeding to the next splits until we find our intended card. It would take at most six(6) splits to find our card.

在我们从一副纸牌中检索我们最喜欢的纸牌的示例中,我们减少了每次拆分纸牌时必须搜索的纸牌数量。 在标准的52张卡片组中,在我们的第一次拆分中,我们将卡片组减少到26张。 在第二个拆分中,我们将牌减少到13张,然后进行下一个拆分,直到找到想要的牌。 最多需要六(6)个分割才能找到我们的卡。

If we use our original to two decks situation, we have 104 cards initially assuming the cards are sorted as one deck. We would have one more step to get our cards to 52 and proceed, at most seven(7)splits. Each time we double our cards(search space) we add one more split. Pretty sweet.

如果我们使用原始到两副牌的情况,那么假设卡片被分类为一个副牌,则最初有104张牌。 我们还要再迈一步,将卡数提高到52,然后最多进行七(7)次拆分。 每次将卡片翻倍(搜索空间)时,都会再增加一个分割。 很甜

The Icing

结冰

This patterns and situations can be represented mathematically. For a collection of elements n , we need m number of splits to get our target. Thus a double in our collection 2n results in m+1 splits.

这种模式和情况可以用数学表示。 对于元素n的集合,我们需要m个分割才能获得目标。 因此,我们的集合2 n中的double导致m + 1个拆分。

A mathematically function that represents such change is logarithms, if you need some more explanation on this, I recommend this video.

代表这种变化的数学函数是对数,如果您需要更多对此的解释,我推荐此视频

To see it clearly, if you have a collection of elements which are to the power of two that is 1,2,4,8,16.The result of using the logarithm function to base two(2) is equal to m. Thus m+1 is the maximum number of splits we would require to find our target. Ready for the cherry.

为了清楚地看到它,如果您具有一个以1,2,4,8,16为二的幂的元素集合。使用对数函数使二(2)为基的结果等于m。 因此, m +1是找到目标所需的最大拆分数。 准备樱桃。

+----+--------------+
| n | log base 2 n |
+----+--------------+
| 1 | 0 |
+----+--------------+
| 2 | 1 |
+----+--------------+
| 4 | 2 |
+----+--------------+
| 8 | 3 |
+----+--------------+
| 16 | 4 |
+----+--------------+

The cherry

樱桃

To calculate the time complexity of binary search we can apply our knowledge seen in this case. Therefore in the worst case binary search worst time complexity is O(log n).

为了计算二进制搜索的时间复杂度,我们可以应用在这种情况下看到的知识。 因此,在最坏的情况下,二进制搜索的最差时间复杂度是O(log n)。

Enjoy!

请享用!

升级编码 (Level Up Coding)

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程

翻译自: https://medium.com/@Daniel_Alpine/divide-conquer-and-binary-search-f3645e43f05d

二分法搜索

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值