python中二进制表示_Python中的二进制搜索:直观介绍

python中二进制表示

欢迎 (Welcome)

In this article, you will learn how the Binary Search algorithm works behind the scenes and how you can implement it in Python.

在本文中,您将学习Binary Search算法在幕后的工作方式以及如何在Python中实现它。

In particular, you will learn:

特别是,您将学到:

  • How the algorithm works behind the scenes to find a target element.

    该算法如何在后台工作以找到目标元素。
  • How its Python implementation works line by line.

    它的Python实现是如何逐行进行的。
  • Why it is a very efficient algorithm compared to Linear Search.

    与线性搜索相比,为什么它是一种非常有效的算法。
  • Its advantages and requirements.

    其优点和要求。

Let's begin! ✨

让我们开始!

This algorithm is used to find an element in an ordered sequence (for example: a list, tuple, or string).

该算法用于查找有序序列中的元素(例如:列表,元组或字符串)。

要求 (Requirements)

To apply the Binary Search algorithm to a sequence, the sequence already has to be sorted in ascending order. Otherwise, the algorithm will not find the correct answer. If it does, it will be by pure coincidence.

要将二进制搜索算法应用于序列,该序列必须已经按升序排序。 否则,算法将找不到正确的答案。 如果确实如此,那纯属巧合。

💡 Tip: You can sort the sequence before applying Binary Search with a sorting algorithm that meets your needs.

提示:您可以在使用符合您需要的排序算法应用Binary Search之前对序列进行排序。

输入输出 (Input and Output)

The algorithm (implemented as a function) needs this data:

该算法(作为函数实现)需要以下数据:

  • An ordered sequence of elements (for example: list, tuple, string).

    元素的有序序列(例如:列表,元组,字符串)。
  • The target element that we are searching for.

    我们正在搜索的目标元素。

It returns the index of the element that you are looking for if it's found. If the element is not found, -1 is returned.

如果找到该元素,它将返回您要寻找的元素的索引 。 如果找不到该元素,则返回-1。

效率 (Efficiency)

It is very efficient compared to Linear Search (searching for an element one by one, starting from the first one) because we are able to "discard" half of the list on every step.

与线性搜索(从第一个开始一个一个地搜索一个元素)相比,它非常有效,因为我们能够在每一步上“丢弃”列表的一半。

Let's start diving into this algorithm.

让我们开始研究该算法。

🔹视觉演练 (🔹 Visual Walkthrough)

We will apply the Binary Search algorithm to this list:

我们将二进制搜索算法应用于此列表:

💡 Tip: Notice that the list is already sorted. It included the indices as a visual reference.

💡 提示:请注意,列表已经排序。 它包括索引作为视觉参考。

目标 (Goal)

We want to find the index of the integer 67.

我们想找到整数67的索引。

间隔 (Interval)

Let's pretend that we are the algorithm. How do we start the process?

假设我们是算法。 我们如何开始这个过程?

We start by selecting the two bounds of the interval where we want to search. We want to search the entire list, so we select index 0 as the lower bound and index 5 as the upper bound:

我们首先选择要搜索的区间的两个边界。 我们要搜索整个列表,因此我们选择索引0作为下限,选择索引5作为上限:

中间元素 (Middle Element)

Now we need to find the index of the middle element in this interval. We do this by adding the lower bound and the upper bound and dividing the result by 2 using integer division.

现在我们需要找到此间隔中中间元素的索引。 为此,我们将上下限相加,然后使用整数除法将结果除以2。

In this case, (0 + 5)//2 is 2 because the result of 5/2 is 2.5 and integer division truncates the decimal part.

在这种情况下, (0 + 5)//22因为5/2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值