专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »算法 » 排序算法:合并排序算法 »正文

排序算法:合并排序算法

来源: 发布时间:星期三, 2008年12月10日 浏览:26次 评论:0
参考:[http://www.crazycoder.cn/]合并排序也用了分治的策略,不过划分只是从中间做,而merge复杂一些.
参考:[http://www.crazycoder.cn/]#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_LENGTH 100
/*Show usage*/
void usage(char * prog)
{
printf(\"%s Usage:\\n\", prog);
printf(\"%s <length of 1st list> <length of 2nd list>(Both should be less than 100)>\\n\", prog);
}
/*Generate and initialize the list*/
int * generate_list(int count)
{
int i;
int * list;
list = (int *)malloc(count*sizeof(int));
if(list == NULL)
{
perror(\"malloc\");
return NULL;
}
/*Initialize the list with integers less than 100*/
srandom((unsigned int)time(NULL));
for (i = 0; i < count ; i ++)
list[i] = random()%100;
return list;
}
/*Show the list*/
void show_list(int * list, int length)
{
int i;
for(i = 0; i < length; i ++)
printf(\"%d \", list[i]);
printf(\"\\n\");
}
void merge(int * srclist, int first, int mid, int last)
{
int * ord_list = (int *)malloc((last - first + 1)* sizeof(int));
int i = first, j = mid+1, k = 0;
int t;
while((i <= mid) && (j <= last))
{
if(srclist[i] < srclist[j])
{
ord_list[k] = srclist[i];
i ++;
k ++;
}
else
{
ord_list[k] = srclist[j];
j ++;
k ++;
}
}
if(i > mid)
{
for(t = j; t <= last; t ++)
{
ord_list[k] = srclist[t];
k ++;
}
}
else
{

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: