0045演算法筆記——【隨機化演算法】舍伍德隨機化思想搜尋有序表

來源:互聯網
上載者:User

     問題描述

     用兩個數組來表示所給的含有n個元素的有序集S。用value[0:n]儲存有序集中的元素,link[0:n]儲存有序集中元素在數組value中位置的指標(實際上使用數組類比鏈表)。link[0]指向有序集中的第一個元素,集value[link[0]]是集合中的最小元素。一般地,如果value[i]是所給有序集S中的第k個元素,則value[link[i]]是S中第k+1個元素。S中元素的有序性表現為,對於任意1<=i<=n有value[i]<=value[link[i]]。對於集合S中的最大元素value[k]有,link[k]=0且value[0]是一個大數。

    例:有序集S={1,2,3,5,8,13,21}的一種表現方式:


    搜尋思想

     對於有序鏈表,可採用順序搜尋的方式在所給的有序集S中搜尋值為x的元素。如果有序集S中含有n個元素,則在最壞的情況下,順序搜尋演算法所需的計算時間為O(n)。利用數組下標的索引性質,可以設計一個隨機化搜尋演算法,一改進演算法的搜尋時間複雜性。演算法的基本思想是,隨機抽取數組元素若干次,從較接近搜尋元素x的位置開始做順序搜尋。如果隨機搜尋數組元素k次,則其後順序搜尋所需的平均比較次數為O(n/k+1)。因此,如果去k=|sqrt(n)|,則演算法所需的平均計算時間為(Osqrt(n))。

    隨機化思想下的有序表實現具體代碼如下:

   1、RandomNumber.h

#include"time.h"//隨機數類const unsigned long maxshort = 65536L;const unsigned long multiplier = 1194211693L;const unsigned long adder = 12345L;class RandomNumber{private://當前種子unsigned long randSeed;public:RandomNumber(unsigned long s = 0);//建構函式,預設值0表示由系統自動產生種子unsigned short Random(unsigned long n);//產生0:n-1之間的隨機整數double fRandom(void);//產生[0,1)之間的隨機實數};RandomNumber::RandomNumber(unsigned long s)//產生種子{if(s == 0){randSeed = time(0);//用系統時間產生種子}else{randSeed = s;//由使用者提供種子}}unsigned short RandomNumber::Random(unsigned long n)//產生0:n-1之間的隨機整數{randSeed = multiplier * randSeed + adder;//線性同餘式return (unsigned short)((randSeed>>16)%n);}double RandomNumber::fRandom(void)//產生[0,1)之間的隨機實數{return Random(maxshort)/double(maxshort);}

    2、7d3d2.cpp

//隨機化演算法搜素有序表#include "stdafx.h"#include "RandomNumber.h"#include "math.h"#include <iostream>using namespace std;template<class Type>class OrderedList{friend int main();public:OrderedList(Type Small,Type Large,int MaxL);~OrderedList();bool Search(Type x,int& index);//搜尋指定元素int SearchLast(void);//搜尋最大元素void Insert(Type k);//插入指定元素void Delete(Type k);//刪除指定元素void Output();//輸出集合中元素private:int n;//當前集合中元素的個數int MaxLength;//集合中最大元素的個數Type *value;//儲存集合中元素的數組int *link;//指標數組RandomNumber rnd;//隨機數產生器Type Small;//集合中元素的下界Type TailKey;//集合中元素的上界};//建構函式template<class Type>OrderedList<Type>::OrderedList(Type small,Type Large,int MaxL){MaxLength = MaxL;value = new Type[MaxLength+1];link = new int[MaxLength+1];TailKey = Large;n = 0;link[0] = 0;value[0] = TailKey;Small = small;}//解構函式template<class Type>OrderedList<Type>::~OrderedList(){delete value;delete link;}//搜尋集合中指定元素ktemplate<class Type>bool OrderedList<Type>::Search(Type x,int& index){index = 0;Type max = Small;int m = floor(sqrt(double(n)));//隨機抽取數組元素次數for(int i=1; i<=m; i++){int j = rnd.Random(n)+1;//隨機產生數組元素位置Type y = value[j];if((max<y)&& (y<x)){max = y;index = j;}}//順序搜尋while(value[link[index]]<x){index = link[index];}return (value[link[index]] == x);}//插入指定元素template<class Type>void OrderedList<Type>::Insert(Type k){if((n == MaxLength)||(k>=TailKey)){return;}int index;if(!Search(k,index)){value[++n] = k;link[n] = link[index];link[index] = n;}}//搜尋集合中最大元素template<class Type>int OrderedList<Type>::SearchLast(void){int index = 0;Type x = value[n];Type max = Small;int m = floor(sqrt(double(n)));//隨機抽取數組元素次數for(int i=1; i<=m; i++){int j = rnd.Random(n)+1;//隨機產生數組元素位置Type y = value[j];if((max<y)&&(y<x)){max = y;index = j;}}//順序搜尋while(link[index]!=n){index = link[index];}return index;}//刪除集合中指定元素template<class Type>void OrderedList<Type>::Delete(Type k){if((n==0)&&(k>=TailKey)){return;}int index;if(Search(k,index)){int p = link[index];if(p == n){link[index] = link[p];}else{if(link[p]!=n){int q = SearchLast();link[q] = p;link[index] = link[p];}value[p] = value[n];//刪除元素由最大元素來填補link[p] = link[n];}n--;}}//輸出集合中所有元素template<class Type>void OrderedList<Type>::Output(){int index = 0,i = 0;while(i<n){index = link[index];cout<<value[index]<<" ";i++;}cout<<endl;cout<<"value:";for(i=0; i<=n; i++){cout.width(4);cout<<value[i];}cout<<endl;cout<<"link:";for(i=0; i<=n; i++){cout.width(4);cout<<link[i];}cout<<endl;}int main(){static RandomNumber rnd;OrderedList<int> *ol = new OrderedList<int>(0,100,100);//建立cout<<"=========建立==========="<<endl;while(ol->n<10){int e = rnd.Random(99);ol->Insert(e);}ol->Output();cout<<endl;//搜尋cout<<"=========搜尋==========="<<endl;int index;cout<<"搜尋有序表value數組中第5個元素如下:"<<endl;cout<<"value[5]="<<ol->value[5]<<endl;ol->Search(ol->value[5],index);cout<<"位置搜尋結果為:"<<ol->link[index]<<endl;cout<<endl;//刪除cout<<"=========刪除==========="<<endl;cout<<"刪除有序表value數組中第5個元素如下:"<<endl;cout<<"value[5]="<<ol->value[5]<<endl;ol->Delete(ol->value[5]);ol->Output();delete ol;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.