90 likes | 215 Views
CSCI 1001. overview of. computer science. ALGORITHM EFFICIENCY III. Input : a list a 1 ,…,a n set g to n , ℓ to 1 while ℓ ≤ g do if a ℓ ≠ 0 then set ℓ to ℓ+ 1 else set g to g-1, r to ℓ+ 1 while r ≤ n do set a r-1 to a r , r to r+1
E N D
CSCI 1001 overview of computer science ALGORITHM EFFICIENCY III
Input: a list a1,…,an set g to n, ℓto 1 whileℓ≤ gdo if aℓ≠ 0 then setℓto ℓ+1 else set g to g-1, r to ℓ+1 while r ≤ ndo set ar-1 to ar, r to r+1 output a1,…,ag and stop.
Input: a list a1,…,an set ℓ1…ℓn to 0, g to 0 for each i in {1,…,n} do if ai ≠ 0 then set g to g+1 set ℓgto ai output ℓ1,…,ℓg and stop.
Input: a list a1,…,an set ℓ to 1, r to n while ℓ ≤ rdo if aℓ = 0 then set aℓ to ar, r to r-1 else set ℓ to ℓ + 1 ifar = 0 then set r to r-1 output a1,…,ar and stop.
O(logn) Binary Search O(n) Linear Search Cleanup! Common Friends O(n log n) Merge Sort O(n2) Bubble, Insertion Sort Stable Marriages Viterbi Decoding O(n3) Public Key Crypto Linear Equation Solving
O(2n) Satisfiability Traveling Salesperson Optimization Problems P vs NP Is it easier to check an answer than find one?
Unsolvable Halting problem def wont_halt(s): while True: None return def will_halt(s): return The Halting Problem: given a program and its input, will the program halt?
liar_liar = ‘def liar_liar_pants_on_fire(code): def will_it_halt(code,input): … def loop_forever(): while True: None if will_it_halt(code, code): loop_forever() else: return True’ liar_liar_pants_on_fire(liar_liar)