归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
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代码:
更多信息请参考:http://baike.baidu.com/view/90797.htm
C语言代码:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>
void init(int *array, int count)
{
int n = 0;
srand((unsigned int)time(NULL));
for (n=0; n<count; n++)
{
array[n] = rand()%100 + 1;
}
}
void output(int *array, int count)
{
int n = 0;
for (n=0; n<count; n++)
{
printf("%5d", array[n]);
}
printf("\n");
}
void merge(int* array, int* temparray, int count, int low, int mid, int high)
{
int i = low;
int j = mid+1;
int k = low;
while(i<=mid&&j<=high)
{
if(array[i]<=array[j]) // 此处为稳定排序的关键,不能用小于
{
temparray[k++]=array[i++];
}
else
{
temparray[k++]=array[j++];
}
}
while(i<=mid)
{
temparray[k++]=array[i++];
}
while(j<=high)
{
temparray[k++]=array[j++];
}
for(i=low;i<=high;i++)// 写回原数组
{
array[i]=temparray[i];
}
}
void mergesort(int* array, int* temparray, int count, int start, int end)
{
if(start < end)
{
int mid = (start+end)/2;
mergesort(array, temparray, count, start, mid);
mergesort(array, temparray, count, mid+1, end);
merge(array, temparray, count, start, mid, end);
}
}
int main()
{
const int count = 10;
int array[count];
int temparray[count];
memset(array, 0, sizeof(int)*count);
memset(temparray, 0, sizeof(int)*count);
init(array, count);
printf("data before sort: ");
output(array, count);
mergesort(array, temparray, count, 0, count-1);
printf("data after sort: ");
output(array, count);
return 0;
}
运行结果如下:
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代码:
import java.util.Random;
/**
* 归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,
* 每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
*/
public class Sort
{
/*
* 输出数组中的数据
*/
public static void OutputArray(int[] array)
{
for (int data : array)
{
System.out.print(data + "\t");
}
System.out.println();
}
/*
* 生成需要排序的数组
*/
public static int[] createArray(int count)
{
int array[] = new int[count];
Random r = new Random();
for (int i = 0; i < count; i++)
{
array[i] = r.nextInt(100);
}
System.out.println("");
System.out.print("data before sort:\t");
OutputArray(array);
return array;
}
private static int[] merge_sort(int[] array, int start, int end)
{
int[] result = new int[end-start+1];
if(start<end)
{
int mid= (start+end)/2;
int[] left= merge_sort(array, start, mid);
int[] right = merge_sort(array, mid+1, end);
result= merge(left,right);
}
else if (start == end)
{
result[0] = array[start];
return result;
}
return result;
}
private static int[] merge(int[] left, int[] right)
{
int[] result = new int[left.length+right.length];
int i=0;
int j=0;
int k=0;
while(i<left.length&&j<right.length)
{
if(left[i]<right[j])
{
result[k++] = left[i++];
}
else
{
result[k++] = right[j++];
}
}
while(i<left.length)
{
result[k++] = left[i++];
}
while (j<right.length)
{
result[k++]= right[j++];
}
return result;
}
public static void main(String[] args)
{
int[] arr1=createArray(10);
System.out.print("data after sort:\t");
int[] result = merge_sort(arr1, 0, arr1.length-1);
OutputArray(result);
}
}
排序结果如下:
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