c语言 二进制压缩算法_使用C ++解释的二进制搜索算法

本文介绍了二进制搜索算法的工作原理,通过C++代码解释如何在有序数组中查找目标元素。通过不断缩小搜索范围,每次迭代消除一半的数组元素,直至找到目标值的索引。
摘要由CSDN通过智能技术生成

c语言 二进制压缩算法

by Pablo E. Cortez

由Pablo E.Cortez

使用C ++解释的二进制搜索算法 (Binary Search Algorithms Explained using C++)

Binary search is one of those algorithms that you’ll come across on every (good) introductory computer science class. It’s an efficient algorithm for finding an item in an ordered list. For the sake of this example we’ll just assume this is an array.

二进制搜索是您在每门(入门)计算机科学入门课程中都会遇到的算法之一。 这是一种用于在有序列表中查找商品的高效算法。 为了这个例子,我们只假设这是一个数组。

The goals of binary search is to:

二进制搜索的目标是:

  • be able to discard half of the array at every iteration

    每次迭代都能丢弃一半的数组
  • minimize the number of elements we have to go through

    最小化我们必须经历的元素数量
  • leave us with one final value

    给我们一个最终的价值

Take for example the following array of integers:

以下面的整数数组为例:

int array[] = {     1, 3, 4, 6, 7, 8, 10, 13, 14, 18, 19, 21, 24, 37, 40, 45, 71 };

Let’s say we are trying to find the index value of the number 7 in this array. There are 17 items in total and the index values go from 0 to 16.

假设我们正在尝试查找此数组中数字7的索引值。 共有17个项目,索引值从0到16。

We can see that the index value of 7 is 4, since it’s the fifth element in the array.

我们可以看到索引值为7,因为它是数组中的第五个元素。

But what would be the best way for the computer to find the index value of the number we are looking for?

但是,计算机找到我们要查找的数字的索引值的最佳方法是什么?

First, we store the min and max values, such as 0 and 16.

首先,我们存储minmax ,例如016

int min = 0;int max = 16;

Now we have to come up with a guess. The smartest thing to do would be to guess an index value in the middle of the array.

现在我们不得不猜测。 最明智的做法是猜测数组中间的索引值。

With the index value 0 to 16 in this array, the middle index value of this array would be 8. That holds the number 14.

在此数组的索引值为0到16的情况下,该数组的中间索引值为8。该数字为14。

// This will round down if the quotient is not an integerint guess = (min + max) / 2;

// This will round down if the quotient is not an integer int guess = (min + max) / 2;

Our guess is now equal to 8, which is 14 in the array, since array[8] is equal to 14 .

我们的猜测现在等于8,在数组中为14,因为array[8]等于14

If the number we were looking for was 14, we would be done!

如果我们要查找的数字是14,那么我们将完成!

Since that is not the case, we will now discard half of the array. These are all the numbers after 14, or index value 8, since we know that 14 is greater than 7, and our guess is too high.

既然不是这种情况,我们现在将丢弃数组的一半。 这些都是14或索引值8之后的所有数字,因为我们知道14大于7,我们的猜测太高了。

After the first iteration, our search is now within: 1, 3, 4, 6, 7, 8, 10, 13

第一次迭代后,我们现在的搜索范围是: 1, 3, 4, 6, 7, 8, 10, 13

We don’t have to guess in the last half of the original array, because we know that all those values are too big. That’s why it’s important that we apply binary search to an ordered list.

我们不必猜测原始数组的最后一半,因为我们知道所有这些值都太大。 这就是为什么将二进制搜索应用于有序列表很重要。

Since our original guess of 14 was greater than 7, we now decrease it by 1 and store that into max:

由于我们最初对14的猜测大于7,因此现在将其减少1,并将其存储到max

max = guess - 1; // max is now equal to 7, which is 13 in the array

Now the search looks like this:

现在搜索如下所示:

1, 3, 4, 6, 7, 8, 10, 13
min = 0max = 7guess = 3

Because our guess was too low, we discard the bottom half of the array by increasing the min, conversely to what we previously did to max:

因为我们的猜测太低,所以我们通过增加min来丢弃数组的下半部分,这与之前对max所做的相反:

min = guess + 1; // min is now 4

By the next iteration, we are left with:

在下一次迭代中,我们剩下:

7, 8, 10, 13min = 4max = 7guess = 5

Since index value 5 returns 8, we are now one over our target. We repeat the process again, and we are left with:

由于索引值5返回8,因此我们现在比目标高1。 我们再次重复该过程,然后剩下:

7min = 4max = 4guess = 4

And we are left with only one value, 4, as the index of the target number we were looking for, which was 7.

我们只剩下一个值4,即我们要寻找的目标编号的索引,即7。

The purpose of binary search is to get rid of half of the array at every iteration. So we only work on those values on which it makes sense to keep guessing.

二进制搜索的目的是在每次迭代时摆脱数组的一半。 因此,我们只处理那些值得继续猜测的值。

The pseudo-code for this algorithm would look something like this:

该算法的伪代码如下所示:

  1. Let min = 0 , and let max = n where n is the highest possible index value

    min = 0 ,令max = n ,其中n是可能的最高索引值

  2. Find the average of min and max , round down so it’s an integer. This is our guess

    找到minmax的平均值,将其四舍五入为整数。 这是我们的guess

  3. If we guessed the number, stop, we got it!

    如果我们猜到了数字,停下来,我们知道了!
  4. If guessis too low, set min equal to one more than guess

    如果guess值太低,则将min设置为比guess

  5. If guessis too high, set max equal to one less than guess

    如果guess值太高,则将max设置为比guess小1

  6. Go back to step two.

    回到第二步。

Here’s a solution, written in C++:

这是用C ++编写的解决方案:

翻译自: https://www.freecodecamp.org/news/what-is-binary-search-algorithm-c-d4b554418ac4/

c语言 二进制压缩算法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值