O (n) processes two descending arrays, shuffles them to increase, and one array element is smaller than the other array element

Source: Internet
Author: User

The two descending arrays must be shuffled to make them increase progressively, and the number of arrays A is smaller than that of arrays B. Use O (n.

Void processorderedarray (int * arr1, size_t count1, int * arr2, size_t count2) <br/>{< br/> assert (arr1! = NULL & arr2! = NULL); </P> <p> int * Parr = new int [count1 + count2]; <br/> size_t I = 0, j = 0, k = count1 + count2-1; <br/> while (I <count1 & J <count2) <br/>{< br/> If (arr1 [I]> arr2 [J]) <br/>{< br/> Parr [k --] = arr1 [I]; <br/> I ++; <br/>}< br/> else if (arr1 [I] <arr2 [J]) <br/>{< br/> Parr [k --] = arr2 [J]; <br/> J ++; <br/>}< br/> else <br/> {<br/> Parr [k --] = arr1 [I]; <br/> Parr [k --] = arr2 [J]; <br/> I ++; j ++; <br/>}</P> <p> while (I <count1) Parr [k --] = arr1 [I ++]; <br/> while (j <count2) Parr [k --] = arr2 [J ++]; </P> <p> memcpy (arr1, Parr, count1 * sizeof (INT); <br/> memcpy (arr2, Parr + count1, count2 * sizeof (INT); </P> <p> Delete [] Parr; <br/>}</P> <p> int main (INT argc, char * argv []) <br/> {<br/> int A [m] = {6, 5, 2, 1, 0}; <br/> int B [N] = {8, 7, 4, 3 }; <br/> processorderedarray (a, m, B, n); <br/> for (INT I = 0; I <m; I ++) <br/> cout <A [I] <Endl; <br/> for (Int J = 0; j <n; j ++) <br/> cout <B [J] <Endl; <br/> return 0; <br/>}< br/>

 

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.