Searhing Techniques Data Structure
Searhing Techniques Data Structure
Searching Techniques
Poonam Dhand(Assistant Prof., Computer Dept., GCG Ludhiana)
Paramjit Kaur(Assistant Prof., Computer Dept., GCG Ludhiana)
Searching is the process of locating given value position in a list of values, i.e. search
is a process of finding a value in a list of values. Search is said to be successful if the
element being searched is found or unsuccessful when the element being searched is
not found. A search typically answers in either True or False as to whether the item is
present. On occasion it may be modified to return where the item is found. Two
techniques are used for searching:
A linear search is the basic and simple search algorithm. In Linear search, a
sequential search is made over all elements one by one. In other words linear search
searches an element or value from an array till the desired element is not found in
sequential order. Every element is checked and if it is found then that particular item
is returned, otherwise the search continues till the end of the data collection.
Searching begins from first element and continues until the desired element is found
or end of the file is reached. Linear Search is applied on the unsorted or unordered list
when there are fewer elements in a list.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 1: It compares the x with first element located at index 0.Element is not matched
then it moves to the next element.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 2: It compares the x with next element located at index 1. Element is not
matched then it moves to the next element.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 3: It compares the x with next element located at index 2. Element is not
matched then it moves to the next element.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 4: It compares the x with next element located at index 3. Element is not
matched then it moves to the next element.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 5: It compares the x with next element located at index 4. Element is not
matched then it moves to the next element.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Step 6: It compares the x with next element located at index 5. The desired element 85
is found at 5 location. Here the process is stopped and terminate the function.
23 55 11 34 39 85 57 34 56 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Note: If the last element in the list is also doesn't match, then display "Element not
found" and terminate the function.
Algorithm
Linear Search ( A, x)
// Given an array A[i :x] and x represent number of elements
Set found to false
Set position to -1
Set index to 0
While found is false and index < x
If A[index] is equal to search value
found = true
position = index
End if
Add 1 to index
End while
Return position
Pseudocode
int index = 0;
//Position of search value
int position = -1;
//Indicate whether the value is found
bool found = false;
found = true;
position = index;
}
index++;
}
return position;
}
end procedure
Program:
Output:
Disadvantages:
The primary disadvantage of linear search is that it has a very poor O(n)
general efficiency. That is, the performance of the algorithm scales
linearly with the size of the input.
It is slow and time consuming
Binary search is the most popular Search algorithm based on Divide and
conquer technique. It works on a sorted array list either in ascending or
descending order. In this method firstly find the middle element and to search an
element compare the value with the elements in the middle position of the array. If the
value is matched, then search is successful otherwise list is divided into two
halves(one from the first element to the middle element i.e. first half or lower half and
second half or upper half from middle to last element . If the value is less than the
middle element, then it must lie in the lower half of the array and if it's greater than
the element then it must lie in the upper half of the array. We repeat this procedure on
the lower (or upper) half of the array. Binary Search is useful when there are large
numbers of elements in an array.
Example:
Suppose we are given a sorted array of 10 elements. We are to search element
72. i.e. X = 72.
11 18 23 34 39 46 57 69 72 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
11 18 23 34 39 46 57 69 72 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
11 18 23 34 39 46 57 69 72 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
11 18 23 34 39 46 57 69 72 81
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Now X=Mid. The element is found and the search terminated in 3 steps.
Algorithm
BinSearch(a, i, n, x)
// Given an array a[i :n] of elements in nondecreasing order, 1 ≤ i ≤ J, determine
// whether x is present, and if so, return j such that x — a[j]; else return 0.
{
if (n = i) then // If Small(P)
{
if (x = a[i]) then return n;
else return 0;
}
else
{ // Reduce P into a smaller subproblem.
mid := [(i + n/2)];
if (x = a[mid]) then return mid;
else if (x < a[mid]) then
return BinSearch(a, i, mid - 1, x );
else return BinSearch(a, mid +1, n, x);
}
}
Explanation: Above algorithm has four inputs an array a[ ], first element i, last
element n and the element to be searched x. It recursively calculate the middle
element and check the presence of x.
Pseudocode
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Program:
/* implementation of the binary search*/
#include<stdio.h>
#include<conio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
first = 0;
last = size - 1;
middle = (first+last)/2;
Advantages
The binary search is much more efficient than the linear search. Every time it
makes a comparison and fails to find the desired item, it eliminates half of the
remaining portion of the array that must be searched.
Binary search can have random access to the data but linear search only requires
sequential access.
Disadvantage
QUESTIONS
MULTIPLE CHOICE QUESTIONS
1. Which of the following is not the required condition for binary search
algorithm?
a) The list must be sorted
b) There should be the direct access to the middle element in any sub list
c) There must be mechanism to delete and/or insert elements in list.
d) Number values should only be present
2. The Average case occurs in linear search algorithm.
a) when item is somewhere in the middle of the array
b) when item is not the array at all
c) when item is the last element in the array
d) Item is the last element in the array or item is not there at all
3. Binary search algorithm cannot be applied to.
a) Sorted linked list
b) Sorted Binary tree
c) Sorted Linear Array
d) Pointer Array
4. Complexity of linear search algorithm is:
a) O(n)
b) O(log n)
c) O(n2)
d) O(n log n)
5. Finding the location of a given item in a collection of items is called.
a) Discovering
b) Finding
c) Searching
d) Mining
TRUE/FALSE
1. Finding the location of a given item in a collection of items is called searching.
2. The binary search is much more efficient than the linear search.
3. Linear search algorithm is used for large array list elements .
4. Complexity of binary search algorithm in worst and average case is O(n).
5. Comparisons are necessary and sufficient for computing both the minimum
and the maximum is 3n-3/2.
Answers (True/False)
1. (T) 2. (T) 3. (F) 4. (F) 5. (T)
Ans: Advantages
The binary search is much more efficient than the linear search. Every time it
makes a comparison and fails to find the desired item, it eliminates half of the
remaining portion of the array that must be searched.
Binary search can have random access to the data but linear search only requires
sequential access.
Disadvantage
The time complexity of binary search algorithm in best case is O(1) and in average
and worst case is O(log(n)).
EXERCISE
Q1. Search the element given array(24,13,56,35,46,27,78,48) using binary
search.
Q2. Define searching. Explain different types of searching.