0% found this document useful (0 votes)
8 views

searching in data tructure

The document explains two searching algorithms: Linear Search and Binary Search. Linear Search sequentially checks each element until the desired one is found, while Binary Search efficiently narrows down the search in a sorted array by dividing the interval in half, achieving a time complexity of O(Log n). It also provides code examples for both iterative and recursive implementations of Binary Search.

Uploaded by

vinip3127
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

searching in data tructure

The document explains two searching algorithms: Linear Search and Binary Search. Linear Search sequentially checks each element until the desired one is found, while Binary Search efficiently narrows down the search in a sorted array by dividing the interval in half, achieving a time complexity of O(Log n). It also provides code examples for both iterative and recursive implementations of Binary Search.

Uploaded by

vinip3127
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Linear Search

Linear Search is defined as a sequential search algorithm that starts at one end and goes through each
element of a list until the desired element is found, otherwise the search continues till the end of the
data set. It is the easiest searching algorithm
#include <stdio.h>
int search(int array[], int n, int x)
{
// Going through array sequentially
for (int i = 0; i < n; i++)
if (array[i] == x)
return i;
return -1;
}

Binary Search
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search
interval in half. The idea of binary search is to use the information that the array is sorted and reduce
the time complexity to O(Log n).
Binary Search Algorithm: The basic steps to perform Binary Search are:
 Begin with the mid element of the whole array as a search key.
 If the value of the search key is equal to the item then return an index of the search key.
 Or if the value of the search key is less than the item in the middle of the interval, narrow the
interval to the lower half.
 Otherwise, narrow it to the upper half.
 Repeatedly check from the second point until the value is found or the interval is empty.

Iteration Method
#include <stdio.h>
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:
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

You might also like