c語言 二進制壓縮算法_使用C ++解釋的二進制搜索算法

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語言 二進制壓縮算法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395657.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395657.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395657.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【LATEX】個人版latex論文模板

以下是我的個人論文模板,運行環境為Xelatex(在線ide:Sharelatex.com) 鑒于本人常有插入程序的需求,故引用了lstlisting \RequirePackage{ifxetex} \ifxetex\documentclass[hyperref, UTF8, c5size, no-math, winfonts,a4paper]{ct…

初識virtual memory

一、先談幾個重要的東西 virtual memory是一個抽象概念,書上的原文是"an abstraction of main memory known as virtual memory"(參考資料p776)。那么什么是抽象概念。下面說說我個人對這個東西的理解。 所謂抽象概念是指抽象出來的…

java創建mysql驅動,JDBC之Java連接mysql實現增刪改查

使用軟件:mysql、eclipse鏈接步驟:1.注冊驅動2.創建一個連接對象3.寫sql語句4.執行sql語句并返回一個結果或者結果集5.關閉鏈接(一般就是connection、statement、setresult)這三個連接對象,關閉順序一般是(setresult ---> statement …

算法第五章作業

1.你對回溯算法的理解(2分) 回溯法(探索與回溯法)是一種選優搜索法,又稱為試探法,按選優條件向前搜索,以達到目標。但當探索到某一步時,發現原先選擇并不優或達不到目標,…

c++編碼風格指南_100%正確的編碼樣式指南

c編碼風格指南Here are three links worth your time:這是三個值得您花費時間的鏈接: The 100% correct coding style guide (4 minute read) 100%正確的編碼樣式指南( 閱讀4分鐘 ) I wrote a programming language. Here’s how you can, too (10 minu…

xp開機黑屏故障分析

今天裝完xp系統之后,重啟開機發現竟然黑屏了,查資料發現有很多用戶在修改分辨率后,因顯示器不支持修改后的分辨率,會出現電腦黑屏的情況。分辨率調高了,超出了屏幕的范圍,肯定會黑屏,而且這個問…

應用程序圖標_如何制作完美的應用程序圖標

應用程序圖標by Nabeena Mali通過Nabeena Mali 如何制作完美的應用程序圖標 (How to Make the Perfect App Icon) With just 24 app icon slots on the first page of an iPhone home screen, or 28 if you have a fancy iPhone 7, creating the perfect app icon is a vital …

Luogu3702 SDOI2017 序列計數 矩陣DP

傳送門 不考慮質數的條件,可以考慮到一個很明顯的$DP:$設$f_{i,j}$表示選$i$個數,和$mod\ pj$的方案數,顯然是可以矩陣優化$DP$的。 而且轉移矩陣是循環矩陣,所以可以只用第一行的數字代替整個矩陣。當然了這道題$p \leq 100$矩陣…

java閏年的年份,Java案例-判斷給定年份是閏年

專注學子高考志愿填報,分享你所不知道信息。Java案例-判斷給定年份是閏年案例描述編寫程序,判斷給定的某個年份是否是閏年。閏年的判斷規則如下:(1)若某個年份能被4整除但不能被100整除,則是閏年。(2)若某個年份能被400整除&#…

通過path繪制點擊區域

通過path繪制點擊區域 效果 源碼 https://github.com/YouXianMing/Animations // // TapDrawImageView.h // TapDrawImageView // // Created by YouXianMing on 16/5/9. // Copyright © 2016年 YouXianMing. All rights reserved. //#import <UIKit/UIKit.h> #…

Raft與MongoDB復制集協議比較

在一文搞懂raft算法一文中&#xff0c;從raft論文出發&#xff0c;詳細介紹了raft的工作流程以及對特殊情況的處理。但算法、協議這種偏抽象的東西&#xff0c;僅僅看論文還是比較難以掌握的&#xff0c;需要看看在工業界的具體實現。本文關注MongoDB是如何在復制集中使用raft協…

db2 前滾會話

前滾會話 - CLP 示例ROLLFORWARD DATABASE 命令允許每次指定多個操作&#xff0c;各個操作由關鍵字 AND 隔開。例如&#xff0c;要前滾至日志末尾&#xff0c;然后完成&#xff0c;可將下列獨立的命令&#xff1a;db2 rollforward db sample to end of logsdb2 rollforward db …

史上最爛代碼_歷史上最大的代碼庫

史上最爛代碼Here’s a diagram of the biggest codebases in history, as measured by lines of code:這是歷史上最大的代碼庫的圖表&#xff0c;以代碼行來衡量&#xff1a; As you can see, Google has by far the largest codebase of all. And all 2 billion lines of co…

php添加jpeg,PHP-如何將JPEG圖像保存為漸進JPEG?

我具有以下將JPEG保存為漸進JPEG的功能.它已保存,但不是漸進式JPEG.這個對嗎 &#xff1f;function save($filename, $image_type IMAGETYPE_JPEG, $compression 75, $permissions null) {if ($image_type IMAGETYPE_JPEG) {imageinterlace($this->image, true); //conv…

Mysql添加字段.md

alter table td_user add gender bit DEFAULT 0 COMMENT 性別; 轉載于:https://www.cnblogs.com/bihanghang/p/10167446.html

推薦兩款實用工具——hcache和SQLPad

hcacheLinux用戶可能經常遇到的一個問題是內存大部分都被Buff和Cache占用了&#xff0c;但是有時候我們想知道到底Cache了些什么內容卻沒有一個直觀好用的工具。今天給你介紹一個可以查看Linux當前緩存了哪些文件的小工具hcache。hcache是基于pcstat的&#xff0c;pcstat可以查…

如何構建設計系統

by Colm Tuite通過Colm Tuite 如何構建設計系統 (How to construct a design system) 設計和構建一致的設計系統的技巧。 (Tips for designing and building a consistent design system.) Without doubt, I get asked about design systems more than anything else. So, hav…

matlab中get和set命令,關于matlab中get和set的用法

求極值點我現在知道有兩種方法&#xff1a;建立一個fun.m文件&#xff1a;function fxfun(x)fxsin(x)然后在命令窗口中調用&#xff1a;zfmax(fun,[0,pi/2])%同樣的函數還有zfmin(fun,...[0,pi/2]),zfzero(fun,0.5).zfsolve(fun,x0,option)...方程組求解&#xff0c;x0是求根過…

jmeter學習筆記(一)

1.添加JSON Path Extractor >>下載地址&#xff1a;http://jmeter-plugins.org/downloads/all/&#xff0c;下載 JMeterPlugins-ExtrasLibs-X.X.X.zip下載 >>解壓&#xff0c;將lib和lib/ext中的jar包放到安裝目錄對應位置&#xff0c;重啟。 2.參數不能輸入中文&…

docker mysql.sock,Docker mysql主從配置

Docker mysql主從配置一&#xff1a;Mysql基于Docker的主從復制搭建1&#xff1a;安裝docker&#xff0c;安裝步驟可見我之前的文章&#xff1a;Docker-常用基建的安裝與部署docker ps 命令查詢當前的容器狀態&#xff0c;這就是我們最后要達到的效果。2&#xff1a;首先拉取my…