Data Structures19 sections · 729 units
Open in Course

Cut and Paste - Analysis

The split-merge dance

To move [l,r][l, r] to the end:

1.1. Split at ll: get [0,l1][0, l-1] and [l,n1][l, n-1]

2.2. Split the second part at rl+1r-l+1: get [l,r][l, r] and [r+1,n1][r+1, n-1]

3.3. Merge: [0,l1]+[r+1,n1]+[l,r][0, l-1] + [r+1, n-1] + [l, r]

Each split and merge is O(logn)O(\log n). With 22 splits and 22 merges, total time per operation is O(logn)O(\log n). This pattern (split, rearrange, merge) handles many sequence manipulation problems.