一个比快速算法更快的排序算法: flashsort
2010-06-16 18:21
183 查看
现在最快的排序算法是快速排序算法,它的时间复杂度达到O(n log n).但是还有一种排序算法,就是FlashSort排序算法.它的时间复杂度达到O(n),超过了前者.FlashSort排序是基于分类的算法,它的实现思想很简单,是利用构造出来的索引来排序.举一个简单的例子,比如有一百个整数,你很容易就能把它们放在数组的正确位置上,根本不需要作任何比较.
flashsort 主页: http://www.neubert.net/FSOIntro.html
附C实现代码:
[code type="c"]
/***** FLASH.C ,FLOAT-, recursive subroutine version
Translation of Neubert's algorithm into C by Michael Sahota *****/
/* Subroutine Flash(a,n,m,ctr)
- Sorts array a with n elements by use of the index vector l of
dimension m (with m about 0.1 n).
- The routine runs fastest with a uniform distribution of elements.
- The vector l is declare dynamically using the calloc function.
- The variable ctr counts the number of times that flashsort is called.
- THRESHOLD is a very important constant. It is the minimum number
of elements required in a subclass before recursion is used. */
#include
#include
#include
const int THRESHOLD = 75;
const CLASS_SIZE = 75; /* minimum value for m */
void flashsort(float a[],int n,int m,int *ctr)
{
/* declare variables */
int *l,nmin,nmax,i,j,k,nmove,nx,mx;
float c1,c2,flash,hold;
/* allocate space for the l vector */
l=(int*)calloc(m,sizeof(int));
/***** CLASS FORMATION ****/
nmin=nmax=0;
for (i=0 ; i < n ; i++)
if (a[i] < a[nmin]) nmin = i;
else if (a[i] > a[nmax]) nmax = i;
if ( (a[nmax]==a[nmin]) && (ctr==0) )
{
printf("All the numbers are identical, the list is sorted
");
return;
}
c1=(m-1.0)/(a[nmax]-a[nmin]) ;
c2=a[nmin];
l[0]=-1; /* since the base of the "a" (data) array is 0 */
for (k=1; k < m ; k++) l[k]=0;
for (i=0; i < n ; i++)
{
k=floor(c1*(a[i]-c2) );
l[k]+=1;
}
for (k=1; k < m ; k++) l[k]+=l[k-1];
hold=a[nmax];
a[nmax]=a[0];
a[0]=hold;
/**** PERMUTATION *****/
nmove=0;
j=0;
k=m-1;
while ( nmove < n )
{
while ( j > l[k] )
{
j++;
k=floor(c1*(a[j]-c2) ) ;
}
flash=a[ j ] ;
while ( j <= l[k] )
{
k=floor(c1*(flash-c2));
hold=a[ l[k] ];
a[ l[k] ] = flash;
l[k]--;
flash=hold;
nmove++;
}
}
/**** Choice of RECURSION or STRAIGHT INSERTION *****/
for (k=0;k<(m-1);k++)
if ( (nx = l[k+1]-l[k]) > THRESHOLD ) /* then use recursion */
{
flashsort(&a[l[k]+1],nx,CLASS_SIZE,ctr);
(*ctr)++;
}
else /* use insertion sort */
for (i=l[k+1]-1; i > l[k] ; i--)
if (a[i] > a[i+1])
{
hold=a[i];
j=i;
while (hold > a[j+1] ) a[j++]=a[j+1] ;
a[j]=hold;
}
free(l); /* need to free the memory we grabbed for the l vector */
}
[/code]
flashsort 主页: http://www.neubert.net/FSOIntro.html
附C实现代码:
[code type="c"]
/***** FLASH.C ,FLOAT-, recursive subroutine version
Translation of Neubert's algorithm into C by Michael Sahota *****/
/* Subroutine Flash(a,n,m,ctr)
- Sorts array a with n elements by use of the index vector l of
dimension m (with m about 0.1 n).
- The routine runs fastest with a uniform distribution of elements.
- The vector l is declare dynamically using the calloc function.
- The variable ctr counts the number of times that flashsort is called.
- THRESHOLD is a very important constant. It is the minimum number
of elements required in a subclass before recursion is used. */
#include
#include
#include
const int THRESHOLD = 75;
const CLASS_SIZE = 75; /* minimum value for m */
void flashsort(float a[],int n,int m,int *ctr)
{
/* declare variables */
int *l,nmin,nmax,i,j,k,nmove,nx,mx;
float c1,c2,flash,hold;
/* allocate space for the l vector */
l=(int*)calloc(m,sizeof(int));
/***** CLASS FORMATION ****/
nmin=nmax=0;
for (i=0 ; i < n ; i++)
if (a[i] < a[nmin]) nmin = i;
else if (a[i] > a[nmax]) nmax = i;
if ( (a[nmax]==a[nmin]) && (ctr==0) )
{
printf("All the numbers are identical, the list is sorted
");
return;
}
c1=(m-1.0)/(a[nmax]-a[nmin]) ;
c2=a[nmin];
l[0]=-1; /* since the base of the "a" (data) array is 0 */
for (k=1; k < m ; k++) l[k]=0;
for (i=0; i < n ; i++)
{
k=floor(c1*(a[i]-c2) );
l[k]+=1;
}
for (k=1; k < m ; k++) l[k]+=l[k-1];
hold=a[nmax];
a[nmax]=a[0];
a[0]=hold;
/**** PERMUTATION *****/
nmove=0;
j=0;
k=m-1;
while ( nmove < n )
{
while ( j > l[k] )
{
j++;
k=floor(c1*(a[j]-c2) ) ;
}
flash=a[ j ] ;
while ( j <= l[k] )
{
k=floor(c1*(flash-c2));
hold=a[ l[k] ];
a[ l[k] ] = flash;
l[k]--;
flash=hold;
nmove++;
}
}
/**** Choice of RECURSION or STRAIGHT INSERTION *****/
for (k=0;k<(m-1);k++)
if ( (nx = l[k+1]-l[k]) > THRESHOLD ) /* then use recursion */
{
flashsort(&a[l[k]+1],nx,CLASS_SIZE,ctr);
(*ctr)++;
}
else /* use insertion sort */
for (i=l[k+1]-1; i > l[k] ; i--)
if (a[i] > a[i+1])
{
hold=a[i];
j=i;
while (hold > a[j+1] ) a[j++]=a[j+1] ;
a[j]=hold;
}
free(l); /* need to free the memory we grabbed for the l vector */
}
[/code]
相关文章推荐
- 一个比快速算法更快的排序算法: flashsort
- 看到一个比较NB的排序算法,sleep sort,贴过来,纯属娱乐。
- 各位,我做了一个flash的Sort Algorithm Visualization
- 经典排序算法 - Flash Sort
- 一个手写排序算法和sort(),qsort()的比较
- 一个很有创意的Flash项目Nudemessenger
- flash中怎样选取一个图像中指定的颜色
- 排序算法七:基数排序(Radix sort)
- 一个网站通用的FLASH 图片切换特效!
- Flash发布iOS应用全攻略(二)——如何成为一个合法的iOS开发者
- 今天我感冒了,但是却还要完成一个flash PHP+MYSQL整站程序
- 如何在freescale banked区域操作一个变量(RAM, EEPROM, FLASH)
- 经典排序算法 - 堆排序Heap sort
- 编写一个void sort(int*x,int n)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件
- Arrays.sort()用的是什么排序算法?怎么优化?
- 臭皮匠排序(Stooge Sort)----(排序算法十二)
- 排序算法系列:基数排序(Radix sort)(C语言)
- 一个非常不错的纯源码“PDF转换swf”完全脱离安装第三方插件,直接使用java调用Flash api将PDF转换成图片,在将图片转换成swf。
- 归并排序(Merging Sort)----(排序算法十三)
- 放一个比较不错的flash缓动函数上来