为何这段归并排序的代码通过编译后不能运行
为什么这段归并排序的代码通过编译后不能运行?
------解决思路----------------------
换一个思路,比如下面这个:
------解决思路----------------------
你这个程序有点凌乱,下面是我写的一个归并排序的程序,你可以参考一下,希望可以给你帮助,主要还是要理清算法的过程
#include<stdio.h>
#define max 5
void mergearray(int a[],int first,int mid,int last)
{
int i=first,j=mid+1;
int m=mid,n=last;
int k=0;
int temp[max];
while(i<=m&&j<=n)
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=m)
temp[k++]=a[i++];
while(j<=n)
temp[k++]=a[j++];
for(i=0;i<k;i++)
a[first+i]=temp[i];
}
void mergesort(int a[],int first,int last)
{
if(first<last)
{
int mid=(first+last)/2;
mergesort(a,first,mid);
mergesort(a,mid+1,last);
mergearray(a,first,mid,last);
}
}
int main()
{
int i;
int a[max]={4,3,1,7,6};
mergesort(a,0,max-1);
for(i=0;i<max;i++)
printf("%2d",a[i]);
return 0;
}
// MergeSort.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <malloc.h>
void Merge(int data1[], int data2[], int s, int m, int n) /*将分别有序的data1[s..m]和data1[m+1..n]归并为有序的data2[s..n]*/
{
int i, k;
for (i = m + 1, k = s; s <= m && i <= n; ++k) /*将data1中的记录由小到大地并入data2*/
if (data1[s] < data1[i]) data2[k] = data1[s++];
else data2[k] = data1[i++];
for (; s <= m; ++k) /*将剩余的data1[s...m]复制到data2*/
data2[k] = data1[s++];
for (; i <= n; ++k) /*将剩余的data1[i...n]复制到data2*/
data2[k] = data1[i++];
}/*Merge*/
void MSort(int data1[], int data2[], int s, int t) /*递归形式的两路归并。将data1[s..t]归并排序为data2[s..t]*/
{
int m, *temp;
if (s == t) data2[s] = data1[s];
else
{
temp = (int *)malloc((t - s + 1) * sizeof(int)); /*辅助空间*/
m = (s + t) / 2; /*将data1[s..t]均分为data1[s..m]和data1[m+1..t]*/
MSort(data1, temp, s, m); /*递归地将data1[s..m]归并为有序的temp[s..m]*/
MSort(data1, temp, m + 1, t); /*递归地将data1[m+1..t]归并为有序的temp[m+1..t]*/
Merge(temp, data2, s, m, t); /*将temp[s..m]和temp[m+1..t]归并到data2[s..t]*/
free(temp);
}
}/*MSort*/
void MergeSort(int data1[], int n) /*对一维数组data1[0..n-1]中的元素进行两路归并排序*/
{
MSort(data1, data1, 0, n - 1);
}/*MergeSort*/
int _tmain(int argc, _TCHAR* argv[])
{
int data[10] = { 48, 37, 64, 96, 75, 12, 26, 48, 54, 3 };
int i;
printf("The array before the merge sortting:");
for (i = 0; i < 10; i++)
{
printf("%d ", data[i]);
}
putchar('\n');
MergeSort(data, 10);
printf("The array after the merge sortting:");
for (i = 0; i < 10; i++)
{
printf("%d ", data[i]);
}
putchar('\n');
return 0;
}
------解决思路----------------------
换一个思路,比如下面这个:
void mergearray(int *a, int first, int mid, int last, int *tmp)
{
int i, j, k, r;
k = r = 0;
i = first, j = mid + 1;
do
{
if (a[i] <= a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
} while (i <= mid && j <= last);
while (i <= mid) tmp[k++] = a[i++];
while (j <= last) tmp[k++] = a[j++];
do
{
a[first + r] = tmp[r];
r++;
} while (r < k);
}
void mergesort(int *a, int first, int last, int *tmp)
{
if (first < last)
{
int mid = first + ((last - first) >> 1);
mergesort(a, first, mid, tmp);
mergesort(a, mid + 1, last, tmp);
mergearray(a, first, mid, last, tmp);
}
}
void MergeSort(int *a, int n)
{
int *tmp = malloc(sizeof(int)*n);
mergesort(a, 0, n - 1, tmp);
free(tmp);
}
int main(void)
{
int arr[] = { 2,1,6,4,0,3,9,5,8,7 };
MergeSort(arr, 10);
return 0;
}
------解决思路----------------------
你这个程序有点凌乱,下面是我写的一个归并排序的程序,你可以参考一下,希望可以给你帮助,主要还是要理清算法的过程
#include<stdio.h>
#define max 5
void mergearray(int a[],int first,int mid,int last)
{
int i=first,j=mid+1;
int m=mid,n=last;
int k=0;
int temp[max];
while(i<=m&&j<=n)
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=m)
temp[k++]=a[i++];
while(j<=n)
temp[k++]=a[j++];
for(i=0;i<k;i++)
a[first+i]=temp[i];
}
void mergesort(int a[],int first,int last)
{
if(first<last)
{
int mid=(first+last)/2;
mergesort(a,first,mid);
mergesort(a,mid+1,last);
mergearray(a,first,mid,last);
}
}
int main()
{
int i;
int a[max]={4,3,1,7,6};
mergesort(a,0,max-1);
for(i=0;i<max;i++)
printf("%2d",a[i]);
return 0;
}