210 likes | 234 Views
Computer Science 3 03A-Searching. Sean P. Strout (sps@cs.rit.edu). 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)
E N D
Computer Science 303A-Searching Sean P. Strout (sps@cs.rit.edu) CS3 - 03A - Searching (v1.01)
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 0 1 2 3 4 5 6 7 8 9 10 11 10 7 9 4 12 5 8 1 6 3 11 2 CS3 - 03A - Searching (v1.01)
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 1 2 3 4 5 6 7 8 9 10 11 5 10 12 15 22 26 32 36 40 44 59 64 CS3 - 03A - Searching (v1.01)
Binary Search • With a binary search, the size of the array is halved on each iteration: • i.e. search for 22 0 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 CS3 - 03A - Searching (v1.01)
Binary Search • What happens when an element is not found? • i.e. search for 11 0 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 CS3 - 03A - Searching (v1.01)
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 0 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 0 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 CS3 - 03A - Searching (v1.01)
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 0 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 CS3 - 03A - Searching (v1.01)
Binary Search • If the element is not in the collection, the start index will eventually exceed the end index • i.e. search for 65 0 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 0 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 CS3 - 03A - Searching (v1.01)
Binary Search 0 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 0 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 0 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 CS3 - 03A - Searching (v1.01)
Binary Search • Given the follow collection using a binary search: • How many accesses will it take to locate element: • 32 • 44 • 5 • 99 0 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 CS3 - 03A - Searching (v1.01)
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 CS3 - 03A - Searching (v1.01)
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); CS3 - 03A - Searching (v1.01)
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 . CS3 - 03A - Searching (v1.01)
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 ... 998 999 1 2 3 ... 999 1000 CS3 - 03A - Searching (v1.01)
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 1 2 3 4 5 6 7 8 9 10 index: 0 99 199 299 399 499 599 699 799 899 999 0 99 199 999 1 ... 100 ... 200 ... 1000 CS3 - 03A - Searching (v1.01)
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 1 2 3 4 5 6 7 8 9 10 index: 0 99 199 299 399 499 599 699 799 899 999 0 99 199 299 999 1 ... 100 ... 200 ... 300 ... 1000 CS3 - 03A - Searching (v1.01)
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 0 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 CS3 - 03A - Searching (v1.01)
Ternary Search • Trace through a search for 44 and 12 : • The ternary search time is O(log3N) 0 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 CS3 - 03A - Searching (v1.01)
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 CS3 - 03A - Searching (v1.01)
ACME Project • The echo server will continuously read BankingMessage objects from the client: Received message VALIDATE 12345 0 • The echo server will always send back a BankingMessage object to the client indicating the operation was successful: Sent message SUCCESS_RESPONSE 12345 100000 • 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 CS3 - 03A - Searching (v1.01)
Revision History • Revision History • v1.00, 3/14/05 12:38 PM, sps Initial revision. CS3 - 03A - Searching (v1.01)