隨機化算法-用數組實現有序遞增鏈表

數組的標號作爲地址信息,按照遞增的順序重新存放在link數組中,link數組存放的是下一個元素即下一個大一些的元素的地址。

也可以這樣理解,link數組是一種函數映射,實現一次運算,使得原本無序的數組經過link組織後有順序。

本算法也可以作爲插入排序的一部分

書寫上用嵌套的[[[....]]]代替鏈表中->->->.....

 

關鍵:注重代碼過程中是怎麼設置link指針的,怎麼修改指針的,注意與線性鏈表對應

 

 

// 搜索有序表.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include "stdafx.h"
#include"iostream"
#include<math.h>
#include<time.h>
#include<iomanip>
using namespace std;
const unsigned long maxshort=65535L;
const unsigned long multiplier=1194211693L;
const unsigned long adder=12345L;
class RandomNumber{
private:
 //當前種子
 unsigned long randSeed;
public:
 //構造函數
 RandomNumber(unsigned long s=0);
 unsigned short Random(unsigned long n);
 double fRandom();
};


RandomNumber::RandomNumber(unsigned long s)
{
 if(s==0)
  randSeed=time(0);
 else
  randSeed=s;
}

double RandomNumber::fRandom()
{
 return Random(maxshort)/double(maxshort);
}

unsigned short RandomNumber::Random(unsigned long n)
{
 randSeed=multiplier*randSeed+adder;
 return (unsigned short)((randSeed>>16)%n);
}


template<class Type>
class OrderedList
{
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 Type[MaxLength+1];
 TailKey=large;
 n=0;
 link[0]=0;
 value[0]=TailKey;
 Small=small;
}

template<class Type>
OrderedList<Type>::~OrderedList()
{
 delete value;
 delete link;
}

template<class Type>
bool OrderedList<Type>::Search(Type x,int &index)//搜索元素x
{
 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)
{
 //刪除集合中指定元素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(void)
{
 int index=0;
 while(index<n)
 {
  cout<<value[link[index]]<<"    ";
  index=link[index];
 }
 cout<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
 int Large=20;
 int Small=4;
 int MaxL=30;
 OrderedList<int> L(Small,Large,MaxL);
 L.Insert(1);
 L.Insert(2);
 L.Insert(3);
 L.Insert(5);
 L.Insert(8);
 L.Insert(13);
 L.Insert(21);
 L.Output();
 system("PAUSE");

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章