Getting Started - 03

NanSu·2022년 9월 14일
post-thumbnail

Designing algorithms



The divide-and-conquer approach

1. Divide-and-Conquer

  • Many recursive algorithms follow a divide-and-conquer approach
  • Three steps at each level of the recursion
    • Divide

      Divide the problem into a number of subproblems that are smaller instances of the same problem

    • Conquer

      Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner.

    • Combine

      Combine the solutions to the subproblems into the solution for the original problem

  • The merge sort algorithm colsely follows the divide-and-conquer paradigm
    • Divide

      Divide the nn-element sequence to be sorted into two subsequences of nn/2 elements each

    • Conquer

      Sort the two subsequences recursively using merge sort

    • Combine

      Merge the two sorted subsequences to produce the sorted answer

2. Merge procedure

  • AA is an array and p,q,p, q, and rr are indices into the array such that pq<rp \leq q < r

  • The subarrays A[pq]A[p \dots q] and A[q+1r]A[q+1 \dots r] are in sorted order

  • It merges the two subarrays to form a single sorted subarray that replaces the current subarray A[pr]A[p \dots r]

  • This procedure takes time Θ(n)\Theta(n), where n=rp+1n = r - p + 1 is the total number of elements being merged

  • \infty is sentinel value, so that whenever a value with \infty is exposed, it cannot be the smaller value unless both subarrays have their sentinel value exposed

  • The operation of lines 10-7 in the call MERGE(A,9,12,16)(A, 9, 12, 16)


  • Loop invariant

    At the start of each iteration of the for loop of lines 12-17, the subarray A[pk1]A[p \dots k - 1] contatins the kpk - p smallest elements of L[1n1+1]L[1 \dots n_1 + 1] and R[1n2+1]R[1 \dots n_2 + 1], in sorted order. Moreover, L[i]L[i] and R[j]R[j] are the smallest elements of their arrays that have not been copied back into AA.

    • Initialization

      Prior to the first iteration of the loop, we have k=pk = p, so that the subarray A[pk1]A[p \dots k - 1] is empty. This empty subarray contains the kp=0k - p = 0 smallest elements of LL and RR, and since i=j=1i = j =1, both L[i]L[i] and R[j]R[j] are the smallest elements of their arrays that have not been copied back into A.

    • Maintenance

      Let it first suppose that L[i]R[j]L[i] \leq R[j]. Then L[i]L[i] is the smallest element not yet copied back into AA. Because A[pk1]A[p \dots k - 1] contains the kpk - p smallest elements, after line 14 copies L[i]L[i] into A[k]A[k], the subarray A[pk]A[p \dots k] will contain the kp+1k - p + 1 smallest elements. Incrementing kk and ii reestablishes the loop invariant for the next iteration. If instead L[i]>R[j]L[i] > R[j], then lines 16-17 perform the appropriate action to maintain the loop invariant.

    • Termination

      At termination, k=r+1k = r + 1. By the loop invariant, the subarray A[pk1]A[p \dots k - 1], which is A[pr]A[p \dots r], contains the kp=rp+1k - p = r - p + 1 smallest elements of L[1n1+1]L[1 \dots n_1 + 1] and R[1n2+1]R[1 \dots n_2 + 1], in sorted order. The arrays LL and RR together contain n1+n2+2=rp+3n_1 + n_2 + 2 = r - p + 3 elements. All but the two largest have been copied back into A, and these two largest elements are the sentinels.

3. Merge sort algorithm

  • The MERGE-SORT(A,p,r)(A, p, r) sorts the elements in the subarray A[pr]A[p \dots r]
  • If prp \geq r, the subarray has at most one element and is therefore already sorted
  • An index qq partitions A[pr]A[p \dots r] into two subarrays: A[pq]A[p \dots q], containing n/2\lceil n/2 \rceil elements, and A[q+1r]A[q + 1 \dots r], containing n/2\lfloor n/2 \rfloor elements
  • The operation of merge sort on the array A=5,2,4,7,1,3,2,6A = \langle 5,2,4,7,1,3,2,6 \rangle
profile
return NanSu;

0개의 댓글