Download presentation
Presentation is loading. Please wait.
1
Computer Science 3 03A-Searching
Sean P. Strout 5/6/2019 CS3 - 03A - Searching (v1.01)
2
A sequential search runs in linear time
Computer Science 3 ( ) 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 1 2 3 4 5 6 7 8 9 10 11 10 7 9 4 12 2 5 8 1 6 3 11 5/6/2019 CS3 - 03A - Searching (v1.01)
3
What happens to the search times if the data is now ordered?
Computer Science 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 1 2 3 4 5 6 7 8 9 10 11 5 10 12 15 22 26 32 36 40 44 59 64 For elements in the array, the search times are the same. For elements not in the array: element < n0: O(1) n0 < element < n11: O(n/2) element > n11: O(n) 5/6/2019 CS3 - 03A - Searching (v1.01)
4
Computer Science 3 ( ) Binary Search With a binary search, the size of the array is halved on each iteration: i.e. search for 22 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 #1: 22 < 32, go left half the distance #2: 22 > 12, go right half the distance #3: 22 = 22, element found 5/6/2019 CS3 - 03A - Searching (v1.01)
5
What happens when an element is not found?
Computer Science 3 ( ) Binary Search What happens when an element is not found? i.e. search for 11 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 #1: 11 < 32, go left half the distance #2: 11 < 12, go left half the distance #3: 11 > 5, go right half the distance #4: 11 > 10 can’t go right half the distance, element not found 5/6/2019 CS3 - 03A - Searching (v1.01)
6
Computer Science 3 ( ) Binary Search The search algorithm uses three indexes to mark the start, middle and end positions i.e. search for 22 If the middle element is greater than the target, the end moves to middle - 1 and new middle = (start + end)/2 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)
7
Computer Science 3 ( ) Binary Search If the middle element is greater than the value, the start moves to middle + 1 and new middle = (start + end)/2 If the middle element equals the value, the search stops and the element is found 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)
8
Computer Science 3 ( ) Binary Search If the element is not in the collection, the start index will eventually exceed the end index i.e. search for 65 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)
9
Computer Science 3 ( ) Binary Search 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 end start 5/6/2019 CS3 - 03A - Searching (v1.01)
10
Given the follow collection using a binary search:
Computer Science 3 ( ) Binary Search Given the follow collection using a binary search: How many accesses will it take to locate element: 32 44 5 99 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 32: 1 44: 2 5: 3 99: 4 5/6/2019 CS3 - 03A - Searching (v1.01)
11
A binary search happens in logarithmic time
Computer Science 3 ( ) 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 5/6/2019 CS3 - 03A - Searching (v1.01)
12
Binary Search - Testing Your Understanding
Computer Science 3 ( ) 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); 5/6/2019 CS3 - 03A - Searching (v1.01)
13
Binary Search - Testing Your Understanding
Computer Science 3 ( ) 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 . Access time of 3: 16, 48, 80, 112 5/6/2019 CS3 - 03A - Searching (v1.01)
14
Consider the binary search time when N=1000
Computer Science 3 ( ) 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 1 2 ... 998 999 1 2 3 ... 999 1000 5/6/2019 CS3 - 03A - Searching (v1.01)
15
For example, if we are searching for the value 186
Computer Science 3 ( ) Indexed Binary Search 1 2 3 4 5 6 7 8 9 10 index: 99 199 299 399 499 599 699 799 899 999 99 199 999 1 ... 100 ... 200 ... 1000 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/ ] = index[2] = 199 Binary search the range from (N=100): O(log2N) = O(log2100) = ~6.64 Total search time = = ~8.64 accesses 5/6/2019 CS3 - 03A - Searching (v1.01)
16
Given the following indexed collection using a binary search:
Computer Science 3 ( ) 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 1 2 3 4 5 6 7 8 9 10 index: 99 199 299 399 499 599 699 799 899 999 99 199 299 999 1 ... 100 ... 200 ... 300 ... 1000 250: 3 275: 4 262: 5 256: 6 251: 8 5/6/2019 CS3 - 03A - Searching (v1.01)
17
Computer Science 3 ( ) 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, move end to probe - 1 If target is greater than, move start to probe + 1 and recompute probe as end - third. Compare the element at probe: If less than, end = probe - 1 If greater than, start = probe + 1 third = (end - start) / 3 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start probe = start + third end 5/6/2019 CS3 - 03A - Searching (v1.01)
18
Trace through a search for 44 and 12 :
Computer Science 3 ( ) Ternary Search Trace through a search for 44 and 12 : The ternary search time is O(log3N) 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 FIND 44: step 1: third = 4 start = 0, end = 12, probe = 4 (value is greater) start = 5, end = 12, probe = 8 (value is greater) start = 9, end = 12 (search in this range) step 2: third = 1 start = 9, end = 12, probe = 10 (value is less) start = 9, end = 9 step3: third = 0 start = 9, end = 9, probe = 9 (value is equal) FIND 12: start = 0, end = 12, probe = 4 (value is less) start = 0, end = 3 start = 0, end = 3, probe = 1 (value is greater) start = 2, end = 3, probe = 2 (value is equal) 5/6/2019 CS3 - 03A - Searching (v1.01)
19
To run you must specify an unused port:
ACME Project For part 1, an echo server for simulating the bank is being provided for you: /usr/local/pub/sps/courses/cs3/projects/ACME/EchoBank.java To run you must specify an unused port: % java EchoBank 9125 EchoBank listening on port 9125 When a client successful connects to the echo server: Accepted socket[addr=/#.#.#.#,port=####,localport=9125] When a client closes the socket to the echo server: ATM has terminated the connection 5/6/2019 CS3 - 03A - Searching (v1.01)
20
Received message VALIDATE 12345 0
ACME Project The echo server will continuously read BankingMessage objects from the client: Received message VALIDATE The echo server will always send back a BankingMessage object to the client indicating the operation was successful: Sent message SUCCESS_RESPONSE The echo server can only accept one client connection at a time, and must be terminated manually Eclipse: Running ATM and EchoServer and selecting their console output 5/6/2019 CS3 - 03A - Searching (v1.01)
21
Revision History Revision History v1.00, 3/14/05 12:38 PM, sps
Computer Science 3 ( ) Revision History Revision History v1.00, 3/14/05 12:38 PM, sps Initial revision. 5/6/2019 CS3 - 03A - Searching (v1.01)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.