1-1 Introduction to Algorithms

Southgiri·2025년 7월 30일

SNUON Algorithm

목록 보기
1/7

Insertion Sort

  • Incremental approach

Loop Invariants and the Correctness Proof

  • We must show three things about a loop invariant
  1. Initialization
  2. Maintenance
  3. Termination

Loop Invariants

  • A condition that is always true for each iteration of a loop
  • The subarray A[1j1]A[1 \dots j-1] consists of the elements originally in A[1j1]A[1 \dots j-1] but in ascending order

1. Initialization

  • True prior to the first iteration
  • When j=2j=2, A[1j1]A[1 \dots j-1] consists of the element A[1]A[1] which is original one in A[1]A[1]
    Moreover, the subarray is sorted

2. Maintenance

  • If it is true before an iteration of the loop, it remains true before the next iteration
  • The subarray A[1j]A[1 \dots j] consists of the elements originally in A[1j]A[1 \dots j] in sorted order
  • Incrementing jj for the next iteration of the for loop preserves the loop invariant

3. Termination

  • When the loop terminates, show that the algorithm is correct
  • When j=n+1j=n+1, the for loop terminates
  • We have that A[1n]A[1\dots n] consists of the elements originally in A[1n]A[1\dots n] but in ascending order
  • Hence the entire array is sorted, which means that the algorithm is correct

Divide and Conquer

Solve a problem recursively by applying three steps at each level of the recursion

  • Divide the problem into a number of subproblems
  • Conquer the subproblem by solving them recursively
  • Combine the solutions to the subproblems into the solution for the original problem

Merge Sort

  • Divide
    • Divide the nn-element sequence to be sorted into two subsequences of n/2n/2 elements each
    • Compute the middle of the subarray, which takes Θ(1)\Theta(1) time
  • Conquer
    • Sort the two subsequences recursively using merge sort
    • Recursively solve two subproblems each of size n/2n/2, which contribute 2T(n/2)2T(n/2) to the running time
  • Combine
    • Merge the two sorted subsequences to produce the sorted answer
    • Take Θ(n)\Theta(n) time
  • The recursion bottoms out when the sequence has length 1, every sequence of length 1 is already in sorted order
  • Running time T(n)T(n) is 2T(n/2)+n)2T(n/2)+n)

MERGE Procedure

  • Assume that the subarrays A[pq]A[p\dots q] and A[q+1r]A[q+1\dots r] are in sorted order
  • Merge them to form a single sorted subarray that replaces the current subarray A[pr]A[p \dots r]
  • MERGE(A,p,q,r)MERGE(A,p,q,r) where AA is an array and p,q,rp,q,r are indices into the array such that pq<rp \leq q < r

Loop Invariant and the Correctness Proof

Loop Invariant

  • At the start of each iteration, the subarray A[pk1]A[p \dots k-1] contains the (kp)(k-p) smallest elements
  • 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

  • We have k=pk=p, so the subarray A[pk1]A[p \dots k-1] is empty
  • 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 AA

Maintenance

  • When L[i]R[j]L[i] \leq R[j]
    • L[i]L[i] is the smallest element
    • After line 14, the subarray will contain the kp+1k-p+1 smallest elements
    • Incrementing kk and ii reestablishes the loop invariant for the next iteration
  • When L[i]>R[j]L[i] > R[j]
    • Line 16, 17 perform the appropriate action

Termination

  • At termination, k=r+1k=r+1
  • The subarray A[pr]A[p \dots r] contains smallest elements of LL and RR in sorted order

1개의 댓글

comment-user-thumbnail
2025년 8월 1일

와 멋져요

답글 달기