java的实现.docx
- 文档编号:8380711
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:11
- 大小:16.34KB
java的实现.docx
《java的实现.docx》由会员分享,可在线阅读,更多相关《java的实现.docx(11页珍藏版)》请在冰豆网上搜索。
java的实现
各类排序算法java的实现
插入排序:
packageorg.rut.util.algorithm.support;
importorg.rut.util.algorithm.SortUtil;
/**
*@authortreeroot
*@since2006-2-2
*@version1.0
*/
publicclassInsertSortimplementsSortUtil.Sort{
/*(non-Javadoc)
*@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[])
*/
publicvoidsort(int[]data){
inttemp;
for(inti=1;i for(intj=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1); } } } } 冒泡排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassBubbleSortimplementsSortUtil.Sort{ /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ inttemp; for(inti=0;i for(intj=data.length-1;j>i;j--){ if(data[j] SortUtil.swap(data,j,j-1); } } } } } 选择排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassSelectionSortimplementsSortUtil.Sort{ /* *(non-Javadoc) * *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ inttemp; for(inti=0;i intlowIndex=i; for(intj=data.length-1;j>i;j--){ if(data[j] lowIndex=j; } } SortUtil.swap(data,i,lowIndex); } } } Shell排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassShellSortimplementsSortUtil.Sort{ /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ for(inti=data.length/2;i>2;i/=2){ for(intj=0;j insertSort(data,j,i); } } insertSort(data,0,1); } /** *@paramdata *@paramj *@parami */ privatevoidinsertSort(int[]data,intstart,intinc){ inttemp; for(inti=start+inc;i for(intj=i;(j>=inc)&&(data[j] SortUtil.swap(data,j,j-inc); } } } } 快速排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassQuickSortimplementsSortUtil.Sort{ /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ quickSort(data,0,data.length-1); } privatevoidquickSort(int[]data,inti,intj){ intpivotIndex=(i+j)/2; //swap SortUtil.swap(data,pivotIndex,j); intk=partition(data,i-1,j,data[j]); SortUtil.swap(data,k,j); if((k-i)>1)quickSort(data,i,k-1); if((j-k)>1)quickSort(data,k+1,j); } /** *@paramdata *@parami *@paramj *@return */ privateintpartition(int[]data,intl,intr,intpivot){ do{ while(data[++l] while((r! =0)&&data[--r]>pivot); SortUtil.swap(data,l,r); } while(l SortUtil.swap(data,l,r); returnl; } } 改进后的快速排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassImprovedQuickSortimplementsSortUtil.Sort{ privatestaticintMAX_STACK_SIZE=4096; privatestaticintTHRESHOLD=10; /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ int[]stack=newint[MAX_STACK_SIZE]; inttop=-1; intpivot; intpivotIndex,l,r; stack[++top]=0; stack[++top]=data.length-1; while(top>0){ intj=stack[top--]; inti=stack[top--]; pivotIndex=(i+j)/2; pivot=data[pivotIndex]; SortUtil.swap(data,pivotIndex,j); //partition l=i-1; r=j; do{ while(data[++l] while((r! =0)&&(data[--r]>pivot)); SortUtil.swap(data,l,r); } while(l SortUtil.swap(data,l,r); SortUtil.swap(data,l,j); if((l-i)>THRESHOLD){ stack[++top]=i; stack[++top]=l-1; } if((j-l)>THRESHOLD){ stack[++top]=l+1; stack[++top]=j; } } //newInsertSort().sort(data); insertSort(data); } /** *@paramdata */ privatevoidinsertSort(int[]data){ inttemp; for(inti=1;i for(intj=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1); } } } } 归并排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassMergeSortimplementsSortUtil.Sort{ /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ int[]temp=newint[data.length]; mergeSort(data,temp,0,data.length-1); } privatevoidmergeSort(int[]data,int[]temp,intl,intr){ intmid=(l+r)/2; if(l==r)return; mergeSort(data,temp,l,mid); mergeSort(data,temp,mid+1,r); for(inti=l;i<=r;i++){ temp=data; } inti1=l; inti2=mid+1; for(intcur=l;cur<=r;cur++){ if(i1==mid+1) data[cur]=temp[i2++]; elseif(i2>r) data[cur]=temp[i1++]; elseif(temp[i1] data[cur]=temp[i1++]; else data[cur]=temp[i2++]; } } } 改进后的归并排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassImprovedMergeSortimplementsSortUtil.Sort{ privatestaticfinalintTHRESHOLD=10; /* *(non-Javadoc) * *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ int[]temp=newint[data.length]; mergeSort(data,temp,0,data.length-1); } privatevoidmergeSort(int[]data,int[]temp,intl,intr){ inti,j,k; intmid=(l+r)/2; if(l==r) return; if((mid-l)>=THRESHOLD) mergeSort(data,temp,l,mid); else insertSort(data,l,mid-l+1); if((r-mid)>THRESHOLD) mergeSort(data,temp,mid+1,r); else insertSort(data,mid+1,r-mid); for(i=l;i<=mid;i++){ temp=data; } for(j=1;j<=r-mid;j++){ temp[r-j+1]=data[j+mid]; } inta=temp[l]; intb=temp[r]; for(i=l,j=r,k=l;k<=r;k++){ if(a data[k]=temp[i++]; a=temp; }else{ data[k]=temp[j--]; b=temp[j]; } } } /** *@paramdata *@paraml *@parami */ privatevoidinsertSort(int[]data,intstart,intlen){ for(inti=start+1;i for(intj=i;(j>start)&&data[j] SortUtil.swap(data,j,j-1); } } } } 堆排序: packageorg.rut.util.algorithm.support; importorg.rut.util.algorithm.SortUtil; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassHeapSortimplementsSortUtil.Sort{ /*(non-Javadoc) *@seeorg.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ publicvoidsort(int[]data){ MaxHeaph=newMaxHeap(); h.init(data); for(inti=0;i h.remove(); System.arraycopy(h.queue,1,data,0,data.length); } privatestaticclassMaxHeap{ voidinit(int[]data){ this.queue=newint[data.length+1]; for(inti=0;i queue[++size]=data; fixUp(size); } } privateintsize=0; privateint[]queue; publicintget(){ returnqueue[1]; } publicvoidremove(){ SortUtil.swap(queue,1,size--); fixDown (1); } //fixdown privatevoidfixDown(intk){ intj; while((j=k<<1)<=size){ if(j j++; if(queue[k]>queue[j])//不用交换 break; SortUtil.swap(queue,j,k); k=j; } } privatevoidfixUp(intk){ while(k>1){ intj=k>>1; if(queue[j]>queue[k]) break; SortUtil.swap(queue,j,k); k=j; } } } } SortUtil: packageorg.rut.util.algorithm; importorg.rut.util.algorithm.support.BubbleSort; importorg.rut.util.algorithm.support.HeapSort; importorg.rut.util.algorithm.support.ImprovedMergeSort; importorg.rut.util.algorithm.support.ImprovedQuickSort; importorg.rut.util.algorithm.support.InsertSort; importorg.rut.util.algorithm.support.MergeSort; importorg.rut.util.algorithm.support.QuickSort; importorg.rut.util.algorithm.support.SelectionSort; importorg.rut.util.algorithm.support.ShellSort; /** *@authortreeroot *@since2006-2-2 *@version1.0 */ publicclassSortUtil{ publicfinalstaticintINSERT=1; publicfinalstaticintBUBBLE=2; publicfinalstaticintSELECTION=3; publicfinalstaticintSHELL=4; publicfinalstatic
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实现
![提示](https://static.bdocx.com/images/bang_tan.gif)