先贴个我写的例子先: 1. public class MergeSortByBruce { 2. 3. /** 4. * @param args 5. */ 6. public static void main(String[] args) { 7. int []data=new int[100001]; 8. for(int i=1;i<=100000;i++){ 9. Random r=new Random(); 10. data[i]=r.nextInt(100000); 11. } 12. System.out.println("Original Data Before Sorting:/t"); 13. for(int i=1;i<data.length;i++){ 14. System.out.print(data[i]+"-"); 15. if(i%40==0)System.out.println(); 16. } 17. long t1=System.currentTimeMillis(); 18. String time1=Long.toString(t1); 19. System.out.println("************************************"+t1); 20. MergeSort(data,1,data.length-2); 21. long t2=System.currentTimeMillis(); 22. String time2=Long.toString(t2); 23. System.out.println("*************************************"+t2); 24. System.out.println(t1-t2); 25. System.out.println(); 26. System.out.println("Now Print the sorted data:/t"); 27. StringBuffer abc=new StringBuffer(); 28. 29. for(int i=1;i<data.length;i++){ 30. abc.append(data[i]); 31. abc.append("-"); 32. System.out.print(data[i]+"-"); 33. if(i%40==0){System.out.println(); 34. abc.append("/n"); 35. } 36. } 37. System.out.println(time1); 38. System.out.println(time2); 39. String lu=abc.toString(); 40. File f=new File("d://sortedData.txt"); 41. try { 42. FileOutputStream os=new FileOutputStream(f); 43. OutputStreamWriter osw=new OutputStreamWriter(os); 44. BufferedWriter bw=new BufferedWriter (osw); 45. bw.write(lu); 46. bw.newLine(); 47. bw.write(time1); 48. bw.newLine(); 49. bw.write(time2); 50. bw.close(); 51. } catch (FileNotFoundException e) { 52. e.printStackTrace(); 53. } catch (IOException e) { 54. // TODO Auto-generated catch block 55. e.printStackTrace(); 56. } 57. } 58. public static void MergeSort(int data[],int first,int n){ 59. int n1,n2; 60. if(n>1){ 61. n1=n/2; 62. n2=n-n1; 63. MergeSort(data,first,n1); //first half 64. MergeSort(data,first+n1,n2); 65. Merge(data,first,n1,n2); 66. } 67. } 68. public static void Merge(int data[],int first,int n1,int n2){ 69. int temp[]=new int[n1+n2]; 70. int copied=0; 71. int copied1=0; 72. int copied2=0; 73. while((copied1<n1) && (copied2<n2)){ 74. if(data[first+copied1]<data[first+n1+copied2]){ 75. temp[copied++]=data[first+copied1++]; 76. }else{ 77. temp[copied++]=data[first+n1+copied2++]; 78. } 79. } 80. while(copied1<n1) 81. temp[copied++]=data[first+copied1++]; 82. while(copied2<n2) 83. temp[copied++]=data[first+n1+copied2++]; 84. for(int i=0;i<n1+n2;i++){ 85. data[first+i]=temp[i]; 86. } 87. } 88. 89. } 因为数据太多了.在console 下面显示不出来....所以输出到本地的一个文件. 关于算法的一些图贴一下: