Sorting
correctness of BestTwoSum
Let TS(start, end)=(L[i],L[j])∣(L[i]+L[j]=w)∧(start≤i≤j≤end)
selection sort.
Comparison: ∑pos=0N−2(N−1−pos)=Θ(N2), changes 2(N−1)=Θ(N)
insertion sort.
Comparison: ≤pos=∑pos=1N−1pos=2N(N−1), changes ≤pos=∑pos=1N−1(1+pos)=2N(N−1)+N−1
merge sort.
A lower bound for general-purpose sorting
assume we have a list of L[0…N) of N distinct values
S: All possible lists L that are treated the same by A such that C:L[i]<L[j]
Can we improve mergesort O(N) memory?
quick sort.
Complexity of quicksort
T(N)={1 T(N−1)+Nif N≤1;if N>1
recursion tree: