我积极解决 发表于 2017-5-16 09:32:16

排序算法复习(Java实现): 插入,冒泡,选择,Shell,快速排序

  为了便于管理,先引入个基础类:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;

/**
*@authoryovn
*
*/
publicabstractclassSorter<EextendsComparable<E>>{

publicabstractvoidsort(E[]array,intfrom,intlen);

publicfinalvoidsort(E[]array)
{
sort(array,0,array.length);
}
protectedfinalvoidswap(E[]array,intfrom,intto)
{
Etmp=array;
array=array;
array=tmp;
}

}
  一 插入排序
该算法在数据规模小的时候十分高效,该算法每次插入第K+1到前K个有序数组中一个合适位置,K从0开始到N-1,从而完成排序:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;
/**
*@authoryovn
*/
publicclassInsertSorter<EextendsComparable<E>>extendsSorter<E>{

/*(non-Javadoc)
*@seealgorithms.Sorter#sort(E[],int,int)
*/
publicvoidsort(E[]array,intfrom,intlen){
Etmp=null;
for(inti=from+1;i<from+len;i++)
{
tmp=array;
intj=i;
for(;j>from;j--)
{
if(tmp.compareTo(array-1])<0)
{
array=array-1];
}
elsebreak;
}
array=tmp;
}
}



}
  
二 冒泡排序
这可能是最简单的排序算法了,算法思想是每次从数组末端开始比较相邻两元素,把第i小的冒泡到数组的第i个位置。i从0一直到N-1从而完成排序。(当然也可以从数组开始端开始比较相邻两元素,把第i大的冒泡到数组的第N-i个位置。i从0一直到N-1从而完成排序。)

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;

/**
*@authoryovn
*
*/
publicclassBubbleSorter<EextendsComparable<E>>extendsSorter<E>{

privatestaticbooleanDWON=true;

publicfinalvoidbubble_down(E[]array,intfrom,intlen)
{
for(inti=from;i<from+len;i++)
{
for(intj=from+len-1;j>i;j--)
{
if(array.compareTo(array-1])<0)
{
swap(array,j-1,j);
}
}
}
}

publicfinalvoidbubble_up(E[]array,intfrom,intlen)
{
for(inti=from+len-1;i>=from;i--)
{
for(intj=from;j<i;j++)
{
if(array.compareTo(array+1])>0)
{
swap(array,j,j+1);
}
}
}
}
@Override
publicvoidsort(E[]array,intfrom,intlen){

if(DWON)
{
bubble_down(array,from,len);
}
else
{
bubble_up(array,from,len);
}
}

}
  
三,选择排序
选择排序相对于冒泡来说,它不是每次发现逆序都交换,而是在找到全局第i小的时候记下该元素位置,最后跟第i个元素交换,从而保证数组最终的有序。
相对与插入排序来说,选择排序每次选出的都是全局第i小的,不会调整前i个元素了。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;
/**
*@authoryovn
*
*/
publicclassSelectSorter<EextendsComparable<E>>extendsSorter<E>{

/*(non-Javadoc)
*@seealgorithms.Sorter#sort(E[],int,int)
*/
@Override
publicvoidsort(E[]array,intfrom,intlen){
for(inti=0;i<len;i++)
{
intsmallest=i;
intj=i+from;
for(;j<from+len;j++)
{
if(array.compareTo(array)<0)
{
smallest=j;
}
}
swap(array,i,smallest);

}

}

}

  四 Shell排序
Shell排序可以理解为插入排序的变种,它充分利用了插入排序的两个特点:
1)当数据规模小的时候非常高效
2)当给定数据已经有序时的时间代价为O(N)
所以,Shell排序每次把数据分成若个小块,来使用插入排序,而且之后在这若个小块排好序的情况下把它们合成大一点的小块,继续使用插入排序,不停的合并小块,知道最后成一个块,并使用插入排序。

这里每次分成若干小块是通过“增量” 来控制的,开始时增量交大,接近N/2,从而使得分割出来接近N/2个小块,逐渐的减小“增量“最终到减小到1。

一直较好的增量序列是2^k-1,2^(k-1)-1,.....7,3,1,这样可使Shell排序时间复杂度达到O(N^1.5)
所以我在实现Shell排序的时候采用该增量序列
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;

/**
*@authoryovn
*/
publicclassShellSorter<EextendsComparable<E>>extendsSorter<E>{

/*(non-Javadoc)
*Ourdeltavaluechoose2^k-1,2^(k-1)-1,.7,3,1.
*complexityisO(n^1.5)
*@seealgorithms.Sorter#sort(E[],int,int)
*/
@Override
publicvoidsort(E[]array,intfrom,intlen){

//1.calculatethefirstdeltavalue;
intvalue=1;
while((value+1)*2<len)
{
value=(value+1)*2-1;

}

for(intdelta=value;delta>=1;delta=(delta+1)/2-1)
{
for(inti=0;i<delta;i++)
{
modify_insert_sort(array,from+i,len-i,delta);
}
}

}

privatefinalvoidmodify_insert_sort(E[]array,intfrom,intlen,intdelta){
if(len<=1)return;
Etmp=null;
for(inti=from+delta;i<from+len;i+=delta)
{
tmp=array;
intj=i;
for(;j>from;j-=delta)
{
if(tmp.compareTo(array-delta])<0)
{
array=array-delta];
}
elsebreak;
}
array=tmp;
}

}
}
  
五 快速排序
快速排序是目前使用可能最广泛的排序算法了。
一般分如下步骤:
1)选择一个枢纽元素(有很对选法,我的实现里采用去中间元素的简单方法)
2)使用该枢纽元素分割数组,使得比该元素小的元素在它的左边,比它大的在右边。并把枢纽元素放在合适的位置。
3)根据枢纽元素最后确定的位置,把数组分成三部分,左边的,右边的,枢纽元素自己,对左边的,右边的分别递归调用快速排序算法即可。
快速排序的核心在于分割算法,也可以说是最有技巧的部分。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->packagealgorithms;

/**
*@authoryovn
*
*/
publicclassQuickSorter<EextendsComparable<E>>extendsSorter<E>{

/*(non-Javadoc)
*@seealgorithms.Sorter#sort(E[],int,int)
*/
@Override
publicvoidsort(E[]array,intfrom,intlen){
q_sort(array,from,from+len-1);
}


privatefinalvoidq_sort(E[]array,intfrom,intto){
if(to-from<1)return;
intpivot=selectPivot(array,from,to);



pivot=partion(array,from,to,pivot);

q_sort(array,from,pivot-1);
q_sort(array,pivot+1,to);

}


privateintpartion(E[]array,intfrom,intto,intpivot){
Etmp=array;
array=array;//nowto'spositionisavailable

while(from!=to)
{
while(from<to&&array.compareTo(tmp)<=0)from++;
if(from<to)
{
array=array;//nowfrom'spositionisavailable
to--;
}
while(from<to&&array.compareTo(tmp)>=0)to--;
if(from<to)
{
array=array;//nowto'spositionisavailablenow
from++;
}
}
array=tmp;
returnfrom;
}


privateintselectPivot(E[]array,intfrom,intto){

return(from+to)/2;
}

}
页: [1]
查看完整版本: 排序算法复习(Java实现): 插入,冒泡,选择,Shell,快速排序