Chapter 1
Analysis of Algorithms
Search Algorithms
Design and Analysis of Computer
Algorithms
Simple Search Algorithms:
• Linear search
• Binary search
• Hashing
linear-search algorithm is in the class O(n)
scan the entries in A and compare 1. def linear-search(A,x):
each entry with x. 2. for i in range(len(A)):
If after j comparisons, 0 ≤ j ≤ n-1, the 3. if A[i] == x:
search is successful, i.e., x = A[j], true 4. return True //successful search
is returned;
5. return False //unsuccessful search
otherwise a false is returned
indicating an unsuccessful search.
Simple Search Algorithms:
• Linear search
• Binary search
• Hashing
Binary-search algorithm
• Logarithmic search
• Divide and conquer approach
• Input is sorted
lo mid hi
A[i] < A[mid] A[mid] A[i] >A[mid]
left Mid-1 Mid+1
right
lo hi
lo hi lo hi
Left = n/2 right = n/2
X < A[mid] X > A[mid]
Binary-search algorithm – recursive
BINARY-SEARCH (A, lo, hi, x)
if (lo > hi)
return 0 Un Successful search
mid (lo+hi)/2
if x = A[mid]
return mid Successful search
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x) Try again left part
same problem of size n/2
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x) Try again right part
same problem of size n/2
Binary-search algorithm
1 2 3 4 5 6 7
2 5 7 11 15 20 50
1 2 3 4 5 6 7
2 5 7 11 15 20 50
1 2 3 5 6 7
2 5 7 15 20 50
1 3 5 7
2 7 15 50
BINARY-SEARCH (A, 1, 7, 15)
1 2 3 4 5 6 7
2 5 7 11 15 20 50
BINARY-SEARCH (A, 1, 7, 15)
1 2 3 4 5 6 7
2 5 7 11 15 20 50
BINARY-SEARCH (A, 5, 7, 15)
5 6 7
15 20 50
BINARY-SEARCH (A, 5, 5, 15) 5
Return 5
successful search 15
BINARY-SEARCH (A, 1, 7, 10)
1 2 3 4 5 6 7
2 5 7 11 15 20 50
BINARY-SEARCH (A, 1, 7, 10)
1 2 3 4 5 6 7
2 5 7 11 15 20 50
BINARY-SEARCH (A, 1, 3, 10)
1 2 3
2 5 7
BINARY-SEARCH (A, 3, 3, 10)
3
7
BINARY-SEARCH (A, 4, 3, 10)
Return 0
Unsuccessful search
Binary-search algorithm- iterative
• begin the search in the middle of 1. Binary-Search[A, n, x]
the list & compare the data of that 2. low ← 1
middle to the target. 3. high ← n
• If A[mid] = target → successful 4. while (low ≤ high)
search 5. mid ← (low + high)/2
• If A[mid] < target → search again 6. if A[mid] = x
in the upper part of the list 7. return mid //successful search
• If A[mid] > target → search again in
the lower part of the list 8. elseif (A[mid] < x)
• Each comparison or iteration 9. low ← mid + 1
reduces the search space to half 10. else high ← mid − 1
N/2 11. return 0 //unsuccessful search
• Until item is found or space is out of
range.
Binary-search example: Binary-Search[A, 1,14, 15]
1 4 5 7 8 9 10 12 15 22 23 27 32 35
1 2 3 4 5 6 7 8 9 10 11 12 13 14
First, we compare x with the middle element A[(1 + 14)/2] = A[7] = 10. Since 15>
A[7],, x cannot be in A[1..7], and therefore this portion of the array can be discarded.
So, we repeat with the subarray 12 15 22 23 27 32 35
A[8...14] 8 9 10 11 12 13 14
15 < A[11]
x cannot be in A[12...14].
So, we repeat with the subarray 12 15 22
A[8...10] 8 9 10
Finally, we find that x = A[9], and
the search is successfully
completed. A[9] =15 ➔ successful search
Binary-search example: Binary-Search[A, 1,14, 35]
1 4 5 7 8 9 10 12 15 22 23 27 32 35
1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 22 23 27 32 35
12
8 9 10 11 12 13 14
27 32 35
12 13 14
35
14 A[14] =35 ➔ successful search
Binary-search example: Binary-Search[A, 14, 13]
1 4 5 7 8 9 10 12 15 22 23 27 32 35
1 2 3 4 5 6 7 8 9 10 11 12 13 14
First, we compare x with the middle element A[(1 + 14)/2] = A[7] = 10. Since 13>
A[7],, x cannot be in A[1..7], and therefore this portion of the array can be discarded.
So, we repeat with the subarray
A[8...14] 12 15 22 23 27 32 35
13 < A[11] 8 9 10 11 12 13 14
x cannot be in A[12...14].
So, we repeat with the subarray 12 15 22
A[8...10]
13 < A[9] 8 9 10
x is in A[8...8].
12 repeat with the subarray A[9...8]
repeat with the subarray A[8...8] 8 (low > high) ➔ return 0
13 > A[8] unsuccessful search
Binary-search complexity
In each call/iteration, the search
BINARY-SEARCH (A, lo, hi, x) gets reduced to half of the array.
if (lo > hi)
𝒏
return 0
mid (lo+hi)/2 𝒏
if x = A[mid] 𝟐𝟏
return mid 𝒏
if ( x < A[mid] ) 𝟐𝟐
BINARY-SEARCH (A, lo, mid-1, x) 𝒏
if ( x > A[mid] ) 𝟐𝟑
BINARY-SEARCH (A, mid+1, hi, x)
1
For n elements in the array,
𝒏
there are logn iterations. 𝒔𝒕𝒐𝒑 𝒂𝒕 𝒌
=𝟏
𝟐
➔𝒏 = 𝟐𝒌
Best case → O(1) ➔𝒌 = 𝐥𝐨𝐠 𝟐 𝒏
Worst case → O(logn)
Simple Search Algorithms:
• Linear search
• Binary search
• Hashing
Hashing
Hashing is a technique to convert a range of key values
into a range of indexes of an array.
• Why hashing?
– better than a binary search
• hashing is used in:
– Web searches
– Databases
– Compilers
– passwords
– Many others
Hashing
General idea
hash table
key space (e.g., hash function:
integers, strings) h(K)
…
Basic Operations:
Search (lookup) − Searches an element in a hash table. TableSize –1
Insert − inserts an element in a hash table.
delete − Deletes an element from a hash table.
Hashing hash table
Domain of keys
Lookup function
(Hash function)
int hashCode(int key){
return key % SIZE;
}
145 %12 = 1
92 %12 = 8
241 %12= 2
127 %12 = 7
73 %12 = 1
154 %12 = 10
Example
• key space = integers 0
1 41
• TableSize = 10
2
• h(K) = K mod 10 3
4 94 ? 14 collisions.
• Insert: 7, 18, 41, 94,14 5
– 7 mod 10 → 7 6
– 18 mod 10 → 8 7 7
– 41 mod 10 → 1 8 18
– 94 mod 10 → 4
9
– 14 mod 10 → 4
Hash table
A hash table is an array of some fixed size.
– Expected time for lockup, Get, Insert, and Delete is
O(1).
– A collision is defined when multiple keys map onto
the same table index.
– Table size is usually prime to avoid bias.
• large table size means wasted space.
• small table size means more collisions.
Hash Functions
• Must return number 0, …, tablesize-1
– Hash(x) = x % TableSize
• Should be efficiently computable → O(1) time.
• Should not waste space unnecessarily
– For every index, there is at least one key that hashes to it
– Load factor lambda = (number of keys / TableSize).
• Should minimize collisions
– different keys hashing to same index
– Good idea to make TableSize prime. Why?
Collision Resolution
Collision: when two keys map to the same
location (index) in the hash table.
one way to resolve collisions is Chaining.
chaining: All keys that map to the same hash
value are kept in a list (or “bucket”).
22
Sorted [0] 0 34
Chains
[4]
• Put in pairs whose
6 23
keys are 6, 12, 34,
7
29, 28, 11, 23, 7, 0,
33, 30, 45 [8]
• Bucket = key % 17.
11 28 45
[12] 12 29
30
[16] 33
Thank you
Next:
Simple Sorting Algorithms