插入排序和归并排序的训练把,还是不太熟,自己写不出来
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=111;
int n,origin[N],tempN[N],changeN[N],flag = 0;
int isSame(int a[],int b[])
{
for(int i = 0;i<n;i++)
{
if(a[i] != b[i])
return 0;
}
return 1;
}
bool showAarry(int a[])
{
for(int i = 0;i<n;i++)
{
printf("%d",a[i]);
if(i<n - 1)
printf(" ");
}
printf("\n");
}
void inserS()
{
for(int i = 1;i<n;i++)
{
if(i!=1 && isSame(tempN,changeN) == 1)
{
flag = 1;
}
int temp = tempN[i];
int j;
for(j = i;j>0;j--)
{
if(tempN[j-1] > temp)
{
tempN[j] = tempN[j-1];
}
else
break;
}
tempN[j] = temp;
if(flag == 1)
break;
}
}
void mergesort(int a[],int l1,int r1,int l2 ,int r2)
{
int i = l1,j=l2,index = 0;
int temp[N];
while(i<=r1 && j<=r2)
{
if(a[i]<a[j])
temp[index++]=a[i++];
else
temp[index++]= a[j++];
}
while(i<=r1) temp[index++] = a[i++];
while(j<=r2) temp[index++] = a[j++];
for(int i = 0;i<index;i++)
{
a[i+l1] = temp[i];
}
}
void mergeS()
{
int step;
for(step = 2;step/2<n;step *= 2)
{
if(step != 2 && isSame(tempN,changeN) == 1)flag =2;
for(int i = 0;i<n;i+=step)
{
// int mid = i+step/2-1;
// if(mid + 1<=n - 1)
// mergesort(tempN,i,mid,mid+1,min(i+step - 1,n));
sort(tempN + i,tempN + min(n,i+step)); //这里用sort代替了归并的过程,自己写的归并会有错误
}
if(flag == 2)
{
showAarry(tempN);
return;
}
}
}
int main()
{
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
scanf("%d",&origin[i]);
tempN[i] = origin[i];
}
for(int i = 0;i<n;i++)
{
scanf("%d",&changeN[i]);
}
inserS();
if(flag == 1){
printf("Insertion Sort\n");
showAarry(tempN);
}
else
{
printf("Merge Sort\n");
for(int i = 0;i<n;i++)
{
tempN[i] = origin[i];
}
mergeS();
}
}
=====================================
错误点在从0开始,所以结束的地方应该是n-1而不是n
这样就正确了
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=111;
int n,origin[N],tempN[N],changeN[N],flag = 0;
int isSame(int a[],int b[])
{
for(int i = 0;i<n;i++)
{
if(a[i] != b[i])
return 0;
}
return 1;
}
bool showAarry(int a[])
{
for(int i = 0;i<n;i++)
{
printf("%d",a[i]);
if(i<n - 1)
printf(" ");
}
printf("\n");
}
void inserS()
{
for(int i = 1;i<n;i++)
{
if(i!=1 && isSame(tempN,changeN) == 1)
{
flag = 1;
}
int temp = tempN[i];
int j;
for(j = i;j>0;j--)
{
if(tempN[j-1] > temp)
{
tempN[j] = tempN[j-1];
}
else
break;
}
tempN[j] = temp;
if(flag == 1)
break;
}
}
void mergesort(int a[],int l1,int r1,int l2 ,int r2)
{
int i = l1,j=l2,index = 0;
int temp[N];
while(i<=r1 && j<=r2)
{
if(a[i]<a[j])
temp[index++]=a[i++];
else
temp[index++]= a[j++];
}
while(i<=r1) temp[index++] = a[i++];
while(j<=r2) temp[index++] = a[j++];
for(int i = 0;i<index;i++)
{
a[i+l1] = temp[i];
}
}
void mergeS()
{
int step;
for(step = 2;step/2<n;step *= 2)
{
if(step != 2 && isSame(tempN,changeN) == 1)flag =2;
for(int i = 0;i<n;i+=step)
{
int mid = i+step/2-1;
if(mid + 1<=n - 1)
mergesort(tempN,i,mid,mid+1,min(i+step - 1,n-1));
// sort(tempN + i,tempN + min(n,i+step));
}
if(flag == 2)
{
showAarry(tempN);
return;
}
}
}
int main()
{
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
scanf("%d",&origin[i]);
tempN[i] = origin[i];
}
for(int i = 0;i<n;i++)
{
scanf("%d",&changeN[i]);
}
inserS();
if(flag == 1){
printf("Insertion Sort\n");
showAarry(tempN);
}
else
{
printf("Merge Sort\n");
for(int i = 0;i<n;i++)
{
tempN[i] = origin[i];
}
mergeS();
}
}