Searching Tiziana Ligorio 1 Todays Plan Searching algorithms and - - PowerPoint PPT Presentation

searching
SMART_READER_LITE
LIVE PREVIEW

Searching Tiziana Ligorio 1 Todays Plan Searching algorithms and - - PowerPoint PPT Presentation

Searching Tiziana Ligorio 1 Todays Plan Searching algorithms and their analysis 2 Searching Looking for something! In this discussion we will assume searching for an element in an array 3 Linear search Most intuitive


slide-1
SLIDE 1

Searching

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Searching algorithms and their analysis

2

slide-3
SLIDE 3

Searching

3

Looking for something! In this discussion we will assume searching for an element in an array

slide-4
SLIDE 4

Linear search

Most intuitive
 Start at first position and keep looking until you find it
 


int linearSearch(int a[], int size, int value) {
 for (int i = 0; i < size; i++) { if (a[i] == value) { return i; } } return-1; }

4

slide-5
SLIDE 5

How long does linear search take?

If you assume value is in the array and probability of finding it at any location is uniform, on average n/2 If value is not in the array (worst case) n Either way it’s O(n)

5

slide-6
SLIDE 6

What if you know array is sorted? Can you do better than linear search?

6

slide-7
SLIDE 7

Lecture Activity

You are given a sorted array of integers. How would you search for 115? ( try to do it in fewer than n steps: don’t search sequentially) You can write pseudocode or succinctly explain your algorithm

7

slide-8
SLIDE 8

We have done this before! When?

8

slide-9
SLIDE 9

9

Look in ?

slide-10
SLIDE 10

Binary Search

10

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-11
SLIDE 11

Binary Search

11

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-12
SLIDE 12

Binary Search

12

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-13
SLIDE 13

Binary Search

13

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-14
SLIDE 14

Binary Search

14

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-15
SLIDE 15

Binary Search

15

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-16
SLIDE 16

Binary Search

16

14 43 76 100 108 158 195 200 274 523 543 599 3

slide-17
SLIDE 17

Binary Search

What is happening here?

17

slide-18
SLIDE 18

Binary Search

What is happening here? Size of search is cut in half at each step

18

slide-19
SLIDE 19

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 
 
 
 


19

Simplification: assume n is a power of 2 so it can be evenly divided in two parts The running time One comparison Search lower OR upper half

slide-20
SLIDE 20

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 T(n/2) = T(n/4) +1
 
 
 


20

One comparison Search lower OR upper half of n/2

slide-21
SLIDE 21

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 T(n/2) = T(n/4) +1
 T(n) = T(n/4) + 1 + 1
 
 


21

slide-22
SLIDE 22

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 
 T(n) = T(n/4) + 2
 . . . 
 


22

22 2 21 1

slide-23
SLIDE 23

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 
 T(n) = T(n/4) + 2
 . . . 
 T(n) = T(n/2k) + k 


23

slide-24
SLIDE 24

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 
 T(n) = T(n/4) + 2
 . . . 
 T(n) = T(n/2k) + k 
 T(n) = T(1) + log2(n)

24

n/n = 1 The number to which I need to raise 2 to get n And we said n = 2k

slide-25
SLIDE 25

Binary Search

What is happening here? Size of search is cut in half at each step Let T(n) be the running time and assume n = 2k
 T(n) = T(n/2) + 1
 
 T(n) = T(n/4) + 2
 . . . 
 T(n) = T(n/2k) + k 
 T(n) = T(1) + log2(n)

25

Binary search is O(log(n))