c语言程序关于合并两个 有序数组 请大神帮我把我的程序改一下 最好能在我错的地方加注释谢谢

Python07

c语言程序关于合并两个 有序数组 请大神帮我把我的程序改一下 最好能在我错的地方加注释谢谢,第1张

#include<stdio.h>

#include <iostream>

using namespace std

int main()

{

int i,j,k,p,n,m

j=0

k=0

int a[20],b[20],c[20]

//scanf("%d",&m)

// for (i=0i<mi++)

//{

// scanf("%d",&a[i])

// }

   m=5

   n=5

   a[0]=1a[1]=3a[2]=5a[3]=7a[4]=9

   b[0]=2b[1]=4b[2]=6b[3]=8b[4]=10

// scanf("%d",&n)

// for (i=0i<ni++)

// {

// scanf("%d",&b[i])

// }

int a1=0,a2=0

for (i=0i<m+ni++)

{

if(a[a1]<=b[a2]){

if(a1>=m){c[i] = b[a2]

a2++}

else{

c[i] = a[a1]

a1++

}

}else{

if(a2>=n){

c[i] = a[a1]

a1++

}else{

c[i] = b[a2]

a2++

}

}

}

for (i=0i<m+ni++)

{

printf("%d\n",c[i])

}

return 0

}

#include<stdio.h>

voidmain()

{

inta[10],b[10],c[20],i,ia,ib,ic

printf("pleaseinputthefirstarray\n")

for(i=0i<10i++)

scanf("%d",&a[i])

for(i=0i<10i++)

scanf("%d",&b[i])

printf("\n")

ia=0ib=0ic=0

while(ia<10&&ib<10)

{

if(a[ia]<b[ib])

{

c[ic]=a[ia]

ia++

}

else{

c[ic]=b[ib]

ib++

}

ic++

}

while(ia<10)

{

c[ic]=a[ia]

ia++

ic++

}

while(ib<10)

{

c[ic]=b[ib]

ib++

ic++

}

for(i=0i<20i++)

{

printf("%5d",c[i])}

}

扩展资料

C语言归并排序

#include<stdio.h>

#defineLEN8

inta[LEN]={5,2,4,7,1,3,2,6}

inttempa[LEN]

voidmerge(inta[],inttempa[],intleft,intright,intrightend)

{

inti,temp

intleftend=right-1

intnum=rightend-left+1

temp=left

while(left<=leftend&&right<=rightend)

{

if(a[left]<a[right])

{

tempa[temp++]=a[left++]

}

else

{

tempa[temp++]=a[right++]

}

}

while(left<=leftend)

{

tempa[temp++]=a[left++]

}

while(right<=rightend)

{

tempa[temp++]=a[right++]

}

for(i=0i<numi++,rightend--)

{

a[rightend]=tempa[rightend]

}

}

voidsort(inta[],inttempa[],intleft,intright)

{

intmid

if(left<right)

{

mid=(left+right)/2

sort(a,tempa,left,mid)

sort(a,tempa,mid+1,right)

merge(a,tempa,left,mid+1,right)

}

}

intmain()

{

sort(a,tempa,0,7)

for(inti=0i<LEN++i)

{

printf("%d,",a[i])

}

return0

}