In Place Algorithm

 

This article is a study note for in place algorithm. Currently learning the two algorithms in place merge and in place martrix transposition.

1. in place goes

Paper link: Practical in-place merging

 The paper discusses how to merge two adjacent sorted arrays in O(n) time complexity and O(1) space complexity.

b) Divide sublist1 and sublist2 into blocks by sqrt(n), and move the remaining tail elements to the beginning as buffers (buffer size >= sqrt(n), we don’t care whether the elements in the buffer are ordered)

c) Selection sort ( O(sqrt(n)*sqrt(n)) ), sort each block so that the end elements of each block are in a non-descending sequence.

 

a) Find the first block tail element a[i], such that a[i] > a[i+1], obviously the previous one satisfies a[i] <= a[i+1], then the previous one has already formed an ordered sequence . Divide series1 and series2.

b) c) Merge series1 and series2 through the buffer until all elements in series1 are arranged in the correct position, as shown in c).

  Since the blocks are sorted according to the non-descending end of the segment, it is obvious that series1 will be finished before series2.

 

 a) b) c) Take series2 as series1, the next block of series2 as series2, and repeat the previous operation. The total time complexity of the above operations is tail O(n).

 d) sqrt(n) <= buffer size < 2*sqrt(n), bubble sort/selection sort, change buffer into ordered sequence, time complexity O(sqrt(n)*sqrt(n)).

  

Attachment: In the paper, the element in the buffer is the largest, and the last step is to operate the elements in the sort buffer.

Personal opinion: We need to ensure that buffer elements >= non-buffer elements, otherwise we still need insert sort. Therefore, I think that it needs to be processed before the merge, and the elements with the largest sqrt(n) are extracted from sublist1 and sublist2 as buffers. But this may break the block, resulting in insufficient block elements. Personally, I think I can cut a little more and make up for it. For example, the first 3*sqrt(n) elements are deducted first. In this way, back to sublist1, there are at least sqrt(n) elements left.

The total time complexity is O(n). The constant may be slightly larger.

 

2.in place matrix transposition

wiki link: in place martrix transposition

 For non-square matrices, perform a permutation operation. For a Circle, record the initial node and replace the current node with the predecessor node each time; replace the last node with the initial node.

Non-square matrices: Following the cycles

for each length>1 cycle C of the permutation
    pick a starting address s in C
    let D = data at s
    let x = predecessor of s in the cycle
    while x ≠ s
        move data from x to successor of x
        let x = predecessor of x
    move data from D to successor of s

Improving memory locality at the cost of greater total data movement

 reference to wiki

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325083601&siteId=291194637