correctness of BestTwoSum
Let
result := empty bag
i, j := 0, N-1
while i < j do
if L[i] + L[j] = w then
add (L[i], L[j]) to result
i,j := i+1,j-1
else if L[i] + L[j] < w then
i := i+1
else
j := j-1
return result /* result = TS(L, 0, N-1) */
selection sort.
Input: L[0...N) of N values
For pos := 0 to N-2 do
min := pos
For i := pos+1 to N-1 do
if L[i] < L[min] then
min := i
swap L[pos] and L[min]
Comparison: , changes
insertion sort.
Input: L[0...N) of N values
For pos := 1 to N-1 do
v := L[pos]
p := pos
while p > 0 and v< L[p-1] do
L[p] := L[p-1]
p := p-1
L[p] := v
Comparison: , changes

merge sort.
- divide-and-conquer
A lower bound for general-purpose sorting
assume we have a list of of distinct values
: All possible lists that are treated the same by A such that
Question
Can we improve mergesort O(N) memory?
quick sort.
Complexity of quicksort
recursion tree: