欢迎来到天天文库
浏览记录
ID:40559043
大小:20.00 KB
页数:3页
时间:2019-08-04
《Java实现几种排序算法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java冒泡排序算法importjava.util.Arrays;publicclassBubbleSort{staticStringsortArray(int[]before){Stringresult="";for(inti=0;i2、re[j]>before[j+1]){t=before[j+1];before[j+1]=before[j];before[j]=t;}}}returnbefore;}publicstaticvoidmain(String[]args){inta[]={12,43,23,56,8,22,65,87};intb[]={23,12,25,67,35,46,82,26,42,79,39};System.out.println("aBeforesorting:"+sortArray(a));a=bubbleSort(a);System.out.println("aAfterSorting:"3、+sortArray(a));System.out.println("bBeforesorting:"+sortArray(b));Arrays.sort(b);//使用系统自带的排序算法System.out.println("bAfterSorting:");for(inti=0;i4、rt{publicstaticvoidselectionSort(int[]number){for(inti=0;i5、i]=number[j];number[j]=t;}publicstaticvoidmain(String[]args){int[]num={2,1,5,876,12,56};selectionSort(num);for(inti=0;i6、mber.length;j++){inttmp=number[j];//后数组中的待插入元素(前数组为第一个元素)inti=j-1;while(tmp7、m[i]);}}}
2、re[j]>before[j+1]){t=before[j+1];before[j+1]=before[j];before[j]=t;}}}returnbefore;}publicstaticvoidmain(String[]args){inta[]={12,43,23,56,8,22,65,87};intb[]={23,12,25,67,35,46,82,26,42,79,39};System.out.println("aBeforesorting:"+sortArray(a));a=bubbleSort(a);System.out.println("aAfterSorting:"
3、+sortArray(a));System.out.println("bBeforesorting:"+sortArray(b));Arrays.sort(b);//使用系统自带的排序算法System.out.println("bAfterSorting:");for(inti=0;i4、rt{publicstaticvoidselectionSort(int[]number){for(inti=0;i5、i]=number[j];number[j]=t;}publicstaticvoidmain(String[]args){int[]num={2,1,5,876,12,56};selectionSort(num);for(inti=0;i6、mber.length;j++){inttmp=number[j];//后数组中的待插入元素(前数组为第一个元素)inti=j-1;while(tmp7、m[i]);}}}
4、rt{publicstaticvoidselectionSort(int[]number){for(inti=0;i5、i]=number[j];number[j]=t;}publicstaticvoidmain(String[]args){int[]num={2,1,5,876,12,56};selectionSort(num);for(inti=0;i6、mber.length;j++){inttmp=number[j];//后数组中的待插入元素(前数组为第一个元素)inti=j-1;while(tmp7、m[i]);}}}
5、i]=number[j];number[j]=t;}publicstaticvoidmain(String[]args){int[]num={2,1,5,876,12,56};selectionSort(num);for(inti=0;i6、mber.length;j++){inttmp=number[j];//后数组中的待插入元素(前数组为第一个元素)inti=j-1;while(tmp7、m[i]);}}}
6、mber.length;j++){inttmp=number[j];//后数组中的待插入元素(前数组为第一个元素)inti=j-1;while(tmp7、m[i]);}}}
7、m[i]);}}}
此文档下载收益归作者所有