Merge two ordered Series

Source: Internet
Author: User

Question: A [] and B [] are arrays in an ordered order. Merge A [] and B [] into C []. For example: A [] = {,}; B [] = {,}, we need to get C [] =, 8, 10, 15, 20 }.

Solution: 1) compare a [0] and B [0] first. If a [0] is relatively small, assign the value of a [0] to C [0]. then compare a [1] and B [0]. 2) When all the values in a number of groups have been assigned to C [], the remaining elements of the other array are directly assigned to C [] at a time.

The following is implemented in Java.

Public class test {public static void main (string [] ARGs) {int [] A = {10, 20, 50, 70, 80,500,100 1}; int [] B = {10, 40, 60, 90, 100,120,200}; int alength =. length; int blength = B. length; int [] C = new int [alength + blength]; int aindex = 0; int bindex = 0; For (INT I = 0; I <alength + blength; I ++) {if (a [aindex] <= B [bindex]) {C [I] = A [aindex]; If (aindex <alength-1) {aindex ++;} // else {for (int t = I + 1; t <alength + blength; t ++) When all data in array A has been read) {c [T] = B [bindex]; bindex ++;} break ;}} else {C [I] = B [bindex]; If (bindex <blength-1) {bindex ++;} // when all data in array B has been read, else {for (int t = I + 1; t <alength + blength; t ++) {c [T] = A [aindex]; aindex ++;} break ;}}for (INT I = 0; I <alength + blength; I ++) {system. out. println (C [I]) ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.