Download presentation
Presentation is loading. Please wait.
Published byAllyson McKenzie Modified over 8 years ago
1
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day
2
Section 4 Some Common Algorithms Searching Sorting
3
Chapter 9: Searching Objectives –To consider efficient searching algorithms –To study their algorithmic complexity –To consider some of the implementation issues arising in Java
4
Searching Not new –Lists, dictionaries, hash tables and binary search tree ADTs all had “search” methods Reconsider these Look at more advanced techniques: –binary search –interpolated binary search
5
Implementation Issues We will focus only on the “keys” Restrict ourselves to arrays –accessing an array: O(1) –accessing a linear linked list: O(n) Use the Comparable interface –Comparable[] list;
6
Old Friends Lists (chapter 4) –position method: simple, sequential search –O(n) ListDictionary (chapter 7) –contains method: sequential search of ordered list On average: n/2 Still O(n)
7
Searching a Hash Table Assumption 1: No collisions –O(1) Assumption 2: clusters/buckets of size c –Found: c/2 –Not found: c –Average: 3c/4
8
Hash Tables (cont.) How is c related to n? Assumption: even distribution of keys –c is proportional to n –Still O(n), but constants are very small –e.g. 0.0375 (rather than 3/4, or 1/2)
9
Hash Tables (cont.) Proper analysis is complicated Internal hashing –item is found External hashing –item is found Where M is the size of the table
10
Hash Tables (cont.) External hashing –item not found Dominant factor: n/M –Measure of how full the table is
11
Binary Search Better than sequential searches (all O(n)) Start in the middle of the (sorted) list 381215202129373942 012 3456789 Searching for 29 Found Total of 4 comparisons
12
The Java Code public static int binarySearch (Comparable[] lst, Comparable item) { int left = 0, right = lst.length-1, look; do { look = (left + right) / 2; if (lst[look].compareTo(item) > 0) right = look - 1; else left = look + 1; } while (lst[look].compareTo(item) != 0 && left <= right); if (lst[look].compareTo(item) == 0) return look; else return -1; } // binarySearch
13
Complexity of the Binary Search Let the number of comparisons for a list of n elements be C n We halve the list with each comparison C n = C n/2 + 1 List with only one element: C 1 =1
14
Complexity (cont.) Assume n is an exact power of 2: n = 2 k C 2 k = C 2 k–1 + 1 This is a Recurrence Relation C 2 k = (C 2 k–2 + 1) + 1 C 2 k = ((C 2 k–3 + 1) + 1) + 1 … C 2 k = C 2 0 + k C 2 k = 1 + k C n = C n/2 + 1
15
Complexity (cont.) So C 2 k = k + 1 Since n = 2 k we have k = log 2 n C n = log 2 n + 1 O(log n)
16
Can we do better? O(log n) is a very good result –search 1 000 000 items with 20 comparisons Yes!
17
Interpolated Binary Search Rather than look in the middle we use the data values to interpolate the position 5 0 1250 999... Searching for 290 Look at position: 290 - 5 1250 - 5 x (999 - 0) = 229
18
Interpolation The values must be numeric, or else interpolated in some different way –Alphabetic data: use Unicode ordering
19
Complexity Harder to work out O(log log n) ItemsComparisons 10003 1 000 0005
20
Binary Search Trees Closely related to the binary search 381215202129373942 012 3456789 20 837 3122139 152942
21
Binary Search Trees: Complexity Analysis similar to that for the binary search Complexity is the same: O(log n) –if the tree is balanced
22
Chapter 9: Summary Analysed the complexity of sequential searches Studied binary search and interpolated binary search Analysed their complexity Compared binary search with binary search tree
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.