The document outlines a lab program for BCA students to implement Linear and Binary search algorithms to find the location of the number 7 in a given array. It includes C code for both search methods, allowing users to input the size of the array and the key element to search for. The program provides options to choose between Linear and Binary search, displaying the position of the found element or indicating if it is not found.
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 ratings0% found this document useful (0 votes)
4 views2 pages
Prog 1
The document outlines a lab program for BCA students to implement Linear and Binary search algorithms to find the location of the number 7 in a given array. It includes C code for both search methods, allowing users to input the size of the array and the key element to search for. The program provides options to choose between Linear and Binary search, displaying the position of the found element or indicating if it is not found.
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/ 2
Department of BCA Data Structure LAB Program No:_______
PROGRAM 01: Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear and Binary search and also display its first occurrence. PROGRAM CODE:
// Linear and Binary Search of the elements given by user
#include<stdio.h> int linear(int key,int n,int a[]) { int i; for(i=0;i<n;i++) { if(key==a[i]) return i+1; } return 0; } int binary(int key,int a[],int low,int high) { int mid; if (low>high) return 0; mid=(low+high)/2; if(key==a[mid]) return mid+1; if(key<a[mid]) return binary(key,a,low,mid-1); else return binary(key,a,mid+1,high); } void main() { int i,a[20],pos,key,n,choice,low=0,high=n-1; printf("enter the size of the array\n"); scanf("%d",&n); printf("enter the element of the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter the key element\n"); scanf("%d",&key); while(1) { ACHARYA INSTITUTE OF GRADUATE STUDIES. Page 1 Department of BCA Data Structure LAB Program No:_______
printf("\n1:linear 2:binary 3.Exit\n");
printf("enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: pos=linear(key,n,a); break; case 2: pos=binary(key,a,0,n-1); break; case 3:exit(0); break; default: printf("enter the valid choice\n"); break; } if(pos==0) printf("%d not found\n",low); else printf("Element found at %d",pos,high); } } OUTPUT: