You are on page 1of 24

Searching

By : Deepak Kumar Singh


Introduction to search techniques
 Not even a single day pass, when we do not have to search for

something in our day to day life, car keys, books, pen, mobile
charger and what not.
 Same is the life of a computer, there is so much data stored in it,

that whenever a user asks for some data, computer has to search
it's memory to look for the data and make it available to the user.
 And the computer has it's own techniques to search through it's

memory fast.
 Searching in the data structure can be done by implementing

searching algorithms to check for or retrieve an element from


any form of stored data structure.
Why do we need searching algorithms?
 We often need to find one particular item of data amongst many

hundreds, thousands, millions or more.


 For example, you might need to find someone’s phone number

on your phone, or a particular business’s address in the UK.


 This is why searching algorithms are important.
 Without them you would have to look at each item of data – each

phone number or business address – individually, to see whether


it is what you are looking for.
 In a large set of data, it will take a long time to do this.
 Instead, a searching algorithm can be used to help find the item

of data you are looking for.


Continued…

 Search algorithms prevent you from having to look through lots


of data to find the information you are searching for
Searching Techniques in Data Structure
 1. Sequential Search

 This is the traditional technique for searching an

element in a collection of elements.


 In this type of search, all the elements of the list are

traversed one by one to find if the element is present in


the list or not.
 One example of such an algorithm is a linear search.

 This is a straightforward and basic algorithm. Suppose

ARR is an array of n elements, and we need to find


location LOC of element ITEM in ARR. For this, LOC is
assigned to -1, which indicates that ITEM is not present
in ARR. While comparing ITEM with data at each ARR
location, and once ITEM == ARR[N], LOC is updated
with location N+1. Hence we found the ITEM in ARR.
Algorithm:
LinearSearch(array, key)
for each item in the array
if item == value
return its index
How it works?
 The following steps are followed to search for an element k =

1 in the list below.


 Start from the first element, compare k with each element x.
 If x == k, return the index.

 Else, return not found.


Code
// Linear Search
int search(int array[], int n, int x) {
// Going through array sequencially
for (int i = 0; i < n; i++)
if (array[i] == x)
return i;
return -1;
}
Complexity of Sequential Search
Here are the complexities of the linear search given below.
 Space complexity

◦ As linear search algorithm does not use any extra space, thus its space
complexity = O(n) for an array of n number of elements.
 Time Complexity
◦ Worst-case complexity: O(n) – This case occurs when the search
element is not present in the array.
◦ Best case complexity: O(1) – This case occurs when the first element is
the element to be searched.
◦ Average complexity: O(n) – This means when an element is present
somewhere in the middle of the array.

Linear Search Applications


 For searching operations in smaller arrays (<100 items).
Binary Search
 This is a technique to search an element in the list using the

divide and conquer technique.


 This type of technique is used in the case of sorted lists.
 Instead of searching an element one by one in the list, it directly

goes to the middle element of the list, divides the array into 2
parts, and decides element lies in which sub-array the element
exists.
 Suppose ARR is an array with sorted n number of elements

present in increasing order.


 With every step of this algorithm, the searching is confined

within BEG and END, which are the beginning and ending index
of sub-arrays.
 The index MID defines the middle index of the array where,
Binary Search Working
 Binary Search Algorithm can be implemented in two ways which

are discussed below.


◦ Iterative Method
◦ Recursive Method
 The recursive method follows the divide and conquer approach.
The general steps for both methods are discussed below
1. The array in which searching is to be performed is:

Let x = 4 be the element to be searched.


2. Set two pointers low and high at the lowest and the highest
positions respectively.
3. Find the middle element mid of the array ie. arr[(low + high)/2]
= 6.

4. If x == mid, then return mid. Else, compare the element to be


searched with m.
5. If x > mid, compare x with the middle element of the elements
on the right side of mid. This is done by setting low to low = mid
+ 1.
6. Else, compare x with the middle element of the elements on the
left side of mid. This is done by setting high to high = mid - 1.
 Repeat steps 3 to 6 until low meets high.

 x = 4 is found.
Binary Search Algorithm
Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid – 1
Recursive Method
binarySearch(arr, x, low, high)
if low > high
return 0
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid] // x is on the right side
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side
return binarySearch(arr, x, low, mid - 1)
Iterative Method
int binarySearch(int array[], int x, int low, int high)
{ // Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
} return -1;
}
Recursive Method
int binarySearch(int array[], int x, int low, int high)
{
if (high >= low)
{
int mid = low + (high - low) / 2;
// If found at mid, then return it
if (array[mid] == x)
return mid;
// Search the left half
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1);
// Search the right half
return binarySearch(array, x, mid + 1, high);
}
return -1;
}
Binary Search Complexity
 Time Complexities

◦ Best case complexity: O(1)


◦ Average case complexity: O(log n)
◦ Worst case complexity: O(log n)
 Space Complexity

◦ The space complexity of the binary search is O(1).

Binary Search Applications


 In libraries of Java, .Net, C++ STL
 While debugging, the binary search is used to pinpoint the place

where the error happens.


Hashing
 Hashing in the data structure is a technique of mapping a large

chunk of data into small tables using a hashing function.


 It is also known as the message digest function.
 It is a technique that uniquely identifies a specific item from a

collection of similar items.


 Simply, It is the technique of representing longer records by

shorter values called key


 The keys are placed in a table called hash table where the keys

are compared for finding the records.


 It uses hash tables to store the data in an array format. Each
value in the array has assigned a unique index number.
 Hash tables use a technique to generate these unique index

numbers for each value stored in an array format. This technique


is called the hash technique.
Hashing Approach
 It is used for storing and retrieving data in O(1) time.

Terminologies
 Search Key (24, 52, 91, 67, 48, 83) : We place these search key

values in hash table on the basis of which data are searched.


 Hash Table : It is simply an array that is address via a hash

function. A table of some fixed size to hold a collection of


records each uniquely identified by some key.
Hash Function (k mod 10, k mod n, folding method, mid-square
method)
◦ The basic idea in hashing is the transformation of a key into
corresponding location in the hash table
◦ A hash function can be defined as a function that takes key as
input and transforms it into a hash table index
Suppose we have hash function (k mod 10)
While inserting search key values in table
We get : 24 mod 10 = 4,
52 mod 10 = 2,
91 mod 10 = 1,
67 mod 10 = 7,
48 mod 10 = 8
83 mod 10 = 3
 Lets we have data 62 also in the search key list then we get 62
mod 10 = 2, where 52 is already stored then what can be done?
 This is the case of collision, how to solve it?

Collision Resolution Technique


Collision in hashing
Let us take an example with hash function (k mod 6)
Search key values are : 24,19,32,44
Hash table: We get 24 mod 6 = 0
19 mod 6 = 1
24 32 mod 6 = 2
0 19 44 mod 6 = 2
1 32 Here 44 needs 2nd index 2
which is already 3 occupied means this is
4 case of collision.
Collision Resolution Techniques
 Open Hashing
 Chaining
In this case we have to chain the element 44 from above example with 32
as given below :
24
19
32 44

 Closed Hashing (Open Addressing)


◦ Linear Probing
◦ Quadratic Probing
◦ Double Hashing
Linear Probing
 In this case next available space will be occupied by the new
element, if next space is also fulfilled with another value then
another next empty space will be used to store new element.
 Example we have 44 to insert then 44 mod 6 =2, here index 2 is
already fulfilled with 32, so next available space will be used as
shown below

Quadratic Probing
 In this case we use (h+i^2 mod n)
 Example: If we have new value 30 then 30 mod 6 = 0, it mean
oth index should be used to store 30, but this index is already
fulfilled with 24. Then h(value) = 0
 Then again we use (h+i^2 mod n), here i represents how many
times we checked the space where 30 should be stored.
 Here i=2 then we get : 0+1^2 mod 6 =1, 1 index is already
fulfilled with 19 then prob(i) will be increased by 1, we get
i=2, then again
h+i^2 mod 6 = 0+2^2 mod 6=4, 4th index is available so, 30 can
be inserted in this location.

You might also like