Download - Sequential Search Algorithm
![Page 1: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/1.jpg)
SEQUENTIAL SEARCH; WORST, AVERAGE, AND BEST CASE SCENARIOS; AND THE GREATEST COMMON DIVISOR
Ayman Hajja, PhD
![Page 2: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/2.jpg)
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.
![Page 3: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/3.jpg)
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…
![Page 4: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/4.jpg)
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
![Page 5: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/5.jpg)
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?
![Page 6: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/6.jpg)
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
![Page 7: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/7.jpg)
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
![Page 8: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/8.jpg)
SEARCH PROBLEM (ANALYSIS)
!
MAKE NO MISTAKE
AVERAGE *is not (always) equal to* (BEST + WORST)/2
!
![Page 9: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/9.jpg)
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
![Page 10: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/10.jpg)
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?
![Page 11: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/11.jpg)
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]
![Page 12: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/12.jpg)
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…
![Page 13: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/13.jpg)
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
![Page 14: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/14.jpg)
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
![Page 15: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/15.jpg)
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
![Page 16: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/16.jpg)
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?
![Page 17: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/17.jpg)
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?
![Page 18: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/18.jpg)
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
![Page 19: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/19.jpg)
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
![Page 20: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/20.jpg)
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
![Page 21: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/21.jpg)
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
![Page 22: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/22.jpg)
BACK TO GCD
For two very large number, min(m, n) is not that great…
Can we do better?
![Page 23: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/23.jpg)
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)
![Page 24: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/24.jpg)
EUCLID’S ALGORITHM IS SIMPLE, YET PROFOUND
It makes the bold statement that factorization is *HARD*
![Page 25: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/25.jpg)
EUCLID’S ALGORITHM
Pseudocode function gcd(a, b)! while b ≠ 0! t := b! b := a mod b! a := t! return a!
Example on board
![Page 26: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/26.jpg)
GEOMETRIC REPRESENTATION OF EUCLID’S ALGORITHM
![Page 27: Sequential Search Algorithm](https://reader031.vdocuments.site/reader031/viewer/2022030316/577cce5f1a28ab9e788de4a1/html5/thumbnails/27.jpg)
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