java数据结构和算法------合并排序

 

 

 1 package iYou.neugle.sort;

 2 

 3 public class Merge_sort {

 4 

 5     public static void MergeSort(double[] array, int start, int end) {

 6         if (start < end) {

 7             int middle = (start + end) / 2;

 8             MergeSort(array, start, middle);

 9             MergeSort(array, middle + 1, end);

10             Merge(array, start, middle, end);

11         }

12     }

13 

14     private static void Merge(double[] array, int start, int middle, int end) {

15         int n1 = middle - start + 1;

16         int n2 = end - middle;

17         double[] L = new double[n1];

18         double[] R = new double[n2];

19         for (int i = 0; i < n1; i++) {

20             L[i] = array[start + i];

21         }

22         for (int i = 0; i < n2; i++) {

23             R[i] = array[middle + i + 1];

24         }

25         for (int i = 0, j = 0, k = start; k <= end; k++) {

26             if (j == n2 || (i < n1 && L[i] <= R[j])) {

27                 array[k] = L[i];

28                 i++;

29             } else {

30                 array[k] = R[j];

31                 j++;

32             }

33         }

34     }

35 }

 

你可能感兴趣的:(java)