L13c Searching
L13c Searching
• Binary Search
• Hashed List Searches
– Collision Resolution
Linear List Searches
• The sequential search algorithm is very slow for the big lists.
• Big-O(n)
.. .. .. ..
. . . .
mid=(first+last)/2
Sequential Sequential
Size Binary (Average) (Worst case)
16 4 8 16
50 6 25 50
256 8 128 256
1.000 10 500 1.000
10.000 14 5.000 10.000
100.000 17 50.000 100.000
1.000.000 20 500.000 1.000.000
Hashed List Searches
• The data structure must contain an element for every possible key.
Direct hashing of
employee numbers.
Subtraction Hashing Method
1 Ali Esin
2 Sema Metin
x=1001 1
x=1002 2
x – 1000 100
x=1100
99
100 Filiz Yılmaz
Modulo Division Hashing Method
We have 300 employees, and the first prime greater that 300 is 307!.
Digit Extraction Method
• Selected digits are extracted from the key and used as the
address.
Example:
379452 394
121267 112
378845 388
526842 568
Midsquare Hashing Method
• The key is squared and the address selected from the middle of
the squared number.
• The most obvious limitation of this method is the size of the
key.
Example:
9452 * 9452 = 89340304 3403 is the address.
Or
379452 379 * 379 = 143641 364
Folding Hashing Method
Pseudorandom Hashing Method
k
α= --------- x 100 the number of elements
n
Collision Resolution Concepts
“Clustering”
• Some hashing algorithms tend to couse data to group
within the list. This is known as clustering.
• Clustering is created by collision.
• If the list contains a high degree of clustering, then
the number of probes to locate an element grows and
the processing efficiency of the list is reduced.
Collision Resolution Concepts
“Clustering”
– Quadratic probe,
– Double hashing,
– Key offset.
Open Addressing
“Linear Probe”
• When data cannot be stored in the home address, we
resolve the collision by adding one to the current address.
Advantage:
• Simple implementation!
• Data tend to remain near their home address.
Disadvantages:
• It tends to produce primary clustering.
.
• The search algorithm may become more complex
especially after data have been deleted!
Open Addressing
“Linear Probe”
Home
Key Address Key Offset Probe 1 Probe 2
572556 2 1865 26 50
collisions!
Collision Resolution
Linked List Resolution
Link head pointer.