标签:归并排序
/*
============================================================================
Name : SuperHakce.c
Author : SuperHakce
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100;
void merge(int map[],int result[],int a,int b,int c){
int i = a,j = b + 1,k = a;;
while(i != b + 1 && j != c + 1){
if(map[i] > map[j]){
result[k ++] = map[j ++];
}else{
result[k ++] = map[i ++];
}
}
while(i != b + 1)
result[k++] = map[i++];
while(j != c + 1)
result[k++] = map[j++];
for(i = a;i <= c;i ++)
map[i] = result[i];
}
void binarySort(int map[],int result[],int a,int b){
int c;
if(a < b){
c = (a + b) / 2;
binarySort(map,result,a,c);
binarySort(map,result,c + 1,b);
merge(map,result,a,c,b);
}
}
int main(void) {
int a[100];
int b[100];
int i;
for(i = 0;i < 100;i ++){
a[i] = rand() % 500 + 1;
printf("%d ",a[i]);
}
printf("\n");
binarySort(a,b,0,99);
for(i = 0;i < 100;i ++){
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
标签:归并排序
原文地址:http://blog.51cto.com/superhakce/2089221