并归排序 c语言数据结构
2012-07-16 16:29
155 查看
#include <stdio.h>
#include <stdlib.h>
#define N 10
void merge(int *a1,int a1_start,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end);
void merge_sort(int *a,int left,int right);
/*
函数功能:两个数组合并成一个数组;
函数原型:void merge(int *a1,int a1_start,int a1_end,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end)
函数参数:
函数返回值:void
*/
void merge(int *a1,int a1_start,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end)
{
int i,j,h,k=0;
int b
;
i=a2_start;
j=a3_start;
while(i<=a2_end&&j<=a3_end)
{
if(a2[i]<=a3[j])
{
b[k++]=a2[i++];
}
else
{
b[k++]=a3[j++];
}
}
while(i<=a2_end)
{
b[k++]=a2[i++];
}
while(j<=a3_end)
{
b[k++]=a3[j++];
}
for(h=0;h<k;h++)
{
a1[a1_start+h]=b[h];
}
}
/*
函数功能:使用归并排序法进行排序:从小到大;
函数原型:void merge_sort(int *a,int left,int right)
函数参数:int *a:数组名
int left:排序数组的开始下标
int right:排序数组的结束下标
函数返回值:void
*/
void merge_sort(int *a,int left,int right)
{
int half;
if(left<right)
{
half=(int)((left+right)/2);
merge_sort(a,left,half);
merge_sort(a,half+1,right);
merge(a,left,a,left,half,a,half+1,right);
}
}
/*main 函数*/
int a
={12,78,895,24,158,369,58,621,1475,2};
int main()
{
int i;
printf("排序前数组序列:/n");
for(i=0;i<N;i++)
{
printf("%5d",a[i]);
}
printf("/n排序前数组序列:/n");
merge_sort(a,0,N-1);
for(i=0;i<N;i++)
{
printf("%d ",a[i]);
}
}
#include <stdlib.h>
#define N 10
void merge(int *a1,int a1_start,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end);
void merge_sort(int *a,int left,int right);
/*
函数功能:两个数组合并成一个数组;
函数原型:void merge(int *a1,int a1_start,int a1_end,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end)
函数参数:
函数返回值:void
*/
void merge(int *a1,int a1_start,int *a2,int a2_start,
int a2_end,int *a3,int a3_start,int a3_end)
{
int i,j,h,k=0;
int b
;
i=a2_start;
j=a3_start;
while(i<=a2_end&&j<=a3_end)
{
if(a2[i]<=a3[j])
{
b[k++]=a2[i++];
}
else
{
b[k++]=a3[j++];
}
}
while(i<=a2_end)
{
b[k++]=a2[i++];
}
while(j<=a3_end)
{
b[k++]=a3[j++];
}
for(h=0;h<k;h++)
{
a1[a1_start+h]=b[h];
}
}
/*
函数功能:使用归并排序法进行排序:从小到大;
函数原型:void merge_sort(int *a,int left,int right)
函数参数:int *a:数组名
int left:排序数组的开始下标
int right:排序数组的结束下标
函数返回值:void
*/
void merge_sort(int *a,int left,int right)
{
int half;
if(left<right)
{
half=(int)((left+right)/2);
merge_sort(a,left,half);
merge_sort(a,half+1,right);
merge(a,left,a,left,half,a,half+1,right);
}
}
/*main 函数*/
int a
={12,78,895,24,158,369,58,621,1475,2};
int main()
{
int i;
printf("排序前数组序列:/n");
for(i=0;i<N;i++)
{
printf("%5d",a[i]);
}
printf("/n排序前数组序列:/n");
merge_sort(a,0,N-1);
for(i=0;i<N;i++)
{
printf("%d ",a[i]);
}
}
相关文章推荐
- 数据结构-数组排序-二路归并-循环实现-C语言
- 数据结构中的排序查找算法(C语言实现)
- EKAlgorithms-常用数据结构Objective-C语言实现一数组排序
- 数据结构--选择排序(C语言)
- 数据结构 查找及排序算法、直接插入排序及顺序查找(c语言实现)
- C语言数据结构 链表与归并排序实例详解
- 数据结构-数组排序(冒泡 选择 插入 归并(合并有序数组))-C语言
- 数据结构 C语言实现选择排序
- 数据结构 c语言设计队列 实现桶式排序和基数排序 测试性能
- C语言_数据结构和算法之_排序和查找
- 数据结构中的排序——C语言实现排序源代码
- 快速排序的C语言实现【严蔚敏--数据结构】
- 快速排序----C语言数据结构
- C语言-数据结构-选择排序-源代码
- (数据结构排序的实验四)快速,冒泡,简单选择,直接插入排序的c语言实现!!
- 选择排序 --C语言数据结构
- 二叉树排序 --C语言数据结构
- C语言基本数据结构之五(折半插入,堆排序,冒泡排序,快速排序,并归排序)
- 线性表数据结构C语言实现
- C++数据结构 排序 二分 插入 冒泡 基数 归并 直选 快排 希尔 堆排序