難以想象的排序效率:希爾排序和插入排序的比較

            最開始學習排序是冒泡排序,那時候以爲排序總不過就是兩兩比較,然後交換值而已:今天突然想實現一線以前所有學過的排序算法,對10w級別以上的數組進行排序的時候,希爾排序和插入排序的效率簡直不在一個數量級上的。

           現在還有一個疑慮就是希爾排序的增量的確定的問題,我的方法是一個固定的增量區間,對排序的數組的元素個數 all = (int)log(size*4) + 1; //增量的個數是排序數數量級的ln,這樣得到的增量數組

 

 對18W個數字排序,時間比較(毫秒) 希爾排序 0.1s 就完成了,有點不敢相信,插入排序用了4.2s

#include <iostream.h>
#include <windows.h>
#include <ctime>
#include <math.h>
#include <cstdlib>

void insertSort( int a[],int size);

//隨機產生size個數組,存到pint裏面
int randArr(int * pint , int size);


void ShellInsert(int a[],int size,int dk);

void ShellSort(int a[],int size,int dlta[],int t);

//得到增量值
int GenDis(int a[],int size);

void main()
{

	int  t =  0;
	int i =0,start=0,tsize=0;
		
	int dis[20] ={0};

 	tsize = 188880;
	cout<<"產生隨機數"<<tsize<<"個"<<endl;

	t = GenDis(dis,tsize);

	cout<<"產生區間間隔距離"<<t<<"個:如下"<<endl;
	for( i = 0 ; i < t ; i++)
	{
		cout<<dis[i]<<" ";
	}cout<<"\n";

	int *pint=new int[tsize];
	
	int *pint2 =new int[tsize];

	if(! randArr(pint,tsize) )
		return;

	memcpy(pint2,pint,sizeof(int) * tsize);


	start  = GetTickCount();
	ShellSort(pint,tsize,dis,t );
	cout<<"time ShellSort used="<< GetTickCount() - start << endl;

	for( i = 0 ; i < t ; i++)
	{
		cout<<pint[i]<<" ";
	}cout<<"=========\n";


	/*插入排序的時間效率*/
	start  = GetTickCount();

	insertSort(pint2,tsize);

	cout<<"time insertSort used="<< GetTickCount() - start << endl;

	for( i = 0 ; i < t ; i++)
	{
		cout<<pint2[i]<<" ";
	}cout<<"=========\n";


	delete[] pint;
	delete[] pint2;
}


int GenDis(int a[],int size)
{
	int dlta []= {1500,1340,1160,1050,800,760,560,370,168,100,65,40,25,10,5,2,1};/*17個*/

	int all = (int)log(size*4) + 1;	//增量的個數是排序數組數量級的ln

	int  i = 0 , j = 0;
	
	if( all >= sizeof(dlta) /sizeof(dlta[0])) 
	{
		all = sizeof(dlta) /sizeof(dlta[0]);
		i = 0;
	}
	else
		i =  sizeof(dlta) /sizeof(dlta[0]) - all;

	for( j = 0 ; i < sizeof(dlta) /sizeof(dlta[0]) ; i++,j++)
	{
		a[j] =dlta[i];
	}

	return all;
}

void insertSort( int a[],int size)
{
	int i ,j;
	int pos;
	for( i = 1 ; i < size ; i++)
	{
		for(pos = a[i] ,j = i-1 ;  j >= 0 && a[j] > pos ; j--)
		{
			a[j+1] = a[j];
		}
		a[j+1] = pos;
	}

}

//產生size個隨機數
int randArr(int * pint , int size)
{
	int i = 0;
	if(!pint)	return 0;
	srand((unsigned int)time(NULL));

	for( i = 0 ; i<size; i++)
	{
			pint[i] = rand() ;

			if( rand() % 10 == 1 && rand() % 10 == 1 && rand() % 10 == 1 &&pint[i] % 10 == 2)
				pint[i] *= -1;
	}
	return 1;
}

void ShellInsert(int a[],int size,int dk)
{
	int i= 0,j = 0 , pos;
	for(i = dk; i < size ; i++)
	{
		if(a[i] < a[i-dk])
		{
			pos = a[i];
			for( j = i -dk; j>=0 && pos < a[j] ;j -= dk)
			{
				a[j + dk] = a[j];
			}
			a[j+dk] = pos;
		}
	}
}

void ShellSort(int a[],int size,int dt[],int t)
{
	for(int k = 0; k <  t ; k++)
	{
		ShellInsert(a,size,dt[k]);
	}
}


 

 

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