Home > Documents > Computer Science 3 03A-Searching

# Computer Science 3 03A-Searching

Date post: 31-Dec-2015
Category:
View: 20 times
Description:
Computer Science 3 03A-Searching. Sean P. Strout ([email protected]). Sequential Search. A sequential search runs in linear time Best Case: search for 10 O(1) Average Case: search for 2 O(6) = O(n/2) Worst Case: search for 11,99 O(12) = O(n) - PowerPoint PPT Presentation
19
06/20/22 CS3 - 03A - Searching (v1.02) 1 Computer Science 3 03A-Searching Sean P. Strout ([email protected])
Transcript

04/19/23 CS3 - 03A - Searching (v1.02) 1

Computer Science 303A-Searching

Sean P. Strout ([email protected])

04/19/23 CS3 - 03A - Searching (v1.02) 2

Sequential Search

• A sequential search runs in linear time

• Best Case: search for 10– O(1)

• Average Case: search for 2– O(6) = O(n/2)

• Worst Case: search for 11,99 – O(12) = O(n)

• If collection size doubles, the search time doubles

10 7 9 4 12 2 5 8 1 6 3 11

0 1151 2 3 4 6 7 8 9 10

04/19/23 CS3 - 03A - Searching (v1.02) 3

Sequential Search

• What happens to the search times if the data is now ordered?– For elements in the array, i.e. 59– For elements not in the array, i.e.: 0, 31, 99

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

04/19/23 CS3 - 03A - Searching (v1.02) 4

Binary Search

• With a binary search, the size of the array is halved on each iteration:– i.e. search for 22

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

#1: 22 < 32, go left half the distance

#3: 22 = 22, element found

#2: 22 > 12, go right half the distance

04/19/23 CS3 - 03A - Searching (v1.02) 5

Binary Search

• What happens when an element is not found?– i.e. search for 11

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

#1: 11 < 32, go left half the distance

#2: 11 < 12, go left half the distance

#4: 11 > 10 can’t go right half the distance, element not found

#3: 11 > 5, go right half the distance

04/19/23 CS3 - 03A - Searching (v1.02) 6

Binary Search

• The search algorithm uses three indexes to mark the start, middle and end positions– e.g. search for 22

• The middle element is greater than the target, so– the end moves to middle - 1 and – new middle = (start + end)/2

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

04/19/23 CS3 - 03A - Searching (v1.02) 7

Binary Search

• The new middle element is less than the value, so– the start moves to the new middle + 1 and – new middle = (start + end)/2

• The middle element equals the value, so the search stops and the element is found

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

04/19/23 CS3 - 03A - Searching (v1.02) 8

Binary Search

• If the element is not in the collection, the start index will eventually exceed the end index– e.g. search for 65

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

04/19/23 CS3 - 03A - Searching (v1.02) 9

Binary Search

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endmiddle

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

startend

04/19/23 CS3 - 03A - Searching (v1.02) 10

Binary Search

• Given the following collection, using a binary search:

• How many accesses will it take to locate element or determine it is not there:– 32– 44– 5– 99

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

04/19/23 CS3 - 03A - Searching (v1.02) 11

Binary Search

• A binary search happens in logarithmic time– Best Case: O(1)

– Average Case: O(log2n)

– Worst Case: O(log2n)

• x=log2n, where x is the power we raise 2 to, to get n– log21 = 0 (20 = 1)

– log22 = 1 (21 = 2)

– log24 = 2 (22 = 4)

– log28 = 3 (23 = 8)

• O(log2n) grows slower than O(n), but the collection must be sorted first

04/19/23 CS3 - 03A - Searching (v1.02) 12

Binary Search - Testing Your Understanding

• Write a sample program, TestSearch.java, which demonstrates a binary search of your InstrumentedArray from lab2

• The main method should:– Read in a single argument which is the Integer to

search for– Create an InstrumentedArray with the following Integer

values: 1-128– Call the search method:

// Returns the position of the target in the

// array, or -1 if is not present

public static int binarySearch(InstrumentedArray array, int target);

04/19/23 CS3 - 03A - Searching (v1.02) 13

Binary Search - Testing Your Understanding

• main method continued:– Print out the results of the search. Position = -1 if not

found.

Array accessed # times. Value i at position #.

• Verify the algorithm is O(log2128)= 7– Search for 0, 1, 63, 64, 65 and 128– Search for elements not in the collection– What elements give an access time of 3?

• I have a perl script which will run all the tests and print results/statistics. Copy it to your local directory to run:

cp /usr/local/pub/sps/courses/cs3/search/BinarySearch/run.pl .

04/19/23 CS3 - 03A - Searching (v1.02) 14

Indexed Binary Search

• Consider the binary search time when N=1000

• O(log2N) = O(log21000) = ~9.97 accesses

• The search time on a large data set can be improved by building an index table for looking up the subset range for the target value

0 1 2

31 2 ...

999

1000

...

999

998

04/19/23 CS3 - 03A - Searching (v1.02) 15

Indexed Binary Search

• For example, if we are searching for the value 186– Start range = index[186/100] = index[1] = 99

• O(1) = 1

– End range = index[186/100 + 1] = index[2] = 199• O(1) = 1

– Binary search the range from 99-199 (N=100):• O(log2N) = O(log2100) = ~6.64

– Total search time = 1 + 1 + 6.64 = ~8.64 accesses

0 99

1001 ...

999

1000...

0 51 2 3 4 6 7 8 9 10

1990 99 299 399 499 599 699 799 899 999

200 ...

199

index:

04/19/23 CS3 - 03A - Searching (v1.02) 16

Indexed Binary Search

• Given the following indexed collection, using a binary search:

• How many accesses will it take to find the element:– 250– 275– 262– 256– 251

0 99

1001 ...

999

1000...

0 51 2 3 4 6 7 8 9 10

1990 99 299 399 499 599 699 799 899 999

200 ...

199

index:

300 ...

299

04/19/23 CS3 - 03A - Searching (v1.02) 17

Ternary Search

• A ternary search builds on the idea of binary search by splitting the search space into thirds (vs. halves)

• For each iteration, compute third & probe and compare element at the probe:– If target is less than probe, move end to probe - 1– If target is greater than probe,

• move start to probe + 1 and • recompute probe as (end - third).

– Compare the element at probe:• If target is less than probe, end = probe - 1• If target is greater than probe, start = probe + 1

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

start endprobe = start + third

third = (end - start) / 3

04/19/23 CS3 - 03A - Searching (v1.02) 18

Ternary Search

• Trace through a search for 44 and 12 :

• The ternary search time is O(log3N)

0 1151 2 3 4 6 7 8 9 10

125 10 15 22 26 32 36 40 44 59 64

12

68

04/19/23 CS3 - 03A - Searching (v1.02) 19

Revision History

• Revision History– v1.00, 3/14/05 12:38 PM, sps

Initial revision.-- v1.02, 3/27/07, chr Minor clarifications

Recommended