欢迎来到天天文库
浏览记录
ID:34452418
大小:33.00 KB
页数:7页
时间:2019-03-06
《各种排序算法(插入冒泡合并希尔快速排序)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.lang.Math;publicclassSort{privatedouble[]sortArray;publicdouble[]getSortArray(){returnsortArray;}publicvoidsetSortArray(double[]sortArray){this.sortArray=sortArray;}/**输出排序样本数组*/publicvoidPrintSortArray(double[]sortArray){for(inti=0;i2、ay[i]+"");}System.out.println();}/**产生随机样本*/publicdouble[]generateRandomSample(intn){sortArray=newdouble[n];for(inti=0;i3、-1;while((j>=0)&&(sortArray[j]>key)){sortArray[j+1]=sortArray[j];j=j-1;}sortArray[j+1]=key;}}/**冒泡排序*/publicvoidbubbleSort(double[]sortArray){for(inti=0;isortArray[j+1]){doubletemp=sortArray[j];sortArray[j]=sortArr4、ay[j+1];sortArray[j+1]=temp;}}}}/**快速排序*/publicvoidquickSort(double[]sortArray,intleft,intright){if(left5、=left-1;for(intj=left;j6、=(p+r)/2;mergeSort(sortArray,p,q);mergeSort(sortArray,q+1,r);merge(sortArray,p,q,r);}}publicvoidmerge(double[]sortArray,intp,intq,intr){intn1=q-p+1;intn2=r-q;double[]left=newdouble[n1+1];double[]right=newdouble[n2+1];for(inti=0;i7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
2、ay[i]+"");}System.out.println();}/**产生随机样本*/publicdouble[]generateRandomSample(intn){sortArray=newdouble[n];for(inti=0;i3、-1;while((j>=0)&&(sortArray[j]>key)){sortArray[j+1]=sortArray[j];j=j-1;}sortArray[j+1]=key;}}/**冒泡排序*/publicvoidbubbleSort(double[]sortArray){for(inti=0;isortArray[j+1]){doubletemp=sortArray[j];sortArray[j]=sortArr4、ay[j+1];sortArray[j+1]=temp;}}}}/**快速排序*/publicvoidquickSort(double[]sortArray,intleft,intright){if(left5、=left-1;for(intj=left;j6、=(p+r)/2;mergeSort(sortArray,p,q);mergeSort(sortArray,q+1,r);merge(sortArray,p,q,r);}}publicvoidmerge(double[]sortArray,intp,intq,intr){intn1=q-p+1;intn2=r-q;double[]left=newdouble[n1+1];double[]right=newdouble[n2+1];for(inti=0;i7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
3、-1;while((j>=0)&&(sortArray[j]>key)){sortArray[j+1]=sortArray[j];j=j-1;}sortArray[j+1]=key;}}/**冒泡排序*/publicvoidbubbleSort(double[]sortArray){for(inti=0;isortArray[j+1]){doubletemp=sortArray[j];sortArray[j]=sortArr
4、ay[j+1];sortArray[j+1]=temp;}}}}/**快速排序*/publicvoidquickSort(double[]sortArray,intleft,intright){if(left5、=left-1;for(intj=left;j6、=(p+r)/2;mergeSort(sortArray,p,q);mergeSort(sortArray,q+1,r);merge(sortArray,p,q,r);}}publicvoidmerge(double[]sortArray,intp,intq,intr){intn1=q-p+1;intn2=r-q;double[]left=newdouble[n1+1];double[]right=newdouble[n2+1];for(inti=0;i7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
5、=left-1;for(intj=left;j6、=(p+r)/2;mergeSort(sortArray,p,q);mergeSort(sortArray,q+1,r);merge(sortArray,p,q,r);}}publicvoidmerge(double[]sortArray,intp,intq,intr){intn1=q-p+1;intn2=r-q;double[]left=newdouble[n1+1];double[]right=newdouble[n2+1];for(inti=0;i7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
6、=(p+r)/2;mergeSort(sortArray,p,q);mergeSort(sortArray,q+1,r);merge(sortArray,p,q,r);}}publicvoidmerge(double[]sortArray,intp,intq,intr){intn1=q-p+1;intn2=r-q;double[]left=newdouble[n1+1];double[]right=newdouble[n2+1];for(inti=0;i7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
7、sortArray[q+i+1];}left[n1]=Double.MAX_VALUE;right[n2]=Double.MAX_VALUE;intpl=0;intpr=0;for(inti=p;i<=r;i++){if(left[pl]<=right[pr]){sortArray[i]=left[pl];pl++;}else{sortArray[i]=right[pr];pr++;}}}/**希尔排序*/publicvoidShellSort(double[]a){intinc=a.len
此文档下载收益归作者所有