(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
Searching in DSA that follow a dsa searching.pptx
1. : Mrs Keerti Shrivastava ma’am_
(Ass. Professor of CSA)
2. Searching…?
Searching in data-strucutre refers to the process of finding a desired
element in set of items. The desired element is called "target". The set of
items to be searched in, can be any data-structure like − list, array,
linked-list, tree or graph.
3. Searching Algorithm…
Linear or Sequential Search :
A linear search or sequential search is a method for finding an element within a
list. It sequentially checks each element of the list until a match is found or the
whole list has been searched.
Linear
Search
Binary
Search
4. Algorithm…
The basic steps to perform Binary Search are:
•Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
•If x matches with an element, return the index.
•If x doesn’t match with any of the elements, return -1.
5. Program…
#include <stdio.h>
int search(int arr[], int N, int x)
{
int i;
for (i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver's code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Element is present at index 3
Output
6. Searching Algorithm…
Binary Search :
Binary Search is a searching algorithm for finding an element's
position in a sorted array. In this approach, the element is always
searched in the middle of a portion of an array. Binary search can be
implemented only on a sorted list of items. If the elements are not
sorted already, we need to sort them first.
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).
7. 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.
8. Program…
// C program to implement recursive Binary Search
#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;
Output
Element is present at index 3