首页 / 算法 / 归并排序-JAVA实现
归并排序-JAVA实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了归并排序-JAVA实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3597字,纯文字阅读大概需要6分钟。
内容图文
1 package com.iloveu.xxx; 2 3 public class MergeSort { 4 5 static final int SIZE = 15; 6 7staticvoid mergeOne(int a[],int b[],int n,int len) 8 { 9int i,j,k,s,e; 10 s=0; 11while(s+len<n){ 12 e = s+2*len-1; 13if(e>=n){//最后一段可能少于len个节点 14 e = n -1; 15 } 16//相邻有序段合并 17 k=s; 18 i=s; 19 j=s+len; 20while(i<s+len && j<=e){//如果两个有序表都未结束时,循环比较 21if(a[i]<=a[j]){//如果较小的元素复制到数组b中 22 b[k++]=a[i++]; 23 }else{ 24 b[k++]=a[j++]; 25 } 26 } 27while(i<s+len){//未合并的部分复制到数组b中 28 b[k++]=a[i++]; 29 } 30while(j<=e){//未合并的部分复制到数组b中 31 b[k++]=a[j++]; 32 33 } 34 s=e+1;//下一对有序段中左段的开始下标 35 } 36if(s<n){//将剩余的一个有序段从数组a中复制到数组b中 37for(;s<n;s++){ 38 b[s] = a[s]; 39 } 40 41 } 42 } 43 44staticvoid mergeSort(int a[],int n)//合并排序 45 { 46int h,count,len,f; 47 48 count = 0;//排序步骤 49 len = 1;//有序序列的长度 50 f = 0;//变量f作标志 51 52int[] p = newint[n]; 53while(len<n){ 54if(f==1){//交替在a和p之间合并 55 mergeOne(p,a,n,len);//p合并到a 56 }else{ 57 mergeOne(a,p,n,len);//a合并到p 58 } 59 len = len*2;//增加有序序列长度 60 f=1-f;//使f值在0和1之间切换 61 62 count++; 63 System.out.printf("第"+count+"步排序结果:");//输出每步排序的结果 64for(h=0;h<SIZE;h++){ 65 System.out.printf(" "+a[h]); 66 67 } 68 System.out.printf("\n"); 69 } 70if(f==1){//如果进行了排序 71for(h=0;h<n;h++){//将内存p中的数据复制回数组a 72 a[h]=p[h]; 73 } 74 } 75 } 76 77publicstaticvoid main(String[] args) { 78// TODO Auto-generated method stub 79int[] shuzu=newint[SIZE]; 80int i; 81 82for(i=0;i<SIZE;i++){ 83 shuzu[i] = (int) (100+Math.random()*(100+1));//初始化数组 84 } 85 86 System.out.print("排序前的数组为:\n");//输出排序前的数组 87for(i=0;i<SIZE;i++){ 88 System.out.print(shuzu[i]+" "); 89 } 90 System.out.print("\n"); 91 92 mergeSort(shuzu,SIZE);//排序操作 93 94 System.out.print("排序后的数组为:\n"); 95for(i=0;i<SIZE;i++){ 96 System.out.print(shuzu[i]+" ");//输出排序后的数组 97try { 98 Thread.sleep(1000); 99 } catch (InterruptedException e) { 100// TODO Auto-generated catch block101 e.printStackTrace(); 102 } 103 } 104 System.out.print("\n"); 105 } 106107108 }
package com.iloveu.xxx;
public class MergeSort {static final int SIZE = 15;static void mergeOne(int a[],int b[],int n,int len){int i,j,k,s,e;s=0;while(s+len<n){e = s+2*len-1;if(e>=n){//最后一段可能少于len个节点e = n -1;}//相邻有序段合并k=s;i=s;j=s+len;while(i<s+len && j<=e){//如果两个有序表都未结束时,循环比较if(a[i]<=a[j]){//如果较小的元素复制到数组b中b[k++]=a[i++];}else{b[k++]=a[j++];}}while(i<s+len){//未合并的部分复制到数组b中b[k++]=a[i++];}while(j<=e){//未合并的部分复制到数组b中b[k++]=a[j++];}s=e+1;//下一对有序段中左段的开始下标}if(s<n){//将剩余的一个有序段从数组a中复制到数组b中for(;s<n;s++){b[s] = a[s];}}}static void mergeSort(int a[],int n)//合并排序{int h,count,len,f;count = 0;//排序步骤len = 1;//有序序列的长度f = 0;//变量f作标志int[] p = new int[n];while(len<n){if(f==1){//交替在a和p之间合并mergeOne(p,a,n,len);//p合并到a}else{mergeOne(a,p,n,len);//a合并到p}len = len*2;//增加有序序列长度f=1-f;//使f值在0和1之间切换count++;System.out.printf("第"+count+"步排序结果:");//输出每步排序的结果for(h=0;h<SIZE;h++){System.out.printf(" "+a[h]);}System.out.printf("\n");}if(f==1){//如果进行了排序for(h=0;h<n;h++){//将内存p中的数据复制回数组aa[h]=p[h];}}}
public static void main(String[] args) {// TODO Auto-generated method stubint[] shuzu=new int[SIZE];int i;for(i=0;i<SIZE;i++){shuzu[i] = (int) (100+Math.random()*(100+1));//初始化数组}System.out.print("排序前的数组为:\n");//输出排序前的数组for(i=0;i<SIZE;i++){System.out.print(shuzu[i]+" ");}System.out.print("\n");mergeSort(shuzu,SIZE);//排序操作System.out.print("排序后的数组为:\n");for(i=0;i<SIZE;i++){System.out.print(shuzu[i]+" ");//输出排序后的数组try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.print("\n");}
}
原文:http://www.cnblogs.com/haciont/p/7859364.html
内容总结
以上是互联网集市为您收集整理的归并排序-JAVA实现全部内容,希望文章能够帮你解决归并排序-JAVA实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。