资源描述:
《java的各种算法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、java的各种算法.txt爱情是彩色气球,无论颜色如何严厉,经不起针尖轻轻一刺。一流的爱人,既能让女人爱一辈子,又能一辈子爱一个女人![size=large]排序的java实现:Java代码importjava.util.*;publicclassBubbleSort{publicvoidsort(int[]a){for(inti=0;ifor(intj=a.length-1;j>=i+1;j--){if(a[j]inttmp=a[j];a[j]=a[j-1];a[j-1]=tmp;}}}}publicstaticvoidmai
2、n(String[]args){int[]a={1,6,3,8,1,56,};BubbleSortbs=newBubbleSort();bs.sort(a);System.out.println(Arrays.toString(a));}}2InsertSortimportjava.util.*;publicclassInsertSort{privateinti=0;publicvoidsort(int[]a){for(intj=1;jintkeys=a[j];i=j-1;while(i>=0&&a[i]>keys){a[i+1
3、]=a[i];i--;}a[i+1]=keys;}}publicstaticvoidmain(String[]args){InsertSorti=newInsertSort();int[]f={5,2,4,6,1,3,0};i.sort(f);System.out.println(Arrays.toString(f));}}3MergeSortimportjava.util.*;publicclassMergeSortTest{privateint[]l,r1;publicvoidmerge(int[]a,intp,intq,i
4、ntr){intn1=q-p+1;intn2=r-q;l=newint[n1];r1=newint[n2+1];for(inti=0;il[i]=a[p+i];}l[n1-1]=Integer.MAX_VALUE;for(inti=0;ir1[i]=a[q+i];}r1[n2]=Integer.MAX_VALUE;inti=0;intj=0;for(intk=p;kif(l[i]<=r1[j]){a[k]=l[i];i=i+1;}else{a[k]=r1[j];j=j+1;}}}publicvoidmergeSort(int[]
5、a,intp,intr){if(p+1intq=(p+r)/2;mergeSort(a,p,q);mergeSort(a,q,r);merge(a,p,q,r);}}publicstaticvoidmain(String[]args){int[]a1={3,41,52,26,38,57,9,49};MergeSortTestmst=newMergeSortTest();mst.mergeSort(a1,0,a1.length);System.out.println(Arrays.toString(a1));}}4Selectio
6、nSortimportjava.util.*;publicclassMergeSortTest{privateint[]l,r1;publicvoidmerge(int[]a,intp,intq,intr){intn1=q-p+1;intn2=r-q;l=newint[n1];r1=newint[n2+1];for(inti=0;il[i]=a[p+i];}l[n1-1]=Integer.MAX_VALUE;for(inti=0;ir1[i]=a[q+i];}r1[n2]=Integer.MAX_VALUE;inti=0;int
7、j=0;for(intk=p;kif(l[i]<=r1[j]){a[k]=l[i];i=i+1;}else{a[k]=r1[j];j=j+1;}}}publicvoidmergeSort(int[]a,intp,intr){if(p+1intq=(p+r)/2;mergeSort(a,p,q);mergeSort(a,q,r);merge(a,p,q,r);}}publicstaticvoidmain(String[]args){int[]a1={3,41,52,26,38,57,9,49};MergeSortTestmst=n
8、ewMergeSortTest();mst.mergeSort(a1,0,a1.length);System.out.println(Arrays.toString(a1));}}5HeapSortimportjava.util.*;publicclassHea