归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
更多信息请参考:http://baike.baidu.com/view/90797.htm
C语言代码:
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <memory.h> 4 #include <time.h> 5 6 void init(int *array, int count) 7 { 8 int n = 0; 9 srand((unsigned int)time(NULL)); 10 11 for (n=0; n<count; n++) 12 { 13 array[n] = rand()%100 + 1; 14 } 15 } 16 17 void output(int *array, int count) 18 { 19 int n = 0; 20 for (n=0; n<count; n++) 21 { 22 printf("%5d", array[n]); 23 } 24 25 printf("\n"); 26 } 27 28 void merge(int* array, int* temparray, int count, int low, int mid, int high) 29 { 30 int i = low; 31 int j = mid+1; 32 int k = low; 33 while(i<=mid&&j<=high) 34 { 35 if(array[i]<=array[j]) // 此处为稳定排序的关键,不能用小于 36 { 37 temparray[k++]=array[i++]; 38 } 39 else 40 { 41 temparray[k++]=array[j++]; 42 } 43 } 44 45 while(i<=mid) 46 { 47 temparray[k++]=array[i++]; 48 } 49 50 while(j<=high) 51 { 52 temparray[k++]=array[j++]; 53 } 54 55 for(i=low;i<=high;i++)// 写回原数组 56 { 57 array[i]=temparray[i]; 58 } 59 } 60 61 void mergesort(int* array, int* temparray, int count, int start, int end) 62 { 63 if(start < end) 64 { 65 int mid = (start+end)/2; 66 mergesort(array, temparray, count, start, mid); 67 mergesort(array, temparray, count, mid+1, end); 68 69 merge(array, temparray, count, start, mid, end); 70 } 71 } 72 73 int main() 74 { 75 const int count = 10; 76 int array[count]; 77 int temparray[count]; 78 79 memset(array, 0, sizeof(int)*count); 80 memset(temparray, 0, sizeof(int)*count); 81 82 init(array, count); 83 84 printf("data before sort: "); 85 output(array, count); 86 87 mergesort(array, temparray, count, 0, count-1); 88 printf("data after sort: "); 89 output(array, count); 90 91 return 0; 92 }
运行结果如下:
data before sort: 50 91 73 42 84 88 9 57 18 35
data after sort: 9 18 35 42 50 57 73 84 88 91
Java代码:
1 import java.util.Random; 2 3 /** 4 * 归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列, 5 * 每个子序列是有序的。然后再把有序子序列合并为整体有序序列。 6 */ 7 8 public class Sort 9 { 10 /* 11 * 输出数组中的数据 12 */ 13 public static void OutputArray(int[] array) 14 { 15 for (int data : array) 16 { 17 System.out.print(data + "\t"); 18 } 19 20 System.out.println(); 21 } 22 23 /* 24 * 生成需要排序的数组 25 */ 26 public static int[] createArray(int count) 27 { 28 int array[] = new int[count]; 29 Random r = new Random(); 30 31 for (int i = 0; i < count; i++) 32 { 33 array[i] = r.nextInt(100); 34 } 35 36 System.out.println(""); 37 System.out.print("data before sort:\t"); 38 39 OutputArray(array); 40 41 return array; 42 } 43 44 private static int[] merge_sort(int[] array, int start, int end) 45 { 46 int[] result = new int[end-start+1]; 47 if(start<end) 48 { 49 int mid= (start+end)/2; 50 int[] left= merge_sort(array, start, mid); 51 int[] right = merge_sort(array, mid+1, end); 52 result= merge(left,right); 53 } 54 else if (start == end) 55 { 56 result[0] = array[start]; 57 return result; 58 } 59 60 return result; 61 } 62 63 private static int[] merge(int[] left, int[] right) 64 { 65 int[] result = new int[left.length+right.length]; 66 int i=0; 67 int j=0; 68 int k=0; 69 while(i<left.length&&j<right.length) 70 { 71 if(left[i]<right[j]) 72 { 73 result[k++] = left[i++]; 74 } 75 else 76 { 77 result[k++] = right[j++]; 78 } 79 } 80 81 while(i<left.length) 82 { 83 result[k++] = left[i++]; 84 } 85 86 while (j<right.length) 87 { 88 result[k++]= right[j++]; 89 } 90 91 return result; 92 } 93 94 public static void main(String[] args) 95 { 96 int[] arr1=createArray(10); 97 98 System.out.print("data after sort:\t"); 99 int[] result = merge_sort(arr1, 0, arr1.length-1); 100 OutputArray(result); 101 } 102 }
排序结果如下:
data before sort: 39 27 27 7 6 41 82 36 89 20
data after sort: 6 7 20 27 27 36 39 41 82 89
转载于:https://www.cnblogs.com/yuanping/archive/2012/12/26/2834900.html