Naive in-place merge

Correctness

naiveInplaceMerge(L,l,m,r):    for im+1 to r:        for ji down to l+1:            if L[j1]L[j]:                break            swap L[j1] and L[j] \begin{aligned} & naiveInplaceMerge(L, l, m, r): \\ & \space \space \space \space \text{for } i \leftarrow m+1 \text{ to } r: \\ & \space \space \space \space \space \space \space \space \text{for } j \leftarrow i \text{ down to } l+1: \\ & \space \space \space \space \space \space \space \space \space \space \space \space \text{if } L[j-1] \leq L[j]: \\ & \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \text{break} \\ & \space \space \space \space \space \space \space \space \space \space \space \space \text{swap } L[j-1] \text{ and } L[j] \\ \end{aligned}

Lemma 1

Given a sorted list A=[a1,a2,...,aN]A = [a_1, a_2, ..., a_N] with NN elements, where a1a2...aNa_1 \leq a_2 \leq ... \leq a_N, and one value xx, the list L=A[x]=[a1,a2,...,aN,x]L = A \cup [x] = [a_1, a_2, ..., a_N, x] (xx is appended to the end of list AA), can be sorted by the naive in-place merge method with l=1,m=N,r=N+1l = 1, m = N, r = N + 1.

  • Base step: When N=0N = 0, list L=[x]L = [x] is trivially true
  • Induction Hypothesis: Suppose this assumption holds when N=kN = k
  • Induction Step: When N=k+1N = k + 1
    • If xL[k]x \geq L[k], then the L=a1a2...akxL = a_1 \leq a_2 \leq ... \leq a_k \leq x is naturally sorted
    • Otherwise, x<L[k]x < L[k] and the xx and L[k]L[k] are swapped.
      • Now L[1...k]=[a1,a2,...,ak1,x]L[1...k] = [a_1, a_2, ... , a_{k-1}, x]
      • By the hypothesis, the naive in-place merge works when N=kN = k, so we can a sorted L[1...k]L[1...k]
      • Thus, the list LL now is sorted since L[1...k]L[1...k] is sorted and all its elements are smaller than the current (k+1)(k+1)th element L[k]L[k]

Lemma 2

Given a sorted list A=[a1,a2,...,aN]A = [a_1, a_2, ..., a_N] with NN elements where a1a2...aNa_1 \leq a_2 \leq ... \leq a_N, and B=[b1,b2,...,bM]B = [b_1, b_2, ..., b_M] with MM elements where b1b2...bMb_1 \leq b_2 \leq ... \leq b_M, the list L=AB=[a1,a2,...,aN,b1,b2,...,bM]L = A \cup B = [a_1, a_2, ..., a_N, b_1, b_2, ..., b_M] can be sorted by the above naive in-place merge method with l=1,m=N,r=M+Nl = 1, m = N, r = M+N.

  • Base step: When M=1M = 1, the condition is same as Lemma 1, so it's true
  • Induction Hypothesis: Suppose this assumption holds when M=kM = k
  • Induction Step: When M=k+1M = k + 1
    • When i=N+1i = N + 1
      • the element L[N+1]L[N+1] will be merged with L[1...N]=AL[1...N] = A
      • then the list L[1...N+1]L[1...N+1] is sorted by Lemma 1
    • When i=N+2i = N + 2
      • the list is composed by sorted sublists A=L[1...N+1]A^\prime = L[1...N+1] and B=L[N+2...N+k+1]B^\prime = L[N+2...N+k+1] with kk elements
      • By the hypothesis, the naive in-place merge works when B=k\vert B^\prime \vert = k
      • Thus, the list L=AB=ABL = A^\prime \cup B^\prime = A \cup B is sorted

Complexity

O(N2)\mathcal{O}(N^2)

results matching ""

    No results matching ""