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

Lecture 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 8: Searching

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Linear Search

⚫ Linear search is a sequential searching algorithm where we start


from one end and check every element of the list until the desired
element is found. It is the simplest searching algorithm.

Algorithm
LinearSearch(array, key)
for each item in the array
if item == value
return its index
Linear Search
Linear Search
Implementation

#include <stdio.h> for (c = 0; c < n; c++)


int main() {
{ if (array[c] == search) /* If required element is found */
int array[100], search, c, n; {
printf("%d is present at location %d.\n", search, c+1);
printf("Enter number of elements in array: "); break;
scanf("%d", &n); }
}
printf("Enter %d integer: ", n); if (c == n)
printf("%d isn't present in the array.", search);
for (c = 0; c < n; c++)
scanf("%d", &array[c]); return 0;
}
printf("Enter a number to search: ");
scanf("%d", &search);
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

Recursive Method

Iteration Method binarySearch(arr, x, low, high)

do until the pointers low and high meet each other. if low > high

mid = (low + high)/2 return False

if (x == arr[mid]) else

return mid mid = (low + high) / 2

else if (x > arr[mid]) // x is on the right side if x == arr[mid]

low = mid + 1 return mid

else // x is on the left side else if x > arr[mid] // x is on the right side

high = mid - 1 return binarySearch(arr, x, mid + 1, high)

else // x is on the left side

return binarySearch(arr, x, low, mid - 1)


Binary
Search
Binary
Search
Binary
Search
Implementation (Iterative Method)

#include <stdio.h> int main(void) {


int array[] = {3, 4, 5, 6, 7, 8, 9};
int binarySearch(int array[], int x, int low, int high) { int n = sizeof(array) / sizeof(array[0]);
// Repeat until the pointers low and high meet each other int x = 4;
while (low <= high) { int result = binarySearch(array, x, 0, n - 1);
int mid = low + (high - low) / 2; if (result == -1)
if (array[mid] == x) printf("Not found");
return mid; else
if (array[mid] < x) printf("Element is found at index %d", result);
low = mid + 1; return 0;
else }
high = mid - 1;
}
return -1;
}
Implementation (Recursive Method)

#include <stdio.h>
int main(void) {
int binarySearch(int array[], int x, int low, int high) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
if (high >= low) {
int n = sizeof(array) / sizeof(array[0]);
int mid = low + (high - low) / 2;
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
// If found at mid, then return it
if (result == -1)
if (array[mid] == x)
printf("Not found");
return mid;
else
printf("Element is found at index %d", result);
// Search the left half
}
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1);

// Search the right half


return binarySearch(array, x, mid + 1, high);
}
return -1;
}
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy