Classic Algorithm: Double Pointer Problem - Array Merging

Algorithms - the soul of the program, yes it is the soul !

Today let's talk about the array merge problem in the double pointer problem

Content reference: "The Book of Python Algorithms You Can Understand"
Please mark the reprint: https://blog.csdn.net/qq_43582207
Python version:
Python3.7 IDE: Jupyter notebook
Author: My apologize

double pointer problem

First introduce a concept: "pointer", which is an object in programming language, it stores the address of a memory space, the computer can find the variable value through this address, and this specific address points to this specific value. The advantage is that fragmented memory space can be efficiently used. (However, there is no pointer in python. The following part involves the use of a list of "simulated pointers" to complete the pointer problem in python)

array merge

1. Merge sorted arrays

The pointer is the address of the memory space, and the value can be found by the subscript of each element in an array, so the subscript value that stores the position of this element can be regarded as a pointer ("analog pointer").
There are two ordered arrays:
l1 = [1, 3, 4, 6, 10]
l2 = [2, 5, 8, 11]
Purpose: Merge l1 and l2 into a new array from small to large

The first comparison compares l2[0] with l1[1], if l2[0] is greater than l1[0], move on to the next comparison with l1[1], and so on. If l2[0] is smaller than l1[0], insert l2[0] at the position of l1[0], and the following elements move backward in turn.
Then compare l2[1] until all elements in l2 are merged

Specific steps:
1. Initialize two arrays:

l1 = [1, 3, 4, 6, 10]
l2 = [2, 5, 8, 11]

2. Create a copy ans of l1 (a variable used to store the sorted array)
All insertion operations are to ans, so that the original l1 will not be modified

ans = l1.copy()

3. Traverse the subscript ind of l1 (the variable that stores the subscript of the value in the array)

ind = 0

4. A for loop is required to traverse the second array l2 from small to large

for i in range(0, len(l2)):

5. First outer loop i = 0, ind = 0, compare the first element of l2 with the first element of l1, if it is smaller than it, insert it after the first element of l1, then end the loop, change i=1 ; If it is greater than it, ind+1, compare the first element of l2 with the second element of l1, and repeat.
Program: while loop If l2[0] <= l1[0], insert l2[0] at the position of subscript 1 in ans, and then jump out of the while loop, otherwise ind adds 1 to 1, when the value of ind is equal to l1 also ends the while loop when the length of .

    while ind < len(l1):
        if l2[i] <= l1[ind]:
            ans.insert(ind + i, l2[i])
            break
        else:
            ind += 1

6. If the elements of l2 have been compared to the last element of l1, or the element of l2 is larger than the element of l1, it means that the element of l2 is larger than all the elements of l1, because l2 is in ascending order, and all the elements of l2 after it are larger, Just splice it directly after the l1 element, and then exit the for loop

    else:
        ans += l2[i:]
        break

Here is the complete code (which can be run directly):

l1 = [1, 3, 4, 6, 10]
l2 = [2, 5, 8, 11]
ind = 0
ans = l1.copy()
for i in range(0, len(l2)):
    while ind < len(l1):
        if l2[i] <= l1[ind]:
            ans.insert(ind + i, l2[i])
            break
        else:
            ind += 1
    else:
        ans += l2[i:]
        break
print(ans)
运行结果:
[1, 2, 3, 4, 5, 6, 8, 10, 11]

============================================================================

The python classic algorithm is continuously updated, and tomorrow's notice: binary search
/// Interested friends, pay attention and learn without getting lost! ///

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324085265&siteId=291194637