0% found this document useful (0 votes)
27 views2 pages

Preern210008439 117 2023 02 10

Uploaded by

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

Preern210008439 117 2023 02 10

Uploaded by

Anshika thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
‘S.No:2 Exp. Name: Write the code to implement Binary Search Date:2022-09-25 ‘Aim: Write a program to implement Binary Search Theory: In computer science, binary search, also known as half-interval search, logarithmic search, or binary g chop, is @ search algorithm that finds the position of a target value within a sorted array. Binary search é Compares the target value to the middle element of the array. 3 3 Algorithm: 3 1. Find the midpoint of the array; this will be the element at arrfsize/2]. The midpoint divides the array 5 into two smaller arrays: the lower half of the array consisting of elements 0 to midpoint - 1, and the Z upper half of the array consisting of elements midpoint to size-1 a = 2. Compare key to arrlmidpoint] by calling the user function cmp_proc. 3. If the key is a match, return arrlmidpoint| otherwise 4, Ifthe array consists of only one element return NULL indicating that there is no match; otherwise 5. If the key is less than the value extracted from arrlmidpoint] search the lower half of the array by recursively calling search; otherwise 6. Search the upper half of the array by recursively calling search. Source Code: [ woven #include int main() 4 g seater: 5 printf("Enter the total number of elements: a scanf ("Xd 8a); B int num(a}: 8 printf("Enter the element in sorted form: "); iS for (int i=O;i 8 printf("Enter the element which you want to search: 2 scanf("%d", &key); 5 int low=0,high=a-1, flag: S while (low <= high) a { @ Low+high)/2; um mid] ) printf("The number %d is found in the list at the position %d\n",num [mid] ,mid+1); flag = 1; high = mid - 1 } else{ low = mide1; } if(flag==0) printf("Element not found\ return 0; Execution Results - alrtest cases have succeeded! Test Case-1 jser Output Enter the total number of elements: 7 Enter the element in sorted form: 11 22 33 44 55 66 77 Enter the element which you want to search: 55, [The nunber 55 is found in the list at the position 5 Test Case-2 jser Output Enter the total number of elements: 5 Enter the element in sorted form: 56 98 118 245 3337 Enter the element which you want to search: 69 Element not found Page No: 2 : : £ '2021-3628-CSE Ad ITS Engineering College

You might also like