我的“归并算法”实现

这是我的“归并算法”Java实现!

 

View Code
  1 /*
  2  * 练习:归并算法
  3  * 功能:对一堆数据进行排序
  4  * 作者:陈沛锐
  5  * 时间:2013.03.25
  6  */
  7 package part01.chapter02;
  8 
  9 import java.util.Random;
 10 import java.util.Scanner;
 11 
 12 public class _3exercise {
 13     public static void main(String[] args) {
 14         // int[] A={1,3,5,7,9,10,2,4,6,8,10};
 15         // MERGE myMerge = new MERGE(A, 0, 5, 10);
 16         //create data
 17         System.out.println("Please inter the data counts");
 18         Scanner scanner=new Scanner(System.in);
 19         int leng = scanner.nextInt();
 20         CreateDate createDate = new CreateDate(leng);
 21         int[] A = createDate.create();
 22         //sort data by MERGE_SORT()
 23         MERGE_SORT merge_sort=new MERGE_SORT();
 24         merge_sort.sort(A,0,leng-1);
 25         //output sorted data
 26         System.out.println("the sorted data are follow:");
 27         for(int i=0;i<leng;i++){
 28             System.out.print(A[i]+" ");
 29         }
 30     }
 31 
 32 
 33 }
 34 //MERGE_SORT
 35 class MERGE_SORT{
 36         public void sort(int[] A, int p, int r) {
 37         MERGE merge = new MERGE();
 38         int q = 0;
 39         if (p < r) {
 40             q = (p + r) / 2;
 41             sort(A, p, q);
 42             sort(A, q+1, r);
 43             merge.sort(A, p, q, r);
 44         }
 45     }
 46 }
 47 // Create date
 48 class CreateDate {
 49     int leng = 0;
 50     Random randomInt = new Random();
 51     CreateDate(int leng) {
 52         this.leng = leng;
 53     }
 54 
 55     public int[] create() {
 56         int[] A = new int[leng];
 57         System.out.println("Please inter the max number(<10000):");
 58         Scanner scanner=new Scanner(System.in);
 59         int maxNum = scanner.nextInt();
 60         if(maxNum>10000){
 61             System.out.println("enter error!");
 62             System.exit(0);
 63         }
 64         for (int i = 0; i < leng; i++) {
 65             A[i] = randomInt.nextInt(maxNum);
 66         }
 67         return A;
 68     }
 69 }
 70 
 71 // MERGE class for merger data
 72 class MERGE {
 73     int[] L = null;
 74     int[] R = null;
 75 
 76     // sort
 77     public void sort(int[] A, int p, int q, int r) {
 78         L = new int[q - p + 2];
 79         R = new int[r - q + 1];
 80         for (int i = 0; i < q - p + 1; i++) {
 81             L[i] = A[p + i];
 82         }
 83         L[q - p + 1] = 10000;// 10000 is max number
 84         for (int j = 0; j < r - q; j++) {
 85             R[j] = A[q + 1 + j];
 86         }
 87         R[r - q] = 10000;// 10000 is max number
 88         // test output A[p..r]
 89         // for(int i=p;i<r+1;i++){
 90         // System.out.print(A[i]+" ");
 91         // }
 92         int j = 0;
 93         int k = 0;
 94         for (int i = 0; i < r - p + 1; i++) {
 95             if (L[j] <= R[k]) {
 96                 A[p + i] = L[j];
 97                 j++;
 98             } else {
 99                 A[p + i] = R[k];
100                 k++;
101             }
102         }
103     }
104 }

 

 

 

 

posted @ 2013-04-27 11:01  iGeneral  阅读(280)  评论(0编辑  收藏  举报