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

野性酷女 2022-01-08 00:39 358阅读 0赞

之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个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. 1 #include <stdio.h>
  2. 2
  3. 3
  4. 4 void mergetwosort(int a[],int m,int b[],int n)
  5. 5 {
  6. 6 int len1=m-1;
  7. 7 int len2=n-1;
  8. 8 int len3=m+n-1;
  9. 9 while(len1>=0&&len2>=0)
  10. 10 {
  11. 11 a[len3--]=a[len1]>b[len2]? a[len1--]:b[len2--];
  12. 12 }
  13. 13 while(len2>=0)
  14. 14 {
  15. 15 a[len3--]=b[len2--];
  16. 16 }
  17. 17 }
  18. 18
  19. 19 int main()
  20. 20 {
  21. 21 int a[4]={
  22. 1,2,4,9};
  23. 22 int b[7]={
  24. 1,3,4,5,7,8,9};
  25. 23 mergetwosort(a,4,b,7);
  26. 24 for (int i = 0; i < 11; i++)
  27. 25 {
  28. 26 printf("%d ",a[i] );
  29. 27 }
  30. 28 return 0;
  31. 29 }

转载于:https://www.cnblogs.com/tao-alex/p/5873470.html

发表评论

表情:
评论列表 (有 0 条评论,358人围观)

还没有评论,来说两句吧...

相关阅读

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

    之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个SMR的博客暂时没有开放,已经写好了,以后会慢慢开放的 今天再更新一个有关排序的算法题 1 Merge So