You are on page 1of 27

SEQUENTIAL SEARCH; WORST, AVERAGE, AND BEST CASE SCENARIOS; AND THE GREATEST COMMON DIVISOR

Ayman Hajja, PhD

SEARCH PROBLEM (SOLUTION)


Given a list of *unsorted* items, what is the best algorithm to find a particular element? Lets say I ask you to find element 4
! Set i to 1.! Repeat this loop:! ! If A[i] = x, then exit the loop.! ! ! Set i to i + 1.! Return i.
12 41 23 10 1 4 2

SEARCH PROBLEM (ANALYSIS)


How efficient/fast is this (sequential search) algorithm?
!

Simple (naive) answer is:


!

It depend on the given array (and element youre trying to find)


!

Any suggestions

SEARCH PROBLEM (ANALYSIS)


The answer is: we calculate how efficient our algorithm is in three different scenarios: Best case scenario Worst case scenario Average case scenario

SEARCH PROBLEM (ANALYSIS)


Best Case Scenario. When would that be?
Answer: When the element were trying to find is first in the list.
!
12 41 23 10 1 4 2

Program Ends

Example: Find element with value 12


!

Only one comparison

SEARCH PROBLEM (ANALYSIS)


Worst Case Scenario. When would that be? Answer: When element were trying to find is last in the list.
!
12 41 23 10 1 4 2

Example: Find element with value 2


!

N time, where N is the size of array

Program Ends

SEARCH PROBLEM (ANALYSIS)


Average Case Scenario. When would that be? Answer: When element were trying to find is the middle of the list.
!
12 41 23 10 1 4 2

Example: Find element with value 10


!

Program Ends

N/2 time, where N is the size of array

SEARCH PROBLEM (ANALYSIS)


!

MAKE NO MISTAKE AVERAGE *is not (always) equal to*


!

(BEST + WORST)/2

SEARCH PROBLEM (ANALYSIS)


Average Case Scenario (Revisited). When would that be? We said earlier: When element were trying to find is the middle of the list. What we should have said: When element were trying to find is in its average position, after (perhaps virtually) trying many input configurations

SEARCH PROBLEM (ANALYSIS)


Lets define the problem again, find element (could be any number between 1 and 9) in a *randomly generated* list of 9 elements containing numbers from 1 to 9 RECALL: When element were trying to find is in its average position, after (perhaps virtually) trying many input configurations If were trying to find element 2 for example, where would its average position be; in this particular problem definition?

SEARCH PROBLEM (ANALYSIS)


If were randomly generate 90 lists, we would get
! ! !
? ? ? ? ? ? ? ? ? 10 times will land here 10 times will land here 10 times will land here 10 times will land here 10 times will land here 10 times will land here 10 times will land here 10 times will land here 10 times will land here [0 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1] [2 2 2 2 2 2 2 2 2] [3 3 3 3 3 3 3 3 3] [4 4 4 4 4 4 4 4 4] [5 5 5 5 5 5 5 5 5] [6 6 6 6 6 6 6 6 6] [7 7 7 7 7 7 7 7 7] [8 8 8 8 8 8 8 8 8]

SEARCH PROBLEM (ANALYSIS)


Lets define a different problem: Find element (could be any number between 1 and 9) in a *randomly generated* list of 9 elements containing numbers from 1 to 100 How many comparisons would we need to perform in the average case?
!

Think about it

SEARCH PROBLEM (ANALYSIS)


Another way of looking at it: The average (running) time of the sequential search algorithm is the average running time to find a particular element; when running the algorithm for many random input configurations

SEARCH PROBLEM (ANALYSIS)


Same goes for Sorting The average (running) time of any sorting algorithm is the average time it takes to sort a list; when running the algorithm for many random input/list configurations

SEARCH PROBLEM (ANALYSIS)


!

When do you think it would be the best case scenario to sort a list in ascending order? a) When the (input) list given is already sorted in descending order b) When the (input) list given is already sorted in ascending order c) When the (input) list is not sorted in either of the two orders

SEARCH PROBLEM (ANALYSIS)


When do you think it would be the worst case scenario to sort a list in ascending order? a) When the (input) list given is already sorted in descending order b) When the (input) list given is already sorted in ascending order c) When the (input) list is not sorted in either of the two orders d) Hmm. this is hard; it depends maybe?

SEARCH PROBLEM (ANALYSIS)


When do you think it would be the worst case scenario to sort a list in ascending order? a) When the (input) list given is already sorted in descending order b) When the (input) list given is already sorted in ascending order c) When the (input) list is not sorted in either of the two orders d) Hmm. this is hard; it depends maybe?

SEARCH PROBLEM (ANALYSIS)


Same goes for Greatest Common Divisor The average (running) cost of the any Greatest Common Divisor algorithm is the average time it takes to of comparisons to find a particular element; when running the algorithm for many random input configurations

BACK TO GCD

The *very slow* algorithm: generate all divisors and find the greatest common one GCD(m, n) will take m+n seconds, *Assuming each mod will take one second

BACK TO GCD
Ill call this: GCD Min Algorithm The *very slow, but better* algorithm: generate all divisors and find the greatest common one, starting with minimum(m, n) GCD(m, n) will take 2*min(m, n) seconds *Assuming each mod will take one second Note here that we are generating divisors for m and n independently; for simplicity

BACK TO GCD
Ill call this: GCD Half-once Algorithm The *very slow, but better* algorithm: generate all divisors and find the greatest common one, starting with minimum(m, n) GCD(m, n) will take 2*min(m, n)/2 = min(m, n) seconds *Assuming each mod will take one second Again, we are generating divisors for m and n independently; for simplicity

BACK TO GCD

For two very large number, min(m, n) is not that great Can we do better?

EUCLIDS ELEMENTS
Many consider Euclid of Alexandria a non-mathematician 13 books were written more than 2000 years ago Books used to be small, so they were merged into one (only ! ~100 page) book now Mostly geometry, but few volumes of the 13 books (or sections) were about number theory (topics such as GCD)

EUCLIDS ALGORITHM IS SIMPLE, YET PROFOUND

It makes the bold statement that factorization is *HARD*

EUCLIDS ALGORITHM
Pseudocode function gcd(a, b)! while b " 0! t := b! b := a mod b! a := t! return a! Example on board

GEOMETRIC REPRESENTATION OF EUCLIDS ALGORITHM

SEARCH PROBLEM WHEN OUR ELEMENTS ARE SORTED


Given a list of *sorted* items, what is the best algorithm to find a particular element? Lets say I ask you to find element 12
!
1 2 4 10 12 23 41

Think about for next class

You might also like