码迷,mamicode.com
首页 > 编程语言 > 详细

归并排序

时间:2018-03-21 11:28:28      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:归并排序

/* ============================================================================ 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!