1.3.2 Searching Operation On Array
1.3.2 Searching Operation On Array
2
Mission of the Department
4
Program Educational Objectives
PEO1 Demonstrate analytical and design skills including the ability to generate creative solutions and foster
team-oriented professionalism through effective communication in their careers.
PEO2 Expertise in successful careers based on their understanding of formal and practical methods of
application development using the concept of computer programming languages and design principles in
accordance to industry 4.0
PEO3 Exhibit the growth of the nation and society by implementing and acquiring knowledge of upliftment of
health, safety and other societal issues.
PEO4 Implement their exhibiting critical thinking and problem- solving skills in professional practices or tackle
social, technical and business challenges.
5
Program Specific Outcomes
6
Topic To be Covered
Step 1: Start from the first element (index 0) and compare key with each element (arr[i]).
Comparing key with first element arr[0]. SInce not equal, Comparing key with next element arr[1]. SInce not equal,
the iterator moves to the next element as a potential the iterator moves to the next element as a potential
match. match.
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm will yield a
successful message and return the index of the element when key is found (here 2).
Code:
// Linear Search in C++ int main()
{
#include <iostream>
int array[] = {2, 4, 0, 1, 9};
using namespace std; int x = 1;
int search(int array[], int n, int x) int n = sizeof(array) / sizeof(array[0]);
{ int result = search(array, n, x);
// Going through array if (result == -1)
sequencially {
• Average: O( n)
• Best: O(1)
• Worst: O( n)
The average time complexity is O( n) as the element to be found may
be present at the end of the list or may not be present at all. Linear
search has to visit all the elements until the needed element is found.
Algorithmically, this comes out to be O( n).
The best and worst case of a search algorithm will be O(1) and O( n)
respectively, as the element to be searched could always be found on
the first iteration or the last iteration.
Binary Search:
• Binary Search Algorithm is a searching algorithm
used in a sorted array by repeatedly dividing
the search interval in half.
• The linear search approach has one
disadvantage. Finding elements in a large array
will be time consuming. As the array grows, the
time will increase linearly. A binary search can be
used as a solution to this problem in some cases.
• The principle of binary search is how we find a
page in book. We open the book at a random
page in the middle and based on that page we
narrow our search to the left or right of the
book. Indeed, this only is possible if the page
numbers are in order.
Recursive Binary Search
Algorithm:
• Create a recursive function and compare the mid of the search space with the key. And based on
the result either return the index where the key is found or call the recursive function for the next
search space
• Algorithm:
BinarySearch(array, start, end , key)
1. Start
2.if start<=end then
mid=start+(end-start)/2
3. if array[mid]==key then
return mid location
4. else if array[mid]<key then
call BinarySerach(array, mid+1, end, key)
5. else when array[mid]>key then
call BinarySerach(array, start, mid-1, key)
6. else
return invalid location
7. Stop
How to calculate “mid” or Middle Element
Index in Binary Search?
First Step: Calculate the mid and compare the mid element with the key. If the key is less than mid
element, move to left and if it is greater than the mid then move search space to the right.
Key (i.e., 23) is greater than current mid element (i.e., 16). Key is less than the current mid 56. The search space moves
The search space moves to the right. to the left.
Second Step: If the key matches the value of the mid element, the element is found and stop search.
Code: (Implementation of Recursive Binary Search Algorithm)
if (arr[mid] == key) {
// Else the element can only be present in right subarray cout << "Element is present at index " << result;
} return 0;
} }
Complexity
• Time Complexity:
• Best Case: O(1)
• Average Case: O(log N)
• Worst Case: O(log N)
• Auxiliary Space: O(1), If the recursive call stack is considered then the
auxiliary space will be O(logN).
Iterative Binary Search
Algorithm:
• Here we use a while loop to continue the process of comparing the key and splitting the
search space in two halves.
• Algorithm:
1. Start
2. Set start=LB and end=UB, mid=int((start+end)/2)
3. Repeat step 4 to 8 while start<=end and A[mid]!=key
4.if key<=A[mid] then
set end=mid-1
5. else
set start=mid+1
6. set mid=int((start+end)/2)
7. if A[mid]==key
set loc=mid
8. else
set loc=Null
9. Stop
Code: (Implementation of Iterative
Binary Search Algorithm:)
// An iterative binary search function. // Driver code
int binarySearch(int arr[], int start, int end, int key) int main(void)
{ {
while (start <= end) { int arr[] = { 2, 3, 4, 10, 40 };
int mid = start + (end - start) / 2; int key = 10;
// Check if x is present at mid int n = sizeof(arr) / sizeof(arr[0]);
if (arr[mid] == key) int result = binarySearch(arr, 0, n - 1, key);
return mid; if(result == -1)
// If key greater, ignore left half
{
if (arr[mid] < key)
start = mid + 1; cout << "Element is not present in array";
// If key is smaller, ignore right half }
else else
end = mid - 1;
} {
// If we reach here, then element was not present cout << "Element is present at index " << result;
return -1; }
} return 0;
}
Complexity
https://www.youtube.com/watch?v=C46QfTjVCNU
THANK YOU