算法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 }
时间: 2024-12-26 07:05:45

算法5--排序--Merge Sorted Array的相关文章

leetcode 题解: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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

LeetCode (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. 刚开始根据归并排序写的多了一个中

No.88 Merge Sorted Array

No.88 Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The

Leetcode:Merge Sorted Array 归并排序

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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in

LeetCode --- 88. Merge Sorted Array

题目链接: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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

[LeetCode] Merge Sorted Array [22]

题目 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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay