Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming...
Transcript of Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming...
![Page 1: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/1.jpg)
LECTURE 11
SEPTEMBER 10, 2015
S P SURESH CHENNAI MATHEMATICAL INSTITUTE
Programming in Haskell Aug-Nov 2015
![Page 2: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/2.jpg)
Measuring efficiency
![Page 3: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/3.jpg)
Measuring efficiency
Computation is reduction
Application of definitions as rewriting rules
![Page 4: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/4.jpg)
Measuring efficiency
Computation is reduction
Application of definitions as rewriting rules
Count the number of reduction steps
Running time is T(n) for input size n
![Page 5: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/5.jpg)
Example: Complexity of ++
![Page 6: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/6.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
![Page 7: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/7.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
![Page 8: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/8.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
1:([2,3] ++ [4,5,6]) ➾
![Page 9: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/9.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
1:([2,3] ++ [4,5,6]) ➾
1:(2:([3] ++ [4,5,6])) ➾
![Page 10: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/10.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
1:([2,3] ++ [4,5,6]) ➾
1:(2:([3] ++ [4,5,6])) ➾
1:(2:(3:([] ++ [4,5,6]))) ➾
![Page 11: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/11.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
1:([2,3] ++ [4,5,6]) ➾
1:(2:([3] ++ [4,5,6])) ➾
1:(2:(3:([] ++ [4,5,6]))) ➾
1:(2:(3:([4,5,6])))
![Page 12: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/12.jpg)
Example: Complexity of ++
[] ++ y = y (x:xs) ++ y = x:(xs++y)
[1,2,3] ++ [4,5,6] ➾
1:([2,3] ++ [4,5,6]) ➾
1:(2:([3] ++ [4,5,6])) ➾
1:(2:(3:([] ++ [4,5,6]))) ➾
1:(2:(3:([4,5,6])))
l1 ++ l2 : use the second rule length l1 times, first rule once, always
![Page 13: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/13.jpg)
Example: elem
![Page 14: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/14.jpg)
Example: elem
elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) | (i==x) = True | otherwise = elem i xs
![Page 15: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/15.jpg)
Example: elem
elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) | (i==x) = True | otherwise = elem i xs
elem 3 [4,7,8,9] ➾ elem 3 [7,8,9] ➾elem 3 [8,9] ➾ elem 3 [9] ➾ elem 3 [] ➾ False
![Page 16: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/16.jpg)
Example: elem
elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) | (i==x) = True | otherwise = elem i xs
elem 3 [4,7,8,9] ➾ elem 3 [7,8,9] ➾elem 3 [8,9] ➾ elem 3 [9] ➾ elem 3 [] ➾ False
elem 3 [3,7,8,9] ➾ True
![Page 17: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/17.jpg)
Example: elem
elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) | (i==x) = True | otherwise = elem i xs
elem 3 [4,7,8,9] ➾ elem 3 [7,8,9] ➾elem 3 [8,9] ➾ elem 3 [9] ➾ elem 3 [] ➾ False
elem 3 [3,7,8,9] ➾ True
Complexity depends on input size and value
![Page 18: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/18.jpg)
Variation across inputs
Worst case complexity
Maximum running time over all inputs of size n
Pessimistic: may be rare
Average case
More realistic, but difficult/impossible to compute
![Page 19: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/19.jpg)
Asymptotic complexity
![Page 20: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/20.jpg)
Asymptotic complexity
Interested in T(n) in terms of orders of magnitude
![Page 21: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/21.jpg)
Asymptotic complexity
Interested in T(n) in terms of orders of magnitude
f(n) = O(g(n)) if there is a constant k such thatf(n) ≤ k g(n) for all n > 0
![Page 22: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/22.jpg)
Asymptotic complexity
Interested in T(n) in terms of orders of magnitude
f(n) = O(g(n)) if there is a constant k such thatf(n) ≤ k g(n) for all n > 0
an2 + bn + c = O(n2) for all a,b,c(take k = a+b+c if a,b,c > 0)
![Page 23: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/23.jpg)
Asymptotic complexity
Interested in T(n) in terms of orders of magnitude
f(n) = O(g(n)) if there is a constant k such thatf(n) ≤ k g(n) for all n > 0
an2 + bn + c = O(n2) for all a,b,c(take k = a+b+c if a,b,c > 0)
Ignore constant factors, lower order terms
![Page 24: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/24.jpg)
Asymptotic complexity
Interested in T(n) in terms of orders of magnitude
f(n) = O(g(n)) if there is a constant k such thatf(n) ≤ k g(n) for all n > 0
an2 + bn + c = O(n2) for all a,b,c(take k = a+b+c if a,b,c > 0)
Ignore constant factors, lower order terms
O(n), O(n log n), O(nk), O(2n), …
![Page 25: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/25.jpg)
Asymptotic complexity …
Complexity of ++ is O(n), where n is the length of the first list
Complexity of elem is O(n)
Worst case!
![Page 26: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/26.jpg)
Complexity of reverse
![Page 27: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/27.jpg)
Complexity of reverse
myreverse :: [a] -> [a] myreverse [] = [] myreverse (x:xs) = (myreverse xs) ++ [x]
![Page 28: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/28.jpg)
Complexity of reverse
myreverse :: [a] -> [a] myreverse [] = [] myreverse (x:xs) = (myreverse xs) ++ [x]
Analyze directly (like ++), or write a recurrence for T(n)
![Page 29: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/29.jpg)
Complexity of reverse
myreverse :: [a] -> [a] myreverse [] = [] myreverse (x:xs) = (myreverse xs) ++ [x]
Analyze directly (like ++), or write a recurrence for T(n)
T(0) = 1 T(n) = T(n-1) + n
![Page 30: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/30.jpg)
Complexity of reverse
myreverse :: [a] -> [a] myreverse [] = [] myreverse (x:xs) = (myreverse xs) ++ [x]
Analyze directly (like ++), or write a recurrence for T(n)
T(0) = 1 T(n) = T(n-1) + n
Solve by expanding the recurrence
![Page 31: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/31.jpg)
Complexity of reverse …
T(0) = 1 T(n) = T(n-1) + n
![Page 32: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/32.jpg)
Complexity of reverse …
T(n) = T(n-1) + n T(0) = 1 T(n) = T(n-1) + n
![Page 33: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/33.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
T(0) = 1 T(n) = T(n-1) + n
![Page 34: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/34.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
= (T(n-3) + n-2) + n-1 + n
T(0) = 1 T(n) = T(n-1) + n
![Page 35: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/35.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
= (T(n-3) + n-2) + n-1 + n
...
T(0) = 1 T(n) = T(n-1) + n
![Page 36: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/36.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
= (T(n-3) + n-2) + n-1 + n
...
= T(0) + 1 + 2 + ... + n
T(0) = 1 T(n) = T(n-1) + n
![Page 37: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/37.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
= (T(n-3) + n-2) + n-1 + n
...
= T(0) + 1 + 2 + ... + n
= 1 + 1 + 2 + ... + n = 1 + n(n+1)/2
T(0) = 1 T(n) = T(n-1) + n
![Page 38: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/38.jpg)
Complexity of reverse …
T(n) = T(n-1) + n
= (T(n-2) + n-1) + n
= (T(n-3) + n-2) + n-1 + n
...
= T(0) + 1 + 2 + ... + n
= 1 + 1 + 2 + ... + n = 1 + n(n+1)/2
= O(n2)
T(0) = 1 T(n) = T(n-1) + n
![Page 39: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/39.jpg)
Speeding up reverse
![Page 40: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/40.jpg)
Speeding up reverse
Can we do better?
![Page 41: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/41.jpg)
Speeding up reverse
Can we do better?
Imagine we are reversing a stack of heavy stack of books
![Page 42: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/42.jpg)
Speeding up reverse
Can we do better?
Imagine we are reversing a stack of heavy stack of books
Transfer to a new stack, top to bottom
![Page 43: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/43.jpg)
Speeding up reverse
Can we do better?
Imagine we are reversing a stack of heavy stack of books
Transfer to a new stack, top to bottom
New stack is in reverse order!
![Page 44: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/44.jpg)
Speeding up reverse …
![Page 45: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/45.jpg)
Speeding up reverse …
transfer :: [a] -> [a] -> [a] transfer [] l = l transfer (x:xs) l = transfer xs (x:l)
![Page 46: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/46.jpg)
Speeding up reverse …
transfer :: [a] -> [a] -> [a] transfer [] l = l transfer (x:xs) l = transfer xs (x:l)
Input size for transfer l1 l2 is length l1
![Page 47: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/47.jpg)
Speeding up reverse …
transfer :: [a] -> [a] -> [a] transfer [] l = l transfer (x:xs) l = transfer xs (x:l)
Input size for transfer l1 l2 is length l1
Recurrence
![Page 48: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/48.jpg)
Speeding up reverse …
transfer :: [a] -> [a] -> [a] transfer [] l = l transfer (x:xs) l = transfer xs (x:l)
Input size for transfer l1 l2 is length l1
Recurrence
T(0) = 1 T(n) = T(n-1) + 1
![Page 49: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/49.jpg)
Speeding up reverse …
transfer :: [a] -> [a] -> [a] transfer [] l = l transfer (x:xs) l = transfer xs (x:l)
Input size for transfer l1 l2 is length l1
Recurrence
T(0) = 1 T(n) = T(n-1) + 1
Expanding: T(n) = 1 + 1 + … + 1 = O(n)
![Page 50: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/50.jpg)
Speeding up reverse …
fastreverse :: [a] -> [a] fastreverse l = transfer l []
Complexity is O(n)
Need to understand the computational model to achieve efficiency
![Page 51: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/51.jpg)
Summary
Measure complexity in Haskell in terms of reduction steps
Account for input size and values
Usually worst-case complexity
Asymptotic complexity
Ignore constants, lower order terms
T(n) = O(f(n))
![Page 52: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/52.jpg)
Sorting
![Page 53: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/53.jpg)
Sorting
Goal is to arrange a list in ascending order
![Page 54: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/54.jpg)
Sorting
Goal is to arrange a list in ascending order
How would we start a pack of cards?
![Page 55: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/55.jpg)
Sorting
Goal is to arrange a list in ascending order
How would we start a pack of cards?
A single card is sorted
![Page 56: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/56.jpg)
Sorting
Goal is to arrange a list in ascending order
How would we start a pack of cards?
A single card is sorted
Put second card before/after first
![Page 57: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/57.jpg)
Sorting
Goal is to arrange a list in ascending order
How would we start a pack of cards?
A single card is sorted
Put second card before/after first
“Insert” third, fourth,… card in correct place
![Page 58: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/58.jpg)
Sorting
Goal is to arrange a list in ascending order
How would we start a pack of cards?
A single card is sorted
Put second card before/after first
“Insert” third, fourth,… card in correct place
Insertion sort
![Page 59: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/59.jpg)
Insertion sort : insert
![Page 60: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/60.jpg)
Insertion sort : insert
Insert an element in a sorted list
![Page 61: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/61.jpg)
Insertion sort : insert
Insert an element in a sorted list
insert :: Int -> [Int] -> [Int] insert x [] = [x] insert x (y:ys) | (x <= y) = x:y:ys | otherwise y:(insert x ys)
![Page 62: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/62.jpg)
Insertion sort : insert
Insert an element in a sorted list
insert :: Int -> [Int] -> [Int] insert x [] = [x] insert x (y:ys) | (x <= y) = x:y:ys | otherwise y:(insert x ys)
Clearly T(n) = O(n)
![Page 63: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/63.jpg)
Insertion sort : isort
![Page 64: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/64.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
![Page 65: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/65.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
Alternatively
![Page 66: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/66.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
Alternatively
isort = foldr insert []
![Page 67: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/67.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
Alternatively
isort = foldr insert []
Recurrence
![Page 68: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/68.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
Alternatively
isort = foldr insert []
Recurrence
T(0) = 1 T(n) = T(n-1) + O(n)
![Page 69: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/69.jpg)
Insertion sort : isortisort :: [Int] -> [Int] isort [] = [] isort (x:xs) = insert x (isort xs)
Alternatively
isort = foldr insert []
Recurrence
T(0) = 1 T(n) = T(n-1) + O(n)
Complexity: T(n) = O(n2)
![Page 70: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/70.jpg)
A better strategy?
Divide list in two equal parts
Separately sort left and right half
Combine the two sorted halves to get the full list sorted
![Page 71: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/71.jpg)
Combining sorted lists
Given two sorted lists l1 and l2, combine into a sorted list l3
Compare first element of l1 and l2
Move it into l3
Repeat until all elements in l1 and l2 are over
Merging l1 and l2
![Page 72: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/72.jpg)
Merging two sorted lists
32 74 89
21 55 64
![Page 73: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/73.jpg)
Merging two sorted lists
32 74 89
21 55 64
21
![Page 74: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/74.jpg)
Merging two sorted lists
32 74 89
21 55 64
21 32
![Page 75: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/75.jpg)
Merging two sorted lists
32 74 89
21 55 64
21 32 55
![Page 76: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/76.jpg)
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64
![Page 77: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/77.jpg)
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74
![Page 78: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/78.jpg)
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74 89
![Page 79: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/79.jpg)
Merge Sort
![Page 80: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/80.jpg)
Merge Sort
Sort l!!0 to l!!(n/2-1)
![Page 81: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/81.jpg)
Merge Sort
Sort l!!0 to l!!(n/2-1)
Sort l!!(n/2) to l!!(n-1)
![Page 82: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/82.jpg)
Merge Sort
Sort l!!0 to l!!(n/2-1)
Sort l!!(n/2) to l!!(n-1)
Merge sorted halves into l'
![Page 83: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/83.jpg)
Merge Sort
Sort l!!0 to l!!(n/2-1)
Sort l!!(n/2) to l!!(n-1)
Merge sorted halves into l'
How do we sort the halves?
![Page 84: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/84.jpg)
Merge Sort
Sort l!!0 to l!!(n/2-1)
Sort l!!(n/2) to l!!(n-1)
Merge sorted halves into l'
How do we sort the halves?
Recursively, using the same strategy!
![Page 85: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/85.jpg)
Merge Sort
43 32 22 78 63 57 91 13
![Page 86: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/86.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78
![Page 87: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/87.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
![Page 88: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/88.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
![Page 89: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/89.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
![Page 90: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/90.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32
![Page 91: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/91.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
![Page 92: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/92.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57
![Page 93: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/93.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
![Page 94: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/94.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43
![Page 95: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/95.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78
![Page 96: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/96.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63
![Page 97: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/97.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
![Page 98: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/98.jpg)
Merge Sort
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78
![Page 99: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/99.jpg)
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
![Page 100: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/100.jpg)
Merge Sort
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
13 22 32 43 57 63 78 91
![Page 101: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/101.jpg)
Merge sort : merge
![Page 102: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/102.jpg)
Merge sort : merge
merge :: [Int] -> [Int] -> [Int} merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) | x <= y = x:(merge xs (y:ys)) | otherwise = y:(merge (x:xs) ys)
![Page 103: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/103.jpg)
Merge sort : merge
merge :: [Int] -> [Int] -> [Int} merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) | x <= y = x:(merge xs (y:ys)) | otherwise = y:(merge (x:xs) ys)
Each comparison adds one element to output
![Page 104: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/104.jpg)
Merge sort : merge
merge :: [Int] -> [Int] -> [Int} merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) | x <= y = x:(merge xs (y:ys)) | otherwise = y:(merge (x:xs) ys)
Each comparison adds one element to output
T(n) = O(n), where n is sum of lengths of input lists
![Page 105: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/105.jpg)
Merge sort
mergesort :: [Int] -> [Int] mergesort [] = [] mergesort [x] = [x] mergesort l = merge (mergesort (front l)) (mergesort (back l)) where front l = take ((length l) `div` 2) l back l = drop ((length l) `div` 2) l
![Page 106: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/106.jpg)
Analysis of Merge Sort
T(n): time taken by Merge Sort on input of size n
Assume, for simplicity, that n = 2k
T(n) = 2T(n/2) + 2n
Two subproblems of size n/2
Splitting the list into front and back takes n steps
Merging solutions requires time O(n/2+n/2) = O(n)
Solve the recurrence by unwinding
![Page 107: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/107.jpg)
Analysis of Merge Sort …
![Page 108: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/108.jpg)
Analysis of Merge Sort …T(1) = 1
![Page 109: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/109.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
![Page 110: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/110.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
= 2 [ 2T(n/4) + n] + 2n = 22 T(n/22) + 4n
![Page 111: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/111.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
= 2 [ 2T(n/4) + n] + 2n = 22 T(n/22) + 4n
= 22 [ 2T(n/23) + 2n/22] + 4n = 23 T(n/23) + 6n …
![Page 112: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/112.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
= 2 [ 2T(n/4) + n] + 2n = 22 T(n/22) + 4n
= 22 [ 2T(n/23) + 2n/22] + 4n = 23 T(n/23) + 6n …
= 2j T(n/2j) + 2jn
![Page 113: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/113.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
= 2 [ 2T(n/4) + n] + 2n = 22 T(n/22) + 4n
= 22 [ 2T(n/23) + 2n/22] + 4n = 23 T(n/23) + 6n …
= 2j T(n/2j) + 2jn
When j = log n, n/2j = 1, so T(n/2j) = 1
![Page 114: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/114.jpg)
Analysis of Merge Sort …T(1) = 1
T(n) = 2T(n/2) + 2n
= 2 [ 2T(n/4) + n] + 2n = 22 T(n/22) + 4n
= 22 [ 2T(n/23) + 2n/22] + 4n = 23 T(n/23) + 6n …
= 2j T(n/2j) + 2jn
When j = log n, n/2j = 1, so T(n/2j) = 1
T(n) = 2j T(n/2j) + 2jn = 2log n + 2(log n) n =n + 2n log n = O(n log n)
![Page 115: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/115.jpg)
Avoid merging
![Page 116: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/116.jpg)
Avoid mergingSome elements in left half move right and vice versa
![Page 117: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/117.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
![Page 118: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/118.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
Suppose the median value in list is m
![Page 119: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/119.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
Suppose the median value in list is m
Move all values ≤ m to left half of list
![Page 120: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/120.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
Suppose the median value in list is m
Move all values ≤ m to left half of list
Right half has values > m
![Page 121: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/121.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
Suppose the median value in list is m
Move all values ≤ m to left half of list
Right half has values > m
Recursively sort left and right halves
![Page 122: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/122.jpg)
Avoid mergingSome elements in left half move right and vice versa
Can we ensure that everything to the left is smaller than everything to the right?
Suppose the median value in list is m
Move all values ≤ m to left half of list
Right half has values > m
Recursively sort left and right halves
List is now sorted! No need to merge
![Page 123: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/123.jpg)
Avoid merging …
![Page 124: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/124.jpg)
Avoid merging …
How do we find the median?
![Page 125: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/125.jpg)
Avoid merging …
How do we find the median?
Sort and pick up middle element
![Page 126: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/126.jpg)
Avoid merging …
How do we find the median?
Sort and pick up middle element
But our aim is to sort!
![Page 127: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/127.jpg)
Avoid merging …
How do we find the median?
Sort and pick up middle element
But our aim is to sort!
Instead, pick up some value in list — pivot
![Page 128: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/128.jpg)
Avoid merging …
How do we find the median?
Sort and pick up middle element
But our aim is to sort!
Instead, pick up some value in list — pivot
Split list with respect to this pivot element
![Page 129: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/129.jpg)
Quicksort
![Page 130: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/130.jpg)
Quicksort
Choose a pivot element
![Page 131: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/131.jpg)
Quicksort
Choose a pivot element
Typically the first value in the list
![Page 132: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/132.jpg)
Quicksort
Choose a pivot element
Typically the first value in the list
Partition list into lower and upper parts with respect to pivot
![Page 133: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/133.jpg)
Quicksort
Choose a pivot element
Typically the first value in the list
Partition list into lower and upper parts with respect to pivot
Move pivot between lower and upper partition
![Page 134: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/134.jpg)
Quicksort
Choose a pivot element
Typically the first value in the list
Partition list into lower and upper parts with respect to pivot
Move pivot between lower and upper partition
Recursively sort the two partitions
![Page 135: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/135.jpg)
Quicksort
High level view
![Page 136: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/136.jpg)
Quicksort
High level view
43 32 22 78 63 57 91 13
![Page 137: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/137.jpg)
Quicksort
High level view
43 32 22 78 63 57 91 13
![Page 138: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/138.jpg)
Quicksort
High level view
43 32 22 78 63 57 91 13
![Page 139: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/139.jpg)
Quicksort
High level view
13 32 22 43 63 57 91 78
![Page 140: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/140.jpg)
Quicksort
High level view
13 22 32 43 57 63 78 91
![Page 141: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/141.jpg)
Quicksort
quicksort :: [Int] -> [Int] quicksort [] = [] quicksort (x:xs) = (quicksort lower) ++ [splitter] ++ (quicksort upper) where splitter = x lower = [ y | y <- xs, y <= x ] upper = [ y | y <- xs, y > x ]
![Page 142: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/142.jpg)
Analysis of Quicksort
Worst case
Pivot is maximum or minimum
One partition is empty
Other is size n-1
T(n) = T(n-1) + n = T(n-2) + (n-1) + n = … = 1 + 2 + … + n = O(n2)
Already sorted array is worst case input!
![Page 143: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/143.jpg)
Analysis of Quicksort
But …
Average case is O(n log n)
Sorting is a rare example where average case can be computed
What does average case mean?
![Page 144: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/144.jpg)
Quicksort: Average case
Assume input is a permutation of {1,2,…,n}
Actual values not important
Only relative order matters
Each input is equally likely (uniform probability)
Calculate running time across all inputs
Expected running time can be shown O(n log n)
![Page 145: Programming in Haskell Aug-Nov 2015spsuresh/teaching/prgh15/lectures/lecture11.pdf · Programming in Haskell Aug-Nov 2015. Measuring efficiency. ... Goal is to arrange a list in ascending](https://reader031.fdocuments.in/reader031/viewer/2022011801/5b0411a17f8b9a41528bedfc/html5/thumbnails/145.jpg)
Summary
Sorting is an important starting point for many functions on lists
Insertion sort is a natural inductive sort whose complexity is O(n2)
Merge sort has complexity O(n log n)
Quicksort has worst-case complexity O(n2) but average-case complexity O(n log n)