首页 > 代码库 > 算法5--排序--Merge Sorted Array

算法5--排序--Merge Sorted Array

之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个SMR的博客暂时没有开放,已经写好了,以后会慢慢开放的

 

今天再更新一个有关排序的算法题

1  Merge Sorted Array
描述
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note: You may assume that A has enough space to hold additional elements from B. the number of
elements initialized in A and B are m and n respectively.
 
 1 #include <stdio.h> 2  3  4 void mergetwosort(int a[],int m,int b[],int n) 5 { 6     int len1=m-1; 7     int len2=n-1; 8     int len3=m+n-1; 9     while(len1>=0&&len2>=0)10     {11         a[len3--]=a[len1]>b[len2]? a[len1--]:b[len2--];12     }13     while(len2>=0)14     {15         a[len3--]=b[len2--];16     }17 }18 19 int main()20 {21     int a[4]={1,2,4,9};22     int b[7]={1,3,4,5,7,8,9};23     mergetwosort(a,4,b,7);24     for (int i = 0; i < 11; i++)25     {26         printf("%d ",a[i] );27     }28     return 0;29 }

 

算法5--排序--Merge Sorted Array