Lecture Searching
Lecture Searching
Linear Search
▪ Linear Search is the simplest searching algorithm.
▪ It traverses the array sequentially to locate the required element.
▪ It searches for an element by comparing it with each element of the array one by one.
▪ So, it is also called as Sequential Search.
}
3. if (i>n) print ” Not Found”;
4. Output: Found or Not Found.
Example
Consider the following list of elements and the element to be searched...
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
Search element 12
Step 1: Search element (12) is compared with first element (65)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 2: Search element (12) is compared with next element (20)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 3: Search element (12) is compared with next element (10)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 4: Search element (12) is compared with next element (55)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 5: Search element (12) is compared with next element (32)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 6: Search element (12) is compared with next element (12)
1 2 3 4 5 6 7 8
list 65 20 10 55 32 12 50 99
12
Both are matching. So, we stop comparing and display element found at index 6.
Binary Search
▪ Binary Search is one of the fastest searching algorithms.
▪ It is used for finding the location of an element in a linear array.
▪ It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
So, the elements must be arranged in-
▪ Either ascending order if the elements are numbers.
▪ Or dictionary order if the elements are strings.
To apply binary search on an unsorted array,
▪ First, sort the array using some sorting technique.
▪ Then, use binary search algorithm.
Algorithm for binary searching
1. Input A[1…..m], x; //A is the array with size m and x is the target element.
2. first=1, last=m
3. while(first<=last)
{
mid = (first+last)/2;
i. if (x=A[mid]), then print mid; //target element=A[mid] or target element is
//in index mid
break(stop searching);
ii. else if(x<A[mid]), then last=mid-1;
iii. else first=mid+1;
}
4. if(first>last), print “Not Found”;
5. Output: mid or “Not Found”;
Example
Consider the following list of elements and element 15 has to be searched in it using Binary Search
Algorithm.
1 2 3 4 5 6 7
3 10 15 20 35 40 60
Step-01:
▪ To begin with, we take first=1 and last=7.
▪ We compute location of the middle element as-
mid = (first + last) / 2 = (1 + 7) / 2 = 4
▪ Here, A[mid] = A[4] = 20 ≠ 15 and first < =last.
▪ So, we start next iteration.
Step-02:
▪ Since A[mid] = 20 > 15, so we take last = mid – 1 = 4 – 1 = 3 whereas beg remains
unchanged.
▪ We compute location of the middle element as-
mid = (first + last) / 2 = (1 + 3) / 2 = 2
▪ Here, A[mid] = A[2] = 10 ≠ 15 and first < =last.
▪ So, we start next iteration.
Step-03:
▪ Since A[mid] = 10 < 15, so we take first = mid + 1 = 2 + 1 = 3 whereas end remains
unchanged.
▪ We compute location of the middle element as-
mid = (first + last) / 2 = (3 + 3) / 2 = 3
▪ Here, A[mid] = A[3] = 15 which matches to the element being searched.
▪ So, our search terminates in success and index 3 is returned.