Basic Typical Algorithm Research: merging ordered arrays

Source: Internet
Author: User

When I did the second question of leetcode, I found that merging ordered arrays is a very interesting problem. Therefore, I would like to summarize some of the content below, which comes from the network.

Method 1:

Merge and call sort.

That is, merge the two arrays into an array, and then call the sort function for the merged array.

class Solution:    def getArray(self, A, B) :        for item in B :            A.append(item)        A.sort()

Method 2:

Extreme Value insertion method.

#include <stdio.h>void insert(int *array1, int len1, int *array2, int len2);int main(int argc, char **argv){int array1[128] = {2, 3, 4, 7, 9, 10, 12};int array2[8] = {1, 5, 11, 12, 14, 16, 18, 20};int i = 0;insert(array1, 7, array2, 8);for(i = 0; i < 15; i++){printf("%d ", array1[i]);}printf("\n");}void insert(int *array1, int len1, int *array2, int len2){int array1_pos = len1 - 1;int array2_pos = len2 - 1;int new_pos = len1 + len2 - 1;if (array1 == NULL || array2 == NULL)return;while (array2_pos >= 0 && array1_pos >= 0){if (array1[array1_pos] <=  array2[array2_pos]){array1[new_pos--] = array2[array2_pos--];}else{array1[new_pos--] = array1[array1_pos--];}}while (array2_pos >= 0){array1[new_pos--] = array2[array2_pos--];}return;}

Method 3:

Insert loop comparison method.

The I element of array a is compared with the J element of array B.

// Compare to this step, it means that there are already I elements in a stored in C, and J elements in B are saved in C. Currently, the I + J elements have been saved in C. Therefore, the result of the next comparison should be placed in Unit C [I + J ].


If a [I] <B [J] C [I + J] = A [I]; I ++; otherwise, C [I + J] = B [J]; j ++;

 

Then, compare the sizes of a [I] and B [J] Again. Therefore, the previous procedures need to be continuously cyclic, the ending mark indicates that all elements in A or B have been saved to C.


While (A has untraversed elements & B also has untraversed elements) {if a [I] <B [J] C [I + J] = A [I]; I ++; otherwise C [I + J] = B [J]; j ++;} while (A contains elements not traversed) c [J + I ++] = A [I ++]; while (there are still elements not traversed in B) c [I + J ++] = B [J ++];
Example 1

// Merge the ordered array STD: vector <int> A; STD: vector <int> B; // place the elements in the array in order // place the elements in the B array in order STD: vector <int> C; STD: vector <int> :: iterator I1 =. begin (); STD: vector <int >:: iterator I2 = B. begin (); While (I1! = A. End () & I2! = B. end () {If (* I1 <* I2) {C. push_back (* I1); I1 ++;} else {C. push_back (* I2); I2 ++ ;}} while (I1! = A. End () c. push_back (* I1 ++); While (I2! = B. End () c. push_back (* I2 ++ );

Algorithm Example 2:

As defined below:

typedef struct{ double index; double value1; double value2;} line;std::vector<line> v1;std::vector<line> v2;

Elements in V1 and V2 are arranged in ascending order of their index values, and the index is not repeated.
Now I want to create a V3 to merge V1 and V2.
The merging rules are as follows:
1) The merged V3 elements are arranged in ascending order of the index, and the index is not repeated.
2) If the elements in V1 and V2 have the same index, they are merged into one element. Then, value1 of this element is set to value1 of V1, and value2 is set to value2of V2.
3) if the index of an element only exists in V1, The value2 value of this element is changed to 0. If the index of an element only exists in V2, The value1 value of this element is changed to 0.
    std::vector<line>::iterator i1 = v1.begin(), i2 = v2.begin();    while(i1 != v1.end() && i2 != v2.end())    {        if(i1->index == i2->index)        {            line t = { i1->index, i1->value1, i2->value2 }            v3.push_back(t);            ++i1;            ++i2;        }        else if(i1->index > i2->index)        {            i2->value1 = 0;            v3.push_back(*i2);            ++i2;        }        else        {            i1->value2 = 0;            v3.push_back(*i1);            ++i1;        }    }    while(i1 != v1.end())        v3.push_back(*(i1++));    while(i2 != v2.end())        v3.push_back(*(i2++))

Algorithm Example 3:

// Combine the deletable and underline positions of node; STD: vector <node> eraseposition; STD: vector <node >:: iterator I1 = delposition. begin (), I2 = underlineposition. begin (); While (I1! = Delposition. End () & I2! = Underlineposition. end () {If (I1-> nstart = I2-> nend) // merge and move forward simultaneously ++ {node. nstart = I2-> nstart; node. nend = I1-> nend; I1 ++; I2 ++; eraseposition. push_back (node); continue;} If (I1-> nend = I2-> nstart) {node. nstart = I1-> nstart; node. nend = I2-> nend; I1 ++; I2 ++; eraseposition. push_back (node); continue;} If (I1-> nend <I2-> nstart) {node = * I1; I1 ++; eraseposition. push_back (node); continue;} If (I1-> nstart> I2-> nend) {node = * I2; I2 ++; erasepo Sition. push_back (node); Continue ;}} while (I1! = Delposition. End () eraseposition. push_back (* (I1 ++); While (I2! = Underlineposition. End () eraseposition. push_back (* (I2 ++ ));


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.