Lean으로 배우는 증명가능한 프로그래밍

9.6.5. 삽입 정렬의 최악 비교 횟수🔗

상한에 도달하는 입력

그림 9.20에 길이가 같은 두 입력이 있다. 오름차순 입력은 새 값을 항상 앞에 넣으므로 정렬된 꼬리가 비어 있지 않을 때 비교 한 번으로 삽입한다. 엄격한 내림차순 입력은 머리가 꼬리의 모든 값보다 커서, 꼬리를 정렬한 뒤에도 그 원소를 끝까지 비교해야 한다. 빈 리스트에서는 두 비용이 모두 0이다.

길이 5에서 오름차순 입력은 4번, 엄격한 내림차순 입력은 10번 비교한다. 웹판에서 길이를 바꾸면 두 입력과 비교 횟수가 바뀐다.
그림 9.20 입력이 오름차순이거나 엄격한 내림차순일 때의 비교 횟수

9.3.4절에서 선형 탐색의 최악 비용을 구할 때도 상한에 도달하는 입력을 구성했다. 여기서는 descendingList (n + 1)의 머리를 n으로 하고 꼬리를 descendingList n으로 둔다. 꼬리의 모든 원소가 n보다 작다는 사실은 lt_of_mem_descendingList에 적는다. 머리와 비교했을 때 조건이 항상 거짓이면 삽입 비용은 리스트 전체의 길이와 같다.

def descendingList : Nat → List Nat | 0 => [] | n + 1 => n :: descendingList n theorem descendingList_length (n : Nat) : (descendingList n).length = n := n:Nat⊢ (descendingList n).length = n induction n with ⊢ (descendingList 0).length = 0 All goals completed! 🐙 n:Natih:(descendingList n).length = n⊢ (descendingList (n + 1)).length = n + 1 All goals completed! 🐙 theorem lt_of_mem_descendingList (n y : Nat) : y ∈ descendingList n → y < n := n:Naty:Nat⊢ y ∈ descendingList n → y < n induction n with y:Nat⊢ y ∈ descendingList 0 → y < 0 All goals completed! 🐙 y:Natn:Natih:y ∈ descendingList n → y < n⊢ y ∈ descendingList (n + 1) → y < n + 1 y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)⊢ y < n + 1 y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)he:y = n⊢ y < n + 1y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)ht:y ∈ descendingList n⊢ y < n + 1 y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)he:y = n⊢ y < n + 1 y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)he:y = n⊢ n < n + 1 All goals completed! 🐙 y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)ht:y ∈ descendingList n⊢ y < n + 1 All goals completed! 🐙 theorem insertCost_eq_length_of_all_lt (x : Nat) (xs : List Nat) (h : ∀ y ∈ xs, y < x) : insertCost x xs = xs.length := x:Natxs:List Nath:∀ (y : Nat), y ∈ xs → y < x⊢ insertCost x xs = xs.length induction xs with x:Nath:∀ (y : Nat), y ∈ [] → y < x⊢ insertCost x [] = [].length All goals completed! 🐙 x:Naty:Natys:List Natih:(∀ (y : Nat), y ∈ ys → y < x) → insertCost x ys = ys.lengthh:∀ (y_1 : Nat), y_1 ∈ y :: ys → y_1 < x⊢ insertCost x (y :: ys) = (y :: ys).length x:Naty:Natys:List Natih:(∀ (y : Nat), y ∈ ys → y < x) → insertCost x ys = ys.lengthh:∀ (y_1 : Nat), y_1 ∈ y :: ys → y_1 < xhn:¬x ≤ y⊢ insertCost x (y :: ys) = (y :: ys).length x:Naty:Natys:List Natih:(∀ (y : Nat), y ∈ ys → y < x) → insertCost x ys = ys.lengthh:∀ (y_1 : Nat), y_1 ∈ y :: ys → y_1 < xhn:¬x ≤ yht:∀ (z : Nat), z ∈ ys → z < x⊢ insertCost x (y :: ys) = (y :: ys).length All goals completed! 🐙 [4, 3, 2, 1, 0]#eval descendingList 5

descendingList 5는 [4, 3, 2, 1, 0]이다. 정렬한 꼬리에도 원래 꼬리의 원소만 들어 있다. (insertionSort_perm …).mem_iff.mp로 정렬된 결과의 포함 관계를 원래 꼬리로 옮기면, 정렬 결과의 모든 원소가 여전히 n보다 작음을 알 수 있다. 마지막 삽입은 정렬한 꼬리의 길이 n만큼 비교하고, 그 비용을 귀납 가정에서 얻은 insertionSortBound n에 더한다.

theorem insertionSortCost_descending (n : Nat) : insertionSortCost (descendingList n) = insertionSortBound n := n:Nat⊢ insertionSortCost (descendingList n) = insertionSortBound n induction n with ⊢ insertionSortCost (descendingList 0) = insertionSortBound 0 All goals completed! 🐙 n:Natih:insertionSortCost (descendingList n) = insertionSortBound n⊢ insertionSortCost (descendingList (n + 1)) = insertionSortBound (n + 1) n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortCost (descendingList (n + 1)) = insertionSortBound (n + 1) All goals completed! 🐙

최악 비용의 상한과 달성. 길이 n인 모든 리스트에서 비용은 insertionSortBound n 이하이고, 그 값과 같은 비용을 갖는 길이 n의 리스트가 있다. 이 두 조건으로 insertionSortWorstCost n이 실제 최댓값임을 보인다.

def insertionSortWorstCost (n : Nat) : Nat := insertionSortBound n theorem insertionSortWorstCost_isMaximum (n : Nat) : (∀ xs : List Nat, xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost n) ∧ (∃ xs : List Nat, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n) := n:Nat⊢ (∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost n) ∧ ∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n n:Nat⊢ ∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost nn:Nat⊢ ∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n n:Nat⊢ ∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost n n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortWorstCost n n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortBound xs.length All goals completed! 🐙 n:Nat⊢ ∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n All goals completed! 🐙

그림 9.21에서 전체 상한과 내림차순 입력의 비용 등식이 만나는 곳을 찾는다. 9.6.6절에서는 이렇게 얻은 최댓값의 증가율을 구한다.