Download presentation
Presentation is loading. Please wait.
1
Functional Design and Programming Lecture 4: Sorting
2
Literature (Pensum) Paulson, chap. 3: Sorting (3.18, 3.19)
3
Exercises Paulson, chap. 3: 3.38-3.42 (obligatory: 3.38, 3.41)
4
Overview Sorting Insertion sort Merge sort Quick sort Algorithmic complexity analysis (time) Asymptotic notation Worst-case complexity
5
Sorting Problem: Given a list l of n numbers, return a list containing the elements of l in min-sorted order (least element first). Algorithmic approaches: incremental (insertion sort): process one element at a time divide-and-conquer (mergesort, quicksort): divide problem into smaller subproblems conquer subproblems (solve them recursively) combine solutions to subproblems
6
Insertion sort (isort) 1 2 4 4 7 8 8 9 sort 5 8 4 2 1 4 9 7 8 l (list) xs (tail of l) x (head of l) 5 insert x into (sort xs)
7
Mergesort 5 8 4 2 1 4 9 7 8 l (list) right (right half of l)left (left half of l) sort 2 4 5 81 4 7 8 9
8
Mergesort... 2 4 5 81 4 7 8 9 1 2 4 4 5 7 8 8 9 merge
9
Quicksort 5 8 4 2 1 4 9 7 8 l (list) pivot 4 2 1 48 7 9 8 5 <= 5 (without pivot!)> 5 pivot 1 2 4 47 8 8 9 sort
10
Quicksort... 5 1 2 4 47 8 8 9 1 2 4 4 5 7 8 8 9 append
11
Performance Programs require resources to execute time (execution time) space (memory) Performance of a program depends on many factors: algorithm and data structures used compiler, host machine, implementation tuning, etc.
12
Algorithmic Complexity Goal: We would like measure the performance quality of algorithms independent of implementation particulars Measures: time (execution time): time complexity space (memory): space complexity
13
Asymptotic analysis Performance depends on inputs: larger inputs require more time and space Asymptotic complexity: measure scalability: how much more time and space do we need to process yet bigger inputs? worst-case analysis: Worst case for inputs of certain size average-case analysis: Average case... best-case analysis: Best possible case...
14
Constant time operations: Definition Operations that take (at a maximum) a fixed (“constant”) number of machine instructions to implement, independent of the size of the data they are operating on.
15
Constant-time operations: Examples Looking up the value of an identifier (including the definition of a function) Binding a value to an identifier: val x = 5 Executing a primitive operation on fixed-size data (bool, int, real, char, but not string, list) Applying a constructor; e.g. nil or :: for lists. Performing a pattern match against patterns defined in a function definition or case expression
16
Constant-time operations: Examples... Passing an argument value to a function (independent of the size of the argument value!) Returning the result from a function (indepdendent of the size of the argument value!)
17
Asymptotic notation (“Big-Oh notation”) Let f, g functions from Nat to Nat (nonnegative integers). We write f(n) = O(g(n)): if there exist positive constants c, n 0 such that f(n) = n 0. We write f(n) = (g(n)): if there exist positive constants c 1, c 2, n 0 s.t. c 1 g(n) = n 0.
18
(n log n) log n n
19
(n 2 ) n... n
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.