145
LECTURE 11 SEPTEMBER 10, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Programming in Haskell Aug-Nov 2015

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)