//合并将两个有序序列([low,mid],[mid+1,high])合并
void Merge(int arr[],int low,int mid,int high)
{
int i=low,j=mid+1,p=0;
int *newarr = (int *)malloc((high-low+1)*sizeof(int));//用来暂存排序好的数据
if(!newarr){
printf("malloc error!\n");
exit(1);
}
while(i<=mid && j<=high){ //以下过程很类似两个有序字符串合并成一个有序字符串
if(arr[i] < arr[j])
newarr[p++] = arr[i++];
else
newarr[p++] = arr[j++];
}
while(i<=mid)
newarr[p++] = arr[i++];
while(j<=high)
newarr[p++] = arr[j++];
for(i=low,p=0;p<(high-low+1);i++,p++) //将结果复制到原数组当中
arr[i] = newarr[p];
free(newarr);
}
/*
* File: mergesort.c
* Time: 2014-07-19 HJJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static void merge1(int array[], int tmp[], int lpos, int rpos, int rend);
static void msort1(int array[], int tmp[], int left, int right);
void merge_sort1(int array[], int n)
{
assert(array!=NULL && n>1); //条件不满足,退出程序并打印错误语句。
int *tmp = (int *)malloc(sizeof(int) * n);
assert(tmp != NULL);
int i;
for (i = 0; i < n; i ++) {
tmp[i] = array[i];
}
msort1(array, tmp, 0, n-1);
free(tmp);
}
//递归的调用此函数,实现折半划分,只完成划分,不实现排序,最终返回array[]数组有序
static void msort1(int array[], int tmp[], int left, int right)
{
assert(array!=NULL && tmp!=NULL);
if (left == right)
return;
int center = (left + right) / 2;
msort1(tmp, array, left, center);
msort1(tmp, array, center+1, right);
merge1(tmp, array, left, center+1, right);
}
//该函数实现,将array[]的左右两半排好序的数组,归并为tmp[],并排序
static void merge1(int array[], int tmp[], int lpos, int rpos, int rend)
{
assert(array!=NULL && tmp!=NULL);
int lend = rpos - 1;
int tmp_pos = lpos;
while (lpos<=lend && rpos<=rend) {
if (array[lpos] <= array[rpos])
tmp[tmp_pos++] = array[lpos++];
else
tmp[tmp_pos++] = array[rpos++];
}
while (lpos <= lend)
tmp[tmp_pos++] = array[lpos++];
while (rpos <= rend)
tmp[tmp_pos++] = array[rpos++];
}
int main(int argc, char *argv[])
{
int a[7] = {6, 5, 4, 3, 2, 1, 7};
merge_sort1(a, 7);
int i;
for (i = 0; i < 7; i ++) {
printf("%3d", a[i]);
}
printf("\n");
return 0;
}
void merge_sort1(int array[], int n)
{
assert(array!=NULL && n>1); //条件不满足,退出程序并打印错误语句。
int *tmp = (int *)malloc(sizeof(int) * n);
assert(tmp != NULL);
int i;
for (i = 0; i < n; i ++) {
tmp[i] = array[i];
}
msort1(array, tmp, 0, n-1);
free(tmp);
}
//递归的调用此函数,实现折半划分,只完成划分,不实现排序,最终返回array[]数组有序
static void msort1(int array[], int tmp[], int left, int right)
{
assert(array!=NULL && tmp!=NULL);
if (left == right)
return;
int center = (left + right) / 2;
msort1(tmp, array, left, center);
msort1(tmp, array, center+1, right);
merge(tmp, array, left, center+1, right);
}
void merge(int array[],int tmp[],int lpos,int rpos,int rend)
{
int i,leftend,num,tmppos;
leftend = rpos - 1;
num = rend - lpos + 1;
tmppos = lpos;
while(lpos <= leftend && rpos <= rend){
if(array[lpos] <= array[rpos])
tmp[tmppos++] = array[lpos++];
else
tmp[tmppos++] = array[rpos++];
}
while(lpos <= leftend)
tmp[tmppos++] = array[lpos++];
while(rpos <= rend)
tmp[tmppos++] = array[rpos++];
for(i = 0;i < num;i++,rend--)
array[rend] = tmp[rend];
}
/*自底向上,这里就不写真正的代码了,从网上copy了*/
void MergePass(SeqList R,int length)
{ //对R[1..n]做一趟归并排序
int i;
for(i=1;i+2*length-1<=n;i=i+2*length)
Merge(R,i,i+length-1,i+2*length-1);
//归并长度为length的两个相邻子文件
if(i+length-1<n) //尚有两个子文件,其中后一个长度小于length
Merge(R,i,i+length-1,n); //归并最后两个子文件
//注意:若i≤n且i+length-1≥n时,则剩余一个子文件轮空,无须归并
} //MergePass
void MergeSort(SeqList R)
{//采用自底向上的方法,对R[1..n]进行二路归并排序
int length;
for(1ength=1;length<n;length*=2) //做 趟归并
MergePass(R,length); //有序段长度≥n时终止
}
void MSort(int arr[],int low,int high)
{
if(low < high){
int mid = (low+high)/2;
MSort(arr,low,mid); //左半区排序
MSort(arr,mid+1,high); //右半区排序
Merge(arr,low,mid,high);//左右半区合并
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有