130-Merge two ordered arrays (ascending order) (best method)

Merge two ordered arrays (ascending order) (best method)
sums1
sums2 are the names of the two arrays and
merge them into the sums1 array (sums1 is large enough)

//合并有序数组 
#include<stdio.h>
int main()
{
    
    
	int len=m+n-1;//sums1数组的大小 
	int i=m-1;//sums1数组合并前的最后一个下标 
	int j=n-1;//sums2数组的最后一个下标 
	while(i>=0&&j>=0)//遍历 
	{
    
    
		if(nums1[i]>nums2[j])//从最后一个数进行对比放在最后的位置 
		{
    
    
			nums1[len--]=nums1[i--];
		}
		else
		{
    
    
			nums1[len--]=nums2[j--];
		}
	}
	if(j>=0)//若剩下sums2还有数字,全部放进sums1中 
	{
    
    
		for(;j>=0;j--)
		{
    
     
			nums1[j]=nums2[j];
		} 
	}
	for(int k=0;k<m+n;k++)//打印合并后的sums1数组 
	{
    
    
		printf("%d",nums1[k]);
	}
	return 0;
	
}

Guess you like

Origin blog.csdn.net/LINZEYU666/article/details/112295686