sequential search algorithm
TRANSCRIPT
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?
Let’s say I ask you to find element 4
!
!
12412310142
Set i to 1.!Repeat this loop:!! If A[i] = x, then exit the loop.!! Set i to i + 1.!Return i.
SEARCH PROBLEM (ANALYSIS)
How efficient/fast is this (sequential search) algorithm?
!
!
!
Simple (naive) answer is:
It depend on the given array (and element you’re 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)
Answer: When the element we’re trying to find is first in the list.
!
Example: Find element with value 12
!
Only one comparison
12412310142
Program Ends
Best Case Scenario. When would that be?
SEARCH PROBLEM (ANALYSIS)
Worst Case Scenario. When would that be?
Answer: When element we’re trying to find is last in the list.
!
Example: Find element with value 2
!
N time, where N is the size of array
12412310142 Program Ends
SEARCH PROBLEM (ANALYSIS)
Average Case Scenario. When would that be?
Answer: When element we’re trying to find is the middle of the list.
!
Example: Find element with value 10
!
N/2 time, where N is the size of array
12412310142
Program Ends
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 we’re trying to find is the middle of the list.
What we should have said: When element we’re trying to find is in its average position, after (perhaps virtually) trying many input configurations
SEARCH PROBLEM (ANALYSIS)
Let’s 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 we’re trying to find is in its average posit ion, after (perhaps vir tually) trying many input configurations
If we’re trying to find element 2 for example, where would its average position be; in this particular problem definition?
SEARCH PROBLEM (ANALYSIS)
If we’re randomly generate 90 lists, we would get
!
!
!
? 10 times will land here [0 0 0 0 0 0 0 0 0]? 10 times will land here [1 1 1 1 1 1 1 1 1]? 10 times will land here [2 2 2 2 2 2 2 2 2]? 10 times will land here [3 3 3 3 3 3 3 3 3]? 10 times will land here [4 4 4 4 4 4 4 4 4]? 10 times will land here [5 5 5 5 5 5 5 5 5]? 10 times will land here [6 6 6 6 6 6 6 6 6]? 10 times will land here [7 7 7 7 7 7 7 7 7]? 10 times will land here [8 8 8 8 8 8 8 8 8]
SEARCH PROBLEM (ANALYSIS)
Let’s 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
I’ll 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
I’ll 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?
EUCLID’S 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)
EUCLID’S ALGORITHM IS SIMPLE, YET PROFOUND
It makes the bold statement that factorization is *HARD*
EUCLID’S 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 EUCLID’S ALGORITHM
SEARCH PROBLEM WHEN OUR ELEMENTS ARE SORTED
Given a list of *sorted* items, what is the best algorithm to find a particular element?
Let’s say I ask you to find element 12
!
Think about for next class
12410122341