0% found this document useful (0 votes)
20 views7 pages

Algo Assignment-P2 Binary SRCH

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

Algo Assignment-P2 Binary SRCH

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

ALGORITHM PRACTICAL ASSIGNMENTS (SOLUTIONS)

GROUP-A: Searching

Assign- 1) Binary Search Method (The program should report the number of pass
& comparisons)
PROCEDURE
Binary search is the search technique that works efficiently on sorted lists.
Hence, to search an element into some list using the binary search technique,
we must ensure that the list is sorted.

Binary search follows the divide and conquer approach in which the list is
divided into two halves, and the item is compared with the middle element of
the list. If the match is found then, the location of the middle element is
returned. Otherwise, we search into either of the halves depending upon the
result produced through the match.

There are two methods to implement the binary search algorithm -


• Iterative method
• Recursive method

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 1


ALGORITHM

1. Binary_Search(a, lower_bound, upper_bound, val)


• 'a' is the given array,
• lower_bound is the index of the first array element,
• upper_bound is the index of the last array element,
• val is the value to search
2. Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
3. Step 2: repeat steps 3 and 4 while beg <=end
4. Step 3: set mid = (beg + end)/2
5. Step 4: if a[mid] = val
6. set pos = mid
7. print pos
8. go to step 6
9. else if a[mid] > val
10. set end = mid - 1
11. else
12. set beg = mid + 1
13. [end of if]
14. [end of loop]
15. Step 5: if pos = -1
16. print "value is not present in the array"
17. [end of if]
18. Step 6: exit

PROGRAM

/* Binary Search: Recursive way */


#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int com=0;
int BinarySearch(int a[], int beg, int end, int item)
{
int mid;
if(end >= beg)
{
com++;
mid = (beg + end)/2;
if(a[mid] == item)

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 2


return mid+1;
else if(a[mid] < item)
return BinarySearch(a,mid+1,end,item);
else
return BinarySearch(a,beg,mid-1,item);
}
return -1;
}

void main()
{
clrscr();
FILE *in;
int n, i, item, a[100], location=-1;
cout<<"Enter Sample Size (No of Elements):";
cin>>n;
printf("Enter the item which you want to search: ");
cin>>item;

in=fopen("Algo\\AInput.txt", "r");
i=0;
while(!feof(in))
{
fscanf(in, "%d", &a[i++]);
}
fclose(in);

location = BinarySearch(a, 0, n-1, item);


if(location != -1)
{
printf("Item found at location: %d",location);
cout<<"\n No of Comparison:"<<com;
}
Else
{
printf("Item not found");
cout<<"\n No of Comparison:"<<com;
}

getch();
}

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 3


/* Binary Search: Iterative Way */
#include <stdio.h>
#include<conio.h>

int BinarySearch(int arr[], int l, int r, int x)


{
while (l <= r)
{
int m = l + (r - l) / 2;

if (arr[m] == x)
return m+1;

else if (arr[m] < x)


l = m + 1;

else
r = m - 1;
}

// If we reach here, then element was not present


return -1;
}

void main(void)
{
clrscr();

FILE *in;
int n, i, item, a[100];
printf("Enter Sample Size (No of Elements):");
scanf("%d", &n);
printf("Enter the item which you want to search: ");
scanf("%d", &item);

in=fopen("AInput.txt", "r");
i=0;
while(!feof(in))
fscanf(in, "%d", &a[i++]);
fclose(in);

int result = BinarySearch(a, 0, n - 1, item);


(result == -1) ? printf("Element is not present in array")
: printf("Element is present at Location: %d", result);
getch();
}

OUTPUT

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 4


Input Set (Input.txt)
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Enter Sample Size (No of Elements): 100


Enter the item which you want to search:
Item found at location: fill accordingly
No of Comparison: fill accordingly

Enter Sample Size (No of Elements): 100


Enter the item which you want to search:
Item not found
No of Comparison: fill accordingly

DISCUSSION

There are two methods to implement the binary search algorithm -


• Iterative method
• Recursive method
The recursive method of binary search follows the divide and conquer approach.
Let the elements of array are -

Let the element to search is, K = 56


We have to use the below formula to calculate the mid of the array -
1. mid = (beg + end)/2
So, in the given array -
beg = 0
end = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 5


Now, the element to search is found. So algorithm will return the index of the
element matched.

TIME & SPACE COMPLEXITY

o Best Case Complexity - In Binary search, best case occurs when the
element to search is found in first comparison, i.e., when the first middle
element itself is the element to be searched. The best-case time
complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary
search is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when
we have to keep reducing the search space till it has only one element.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 6


The worst-case time complexity of Binary search is O(logn).

o The space complexity of binary search is O(1).

Literature Review

Advantages of Binary Search:


• Binary search is faster than linear search, especially for large arrays.
• More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
• Binary search is well-suited for searching large datasets that are stored in
external memory, such as on a hard drive or in the cloud.

Drawbacks of Binary Search:


• The array should be sorted.
• Binary search requires that the data structure being searched be stored in
contiguous memory locations.
• Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered.

Applications of Binary Search:


• Binary search can be used as a building block for more complex algorithms
used in machine learning, such as algorithms for training neural networks or
finding the optimal hyperparameters for a model.
• It can be used for searching in computer graphics such as algorithms for ray
tracing or texture mapping.
• It can be used for searching a database.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 7

You might also like