尋找演算法的總結

來源:互聯網
上載者:User

尋找演算法從總體上來說可以分為四類,它們分別是順序尋找,二分尋找,分塊尋找以及散列表。下面簡單的介紹一下各種尋找演算法。

(一)順序尋找

原理:讓關鍵字與隊列中的數從第一個(或最後一個)開始逐個進行比較,直到找出與關鍵字相同的數為止,否則尋找失敗。

順序尋找適用於線性表的順序儲存結構和鏈式儲存結構。

演算法如下所示(其中ElemType為資料類型):

int search(ElemType array[],int n,ElemType key){for(int i=0;i<n;i++)if(array[i]==key)break;if(i<n)return i;elsereturn -1;}

(二)二分尋找

二分尋找也稱之為折半尋找,它要求資料採用順序儲存結構,並且待排序序列是有序數列。

原理:將表中間的位置記錄的關鍵字與所尋找的關鍵字進行比較,如果相等則尋找成功,否則利用中間位置將表分為前後兩個子表,根據中間位置記錄的關鍵字與尋找關鍵字的大小關小確定接下來尋找哪個子表,重複上述過程,直到找到滿足條件的記錄,尋找成功,否則尋找失敗。

演算法實現如下:

#include "stdafx.h"#include<iostream>using namespace std;int binarysearch(int *array,int start,int end,int key);int _tmain(int argc, _TCHAR* argv[]){int count,blockcount;int key;int i,j;cout<<"input the number of digits:"<<endl;cin>>count;cout<<"input details of digits:"<<endl;int *point=new int[count];for(int i=0;i<count;i++){cin>>point[i];}cout<<"input the key to search:"<<endl;cin>>key;cout<<"the key is in position "<<binarysearch(point,0,count-1,key);delete[] point;return 0;}int binarysearch(int *array,int start,int end,int key){int low=start;int high=end;int mid;if(low>high){cout<<"尋找範圍有錯"<<endl;return -1;}while(low<=high){mid=(low+high)/2;if(*(array+mid)==key){return mid+1;}else if(*(array+mid)<key){low=mid+1;}else{high=mid-1;}}return -1;}

運行結果如下所示:

(三)分塊尋找

分塊尋找又稱之為索引順序尋找,是順序尋找的一種改進。

該方法是將n個資料按塊有序劃分為m個子塊(m<n),每一個字塊中的資料不需要有序,但是塊與塊之間需要有序,即第一塊中的所有元素都必須小於(或者大於)第二塊中的任何一個資料,第二塊中的所有元素都必須小於(或者大於)第三塊中的任何一個資料,以此類推。

原理:先取出各塊中的最大關鍵字構成索引表,再對索引表進行二分尋找或者順序尋找,以確定待查記錄在那個子塊中,最後在已經確定的子塊中按順序尋找的方式進行尋找。

我們首先可以定義一個結構體如下所示:

struct block{int key;//儲存塊中最大記錄int start;//塊的起始索引int end;//塊的結束索引};

完整的演算法實現如下:

#include "stdafx.h"#include<iostream>using namespace std;struct block{int key;//儲存塊中最大記錄int start;//塊的起始索引int end;//塊的結束索引};int _tmain(int argc, _TCHAR* argv[]){int count,blockcount;int key;int i,j;cout<<"input the number of digits:"<<endl;cin>>count;cout<<"input details of digits:"<<endl;int *point=new int[count];for(int i=0;i<count;i++){cin>>point[i];}cout<<"input the key to search:"<<endl;cin>>key;cout<<"input the number of blocks:"<<endl;cin>>blockcount;block *table=new block[blockcount];cout<<"input the detais of each block:"<<endl;for(int i=0;i<blockcount;i++){cin>>table[i].key>>table[i].start>>table[i].end;}i=0;while(i<blockcount&&table[i].key<key){i++;}if(i>=blockcount)return 0;for(j=table[i].start;j<=table[i].end;j++){if(point[j]==key)break;}if(j<=table[i].end)cout<<"the key is in "<<j+1<<endl;delete[] point;delete[] table;return 0;}

程式運行結果如下所示:


(歡迎轉載,但請標註轉載連結....謝謝)

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.