9.6.5. 삽입 정렬의 최악 비교 횟수
상한에 도달하는 입력
그림 9.20에 길이가 같은 두 입력이 있다. 오름차순 입력은 새 값을 항상 앞에 넣으므로 정렬된 꼬리가 비어 있지 않을 때 비교 한 번으로 삽입한다. 엄격한 내림차순 입력은 머리가 꼬리의 모든 값보다 커서, 꼬리를 정렬한 뒤에도 그 원소를 끝까지 비교해야 한다. 빈 리스트에서는 두 비용이 모두 0이다.
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 succ.inl y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)he:y = n⊢ n < n + 1
exact Nat.lt_succ_self n All goals completed! 🐙
· succ.inr y:Natn:Natih:y ∈ descendingList n → y < nh:y ∈ descendingList (n + 1)ht:y ∈ descendingList n⊢ y < n + 1 exact Nat.lt_trans (ih ht) (Nat.lt_succ_self n) 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 := by x:Natxs:List Nath:∀ (y : Nat), y ∈ xs → y < x⊢ insertCost x xs = xs.length
induction xs with
| nil => nil x:Nath:∀ (y : Nat), y ∈ [] → y < x⊢ insertCost x [] = [].length rfl All goals completed! 🐙
| cons y ys ih => cons 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
have hn : ¬ x ≤ y :=
Nat.not_le_of_gt (h y List.mem_cons_self) cons 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
have ht : ∀ z ∈ ys, z < x := by x:Natxs:List Nath:∀ (y : Nat), y ∈ xs → y < x⊢ insertCost x xs = xs.length
intro z hz 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 ≤ yz:Nathz:z ∈ ys⊢ z < x
exact h z (List.mem_cons_of_mem y hz) cons 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
simp [insertCost, hn, ih ht] All goals completed! 🐙
#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 := by n:Nat⊢ insertionSortCost (descendingList n) = insertionSortBound n
induction n with
| zero => zero ⊢ insertionSortCost (descendingList 0) = insertionSortBound 0 rfl All goals completed! 🐙
| succ n ih => succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound n⊢ insertionSortCost (descendingList (n + 1)) = insertionSortBound (n + 1)
have h :
∀ y ∈ insertionSort (descendingList n), y < n := by n:Nat⊢ insertionSortCost (descendingList n) = insertionSortBound n
intro y hy n:Natih:insertionSortCost (descendingList n) = insertionSortBound ny:Nathy:y ∈ insertionSort (descendingList n)⊢ y < n
have hp := insertionSort_perm (descendingList n) n:Natih:insertionSortCost (descendingList n) = insertionSortBound ny:Nathy:y ∈ insertionSort (descendingList n)hp:(insertionSort (descendingList n)).Perm (descendingList n)⊢ y < n
have hm := hp.mem_iff.mp hy n:Natih:insertionSortCost (descendingList n) = insertionSortBound ny:Nathy:y ∈ insertionSort (descendingList n)hp:(insertionSort (descendingList n)).Perm (descendingList n)hm:y ∈ descendingList n⊢ y < n
exact lt_of_mem_descendingList n y hm succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortCost (descendingList (n + 1)) = insertionSortBound (n + 1)
rw [descendingList, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortCost (n :: descendingList n) = insertionSortBound (n + 1) insertionSortCost, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortCost (descendingList n) + insertCost n (insertionSort (descendingList n)) = insertionSortBound (n + 1) ih, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortBound n + insertCost n (insertionSort (descendingList n)) = insertionSortBound (n + 1)
insertCost_eq_length_of_all_lt n _ h, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortBound n + (insertionSort (descendingList n)).length = insertionSortBound (n + 1)
insertionSort_length, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortBound n + (descendingList n).length = insertionSortBound (n + 1) descendingList_length, succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortBound n + n = insertionSortBound (n + 1)
insertionSortBound succ n:Natih:insertionSortCost (descendingList n) = insertionSortBound nh:∀ (y : Nat), y ∈ insertionSort (descendingList n) → y < n⊢ insertionSortBound n + n = insertionSortBound n + n] 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) := by n:Nat⊢ (∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost n) ∧
∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n
constructor left n:Nat⊢ ∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost nright n:Nat⊢ ∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n
· left n:Nat⊢ ∀ (xs : List Nat), xs.length = n → insertionSortCost xs ≤ insertionSortWorstCost n intro xs h left n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortWorstCost n
rw [insertionSortWorstCost, left n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortBound n ← h left n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortBound xs.length] left n:Natxs:List Nath:xs.length = n⊢ insertionSortCost xs ≤ insertionSortBound xs.length
exact insertionSortCost_le_bound xs All goals completed! 🐙
· right n:Nat⊢ ∃ xs, xs.length = n ∧ insertionSortCost xs = insertionSortWorstCost n exact ⟨descendingList n, descendingList_length n,
insertionSortCost_descending n⟩ All goals completed! 🐙
그림 9.21에서 전체 상한과 내림차순 입력의 비용 등식이 만나는 곳을 찾는다. 9.6.6절에서는 이렇게 얻은 최댓값의 증가율을 구한다.