Introduction to Programming: Lecture 6

Similar documents
Programming in Haskell Aug-Nov 2015

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Shell CSCE 314 TAMU. Functions continued

CSC 273 Data Structures

Sorting. Weiss chapter , 8.6

Functional Programming in Haskell Part I : Basics

Faster Sorting Methods

COSC242 Lecture 7 Mergesort and Quicksort

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS 171: Introduction to Computer Science II. Quicksort

Lecture 8: Mergesort / Quicksort Steven Skiena

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Lecture 6: Sequential Sorting

Mergesort again. 1. Split the list into two equal parts

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Reading for this lecture (Goodrich and Tamassia):

Better sorting algorithms (Weiss chapter )

CS240 Fall Mike Lam, Professor. Quick Sort

1 The smallest free number

Searching in General

Sorting and Searching

Sorting Algorithms. + Analysis of the Sorting Algorithms

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Real-world sorting (not on exam)

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Part 20. Sorting and Selection

Unit-2 Divide and conquer 2016

Quicksort (Weiss chapter 8.6)

Programming Languages 3. Definition and Proof by Induction

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Programming Language Concepts: Lecture 14

DATA STRUCTURES AND ALGORITHMS

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

CSci 450: Org. of Programming Languages Overloading and Type Classes

A general introduction to Functional Programming using Haskell

Lecture 19: Functions, Types and Data Structures in Haskell

Programming Languages Fall 2013

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Programming II (CS300)

CSC Design and Analysis of Algorithms

CS 171: Introduction to Computer Science II. Quicksort

11. Divide and Conquer

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Algorithmic Analysis and Sorting, Part Two

CS126 Final Exam Review

L14 Quicksort and Performance Optimization

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

We can use a max-heap to sort data.

Lecture 2: List algorithms using recursion and list comprehensions

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Quick Sort. CSE Data Structures May 15, 2002

Divide and Conquer Algorithms

Introduction to Programming: Lecture 10

Lecture 4: Higher Order Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 Programming Languages

Divide and Conquer 4-0

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Randomized Algorithms: Selection

Divide-and-Conquer. Dr. Yingwu Zhu

Mergesort again. 1. Split the list into two equal parts

Data Structures and Algorithms

BM267 - Introduction to Data Structures

Shell CSCE 314 TAMU. Higher Order Functions

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Divide and Conquer Sorting Algorithms and Noncomparison-based

QuickSort

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Divide and Conquer. Algorithm D-and-C(n: input size)

1. (34 points) Suppose that the following definitions are made: (a) (22 points) Give the values of the following expressions.

Exercise 1 ( = 24 points)

CS61BL. Lecture 5: Graphs Sorting

Searching and Sorting

Merge- and Quick Sort

Lecture 19 Sorting Goodrich, Tamassia

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Sorting Pearson Education, Inc. All rights reserved.

Lecture Notes on Quicksort

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

Introduction to Programming, Aug-Dec 2006

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

Foundations of Computation

COMP 250 Fall recurrences 2 Oct. 13, 2017

An introduction introduction to functional functional programming programming using usin Haskell

CS1 Lecture 30 Apr. 2, 2018

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Transcription:

Introduction to Programming: Lecture 6 K Narayan Kumar Chennai Mathematical Institute http://www.cmi.ac.in/~kumar 28 August 2012

Example: initial segments Write a Haskell function initsegs which returns the list of initial segments of a list. initsegs [1,2,3] = [[],[1],[1,2],[1,2,3]] initsegs [] = [[]]

Example: initial segments Write a Haskell function initsegs which returns the list of initial segments of a list. initsegs [1,2,3] = [[],[1],[1,2],[1,2,3]] initsegs [] = [[]] initsegs [] = [[]] initsegs (x:xs) = [] : map (x:) (initsegs xs)

Example: interleave interleave x l inserts x into all possible positions in the list l interleave 3 [] = [[3]] interleave 3 [2,3] = [[3,2,3],[2,3,3],[2,3,3]] interleave a "abcd" = ["aabcd","aabcd","abacd","abcad","abcda"]

Example: interleave interleave x l inserts x into all possible positions in the list l interleave 3 [] = [[3]] interleave 3 [2,3] = [[3,2,3],[2,3,3],[2,3,3]] interleave a "abcd" = ["aabcd","aabcd","abacd","abcad","abcda"] interleave x [] = [[x]] interleave x (y:ys) = (x:y:ys) : map (y:) (interleave x ys)

Example: Permutations Write down function that computes all the permutations of a given list.

Example: Permutations Write down function that computes all the permutations of a given list. Notice that this problem does not have a unique answer. We are happy with a listing of all the permutations in any order.

Example: Permutations Write down function that computes all the permutations of a given list. Notice that this problem does not have a unique answer. We are happy with a listing of all the permutations in any order. perm [1,2,3] = [[1,2,3],[2,1,3],[2,3,1], [1,3,2],[3,1,2],[3,2,1]]

Example: Permutations Write down function that computes all the permutations of a given list. Notice that this problem does not have a unique answer. We are happy with a listing of all the permutations in any order. perm [1,2,3] = [[1,2,3],[2,1,3],[2,3,1], [1,3,2],[3,1,2],[3,2,1]] perm [x] = [[x]] perm (x:xs) = concat (map (interleave x) (perm xs))

Example: Permutations Write down function that computes all the permutations of a given list. Notice that this problem does not have a unique answer. We are happy with a listing of all the permutations in any order. perm [1,2,3] = [[1,2,3],[2,1,3],[2,3,1], [1,3,2],[3,1,2],[3,2,1]] perm [x] = [[x]] perm (x:xs) = concat (map (interleave x) (perm xs)) The combination concat (map f l) appears so often that there is a function concatmap which does precisely this: concatmap f l = concat (map f l)

Example: Permutations Write down function that computes all the permutations of a given list. Notice that this problem does not have a unique answer. We are happy with a listing of all the permutations in any order. perm [1,2,3] = [[1,2,3],[2,1,3],[2,3,1], [1,3,2],[3,1,2],[3,2,1]] perm [x] = [[x]] perm (x:xs) = concat (map (interleave x) (perm xs)) The combination concat (map f l) appears so often that there is a function concatmap which does precisely this: concatmap f l = concat (map f l) Exercise: What is the type of concatmap

Example: Partitions Given a list l is a collection of nonempty lists l1, l2,..., lk such that l = l1 ++ l2 ++... ++ lk

Example: Partitions Given a list l is a collection of nonempty lists l1, l2,..., lk such that l = l1 ++ l2 ++... ++ lk part [1,2,3] = [[[1],[2],[3]],[[1,2],[3]], [[1],[2,3]],[[1,2,3]]]

Example: Partitions Given a list l is a collection of nonempty lists l1, l2,..., lk such that l = l1 ++ l2 ++... ++ lk part [1,2,3] = [[[1],[2],[3]],[[1,2],[3]], [[1],[2,3]],[[1,2,3]]] The type of part is part :: [a] -> [[[a]]]

Example: Partitions Given a list l is a collection of nonempty lists l1, l2,..., lk such that l = l1 ++ l2 ++... ++ lk part [1,2,3] = [[[1],[2],[3]],[[1,2],[3]], [[1],[2,3]],[[1,2,3]]] The type of part is part :: [a] -> [[[a]]] part [x] = [[[x]]] part (x:xs) = map ([x]:) (part xs) ++ map (f x) (part xs) where f x (y:ys) = (x:y):ys

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order.

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this remove the first element to get a shorter list sort this shorter list insert the first element in the correct position in the sorted list

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this remove the first element to get a shorter list sort this shorter list insert the first element in the correct position in the sorted list sort [2,1,3,2,4]

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this remove the first element to get a shorter list sort this shorter list insert the first element in the correct position in the sorted list sort [2,1,3,2,4] insert 2 in sort [1,3,2,4]

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this remove the first element to get a shorter list sort this shorter list insert the first element in the correct position in the sorted list sort [2,1,3,2,4] insert 2 in sort [1,3,2,4] insert 2 in [1,2,3,4]

Sorting: Insertion sorting Given a list of integers rearrange it in ascending order. Here is one way to do this remove the first element to get a shorter list sort this shorter list insert the first element in the correct position in the sorted list sort [2,1,3,2,4] insert 2 in sort [1,3,2,4] insert 2 in [1,2,3,4] [1,2,2,3,4]

Insertion Sorting How to insert a number in the correct position in a sorted list?

Insertion Sorting How to insert a number in the correct position in a sorted list? insert 3 [1,2,2,4] = [1,2,2,3,4] insert 3 [] = [3]

Insertion Sorting How to insert a number in the correct position in a sorted list? insert 3 [1,2,2,4] = [1,2,2,3,4] insert 3 [] = [3] insert Int -> [Int] -> [Int] insert x [] = [x] insert x (y:ys) (x <= y) = x:y:ys otherwise = y : insert x ys

Insertion Sorting How to insert a number in the correct position in a sorted list? insert 3 [1,2,2,4] = [1,2,2,3,4] insert 3 [] = [3] insert Int -> [Int] -> [Int] insert x [] = [x] insert x (y:ys) (x <= y) = x:y:ys otherwise = y : insert x ys Note that insert is not polymorphic, in the sense that we have seen so far, as <= is not necessarily defined on all types.

Insertion Sorting Define isort :: [Int] -> [Int] using insert

Insertion Sorting Define isort :: [Int] -> [Int] using insert isort [] = [] isort (x:xs) = insert x (isort xs)

Insertion Sorting Define isort :: [Int] -> [Int] using insert isort [] = [] isort (x:xs) = insert x (isort xs) or more succinctly isort = foldr insert []

Sorting: Merge Sorting Here is another method to sort a list. Divide the list into two halves. Sort each recursively using this algorithm. Merge together the two sorted lists into a single sorted list.

Merging two sorted lists 5 16 83 99 33 55 85 93

Merging two sorted lists 5 16 83 99 33 55 85 93

Merging two sorted lists 5 16 83 99 33 55 85 93 5

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33 55

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33 55 83

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33 55 83 85

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33 55 83 85 93

Merging two sorted lists 5 16 83 99 33 55 85 93 5 16 33 55 83 85 93 99

Mergesort 99 5 83 16 85 33 93 55

Mergesort 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55

Mergesort 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55

Mergesort 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55

Mergesort 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55 5 99 16 83 33 85 55 93 99 5 83 16 85 33 93 55

Mergesort 99 5 83 16 85 33 93 55 99 5 83 16 85 33 93 55 5 99 16 83 33 85 55 93

Mergesort 99 5 83 16 85 33 93 55 5 16 83 99 33 55 85 93 5 99 16 83 33 85 55 93

Mergesort 99 5 83 16 85 33 93 55 5 16 83 99 33 55 85 93

Mergesort 5 16 33 55 83 85 93 99 5 16 83 99 33 55 85 93

Mergesort 5 16 33 55 83 85 93 99

Merging in Haskell merge to merge two sorted lists of integers.

Merging in Haskell merge to merge two sorted lists of integers. merge :: [Int] -> [Int] -> [Int] merge [] l = l merge l [] = l merge (x:xs) (y:ys) (x < y) = x : merge xs (y:ys) otherwise = y : merge (x:xs) ys

Merging in Haskell merge to merge two sorted lists of integers. merge :: [Int] -> [Int] -> [Int] merge [] l = l merge l [] = l merge (x:xs) (y:ys) (x < y) = x : merge xs (y:ys) otherwise = y : merge (x:xs) ys Once again, merge is not polymorphic, in the sense that we have seen so far, as it uses <.

Mergesort in Haskell Using merge to write down a mergesort

Mergesort in Haskell Using merge to write down a mergesort mergesort :: [Int] -> [Int] mergesort [] = [] mergesort [x] = [x] mergesort l = merge (mergesort fhalf) (mergesort shalf) where fhalf = take n l shalf = drop n l n = div (length l) 2

Mergesort in Haskell Using merge to write down a mergesort mergesort :: [Int] -> [Int] mergesort [] = [] mergesort [x] = [x] mergesort l = merge (mergesort fhalf) (mergesort shalf) where fhalf = take n l shalf = drop n l n = div (length l) 2

Sorting: Quicksort Suppose we can find the median (middle element in the sorted order) in a list

Sorting: Quicksort Suppose we can find the median (middle element in the sorted order) in a list Collect all values less than median and sort

Sorting: Quicksort Suppose we can find the median (middle element in the sorted order) in a list Collect all values less than median and sort Collect all values more than median and sort

Sorting: Quicksort Suppose we can find the median (middle element in the sorted order) in a list Collect all values less than median and sort Collect all values more than median and sort Combine these sorted sublists using ++ (No need to merge)

Sorting: Quicksort Suppose we can find the median (middle element in the sorted order) in a list Collect all values less than median and sort Collect all values more than median and sort Combine these sorted sublists using ++ (No need to merge) Median is not easy to find.

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice.

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice. Pick a pivot p from the list L

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice. Pick a pivot p from the list L Rearrange the list as L 1 pl 2 where

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice. Pick a pivot p from the list L Rearrange the list as L 1 pl 2 where L1 is the list of elements of L that are smaller than p and

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice. Pick a pivot p from the list L Rearrange the list as L 1 pl 2 where L1 is the list of elements of L that are smaller than p and L2 is the list of elements of L that are at least as big as p.

Quicksort A sorting algorithm proposed by C.A.R.Hoare. It is extensively used in practice. Pick a pivot p from the list L Rearrange the list as L 1 pl 2 where L1 is the list of elements of L that are smaller than p and L2 is the list of elements of L that are at least as big as p. Sort L 1 and L 2 recursively.

Quicksort... 265 319 389 345 159 267 348 365 128

Quicksort... 265 319 389 345 159 267 348 365 128 159 128 265 319 389 345 267 348 365

Quicksort... 265 319 389 345 159 267 348 365 128 159 128 265 319 389 345 267 348 365 128 159 265 267 319 345 365 389 348

Quicksort... 265 319 389 345 159 267 348 365 128 159 128 265 319 389 345 267 348 365 128 159 265 267 319 345 365 389 348 128 159 265 267 319 345 365 389 348

Quicksort... 265 319 389 345 159 267 348 365 128 159 128 265 319 389 345 267 348 365 128 159 265 267 319 345 365 389 348 128 159 265 267 319 345 365 389 348 128 159 265 267 319 345 348 365 389

Quicksort... 265 319 389 345 159 267 348 365 128 159 128 265 319 389 345 267 348 365 128 159 265 267 319 345 365 389 348 128 159 265 267 319 345 365 389 348 128 159 265 267 319 345 348 365 389 128 159 265 267 319 345 348 365 389

Quicksort... quicksort :: [Int] -> [Int] quicksort [] = [] quicksort (x:xs) = (quicksort lower) ++ [splitter] ++ (quicksort upper) where splitter = x lower = filter (<= x) xs upper = filter (> x) xs

Quicksort... quicksort :: [Int] -> [Int] quicksort [] = [] quicksort (x:xs) = (quicksort lower) ++ [splitter] ++ (quicksort upper) where splitter = x lower = filter (<= x) xs upper = filter (> x) xs In the worst case, lower or upper is empty

Quicksort... quicksort :: [Int] -> [Int] quicksort [] = [] quicksort (x:xs) = (quicksort lower) ++ [splitter] ++ (quicksort upper) where splitter = x lower = filter (<= x) xs upper = filter (> x) xs In the worst case, lower or upper is empty For our choice of splitter, worst case input is any sorted list

Measuring efficiency in Haskell Computation in Haskell is rewriting Using a definition to rewrite an expression = reduction step

Measuring efficiency in Haskell Computation in Haskell is rewriting Using a definition to rewrite an expression = reduction step Count number of reduction steps T(n) for input of size n

Measuring efficiency in Haskell Computation in Haskell is rewriting Using a definition to rewrite an expression = reduction step Count number of reduction steps T(n) for input of size n What is the complexity of ++? [] ++ y = y (x:xs) ++ y = x:(xs++y)

Measuring efficiency in Haskell Computation in Haskell is rewriting Using a definition to rewrite an expression = reduction step Count number of reduction steps T(n) for input of size n What is the 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])))

Measuring efficiency in Haskell Computation in Haskell is rewriting Using a definition to rewrite an expression = reduction step Count number of reduction steps T(n) for input of size n What is the 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]))) In l1 ++ l2 we use the second rule length l1 times and the first rule once It takes as many steps as length l1 + 1

Efficiency... We would like to define the complexity of a program to be a function from the size of the input to the number of steps.

Efficiency... We would like to define the complexity of a program to be a function from the size of the input to the number of steps. T(n) = n for ++ where n is the length of the left argument.

Efficiency... We would like to define the complexity of a program to be a function from the size of the input to the number of steps. T(n) = n for ++ where n is the length of the left argument. The function ++ takes the same number of steps on inputs of the same length.

Efficiency... We would like to define the complexity of a program to be a function from the size of the input to the number of steps. T(n) = n for ++ where n is the length of the left argument. The function ++ takes the same number of steps on inputs of the same length. This need not always be the case.

Efficiency... elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) (i==x) = True otherwise = elem i xs The number of steps taken depends on the input (not just its length)

Efficiency... elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) (i==x) = True otherwise = elem i xs The number of steps taken depends on the input (not just its length) elem 3 [3,7,8,9] -> True

Efficiency... elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) (i==x) = True otherwise = elem i xs The number of steps taken depends on the input (not just its length) elem 3 [3,7,8,9] -> True elem 3 [4,7,8,9] -> elem 3 [7,8,9] -> elem 3 [8,9] -> elem 3 [9] -> elem 3 [] -> False

Efficiency... elem :: Int -> [Int] -> Bool elem i [] = False elem i (x:xs) (i==x) = True otherwise = elem i xs The number of steps taken depends on the input (not just its length) elem 3 [3,7,8,9] -> True elem 3 [4,7,8,9] -> elem 3 [7,8,9] -> elem 3 [8,9] -> elem 3 [9] -> elem 3 [] -> False What do we take the value of T(n) to be?

Efficiency... We can take the complexity T(n) on inputs of length n to be

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity The average among all inputs of length n. Average-case complexity

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity The average among all inputs of length n. Average-case complexity Best-case complexity is useless. For eg. it suggests that elem returns the answer in one step on any inputs of any length n.

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity The average among all inputs of length n. Average-case complexity Best-case complexity is useless. For eg. it suggests that elem returns the answer in one step on any inputs of any length n. Worst-case is pessimistic. But it assures us that on any input of length n the program does NOT take more than T(n) steps.

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity The average among all inputs of length n. Average-case complexity Best-case complexity is useless. For eg. it suggests that elem returns the answer in one step on any inputs of any length n. Worst-case is pessimistic. But it assures us that on any input of length n the program does NOT take more than T(n) steps. Average-case is perhaps a more accurate description. But is usually very difficult to calculate.

Efficiency... We can take the complexity T(n) on inputs of length n to be The maximum among all inputs of length n. Worst-case complexity The minimum among all inputs of length n. Best-case complexity The average among all inputs of length n. Average-case complexity Best-case complexity is useless. For eg. it suggests that elem returns the answer in one step on any inputs of any length n. Worst-case is pessimistic. But it assures us that on any input of length n the program does NOT take more than T(n) steps. Average-case is perhaps a more accurate description. But is usually very difficult to calculate. For the purposes of this course, we stick to worst-case analysis.