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

1.to Perform Menu Driven Code For Searching Techniques

This program implements linear and binary search functions in a menu-driven code. The user is prompted to choose between linear and binary search. For linear search, the user enters the size and elements of an unsorted array, and the program searches for a given input number. For binary search, the user enters the size and sorted elements of an array, and the program searches for the input number using binary search logic. The program outputs whether the number is found or not found.

Uploaded by

Shruti Ninawe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
806 views

1.to Perform Menu Driven Code For Searching Techniques

This program implements linear and binary search functions in a menu-driven code. The user is prompted to choose between linear and binary search. For linear search, the user enters the size and elements of an unsorted array, and the program searches for a given input number. For binary search, the user enters the size and sorted elements of an array, and the program searches for the input number using binary search logic. The program outputs whether the number is found or not found.

Uploaded by

Shruti Ninawe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

To perform menu driven code for searching techniques :-

 Linear searching
 Binary searching

Code:-

/*write a menu driven program to do the searching*/

#include<stdio.h>

void lsearch (int[], int);

void bsearch (int[], int);

int

main ()

int ch, ch1, m, n;

int a[10], b[10], i;

do

printf ("\nEnter your choice :");

printf ("\n1. linear search");

printf ("\n2. binary search");

scanf ("\n%d", &ch);

switch (ch)

case 1:

printf ("\n Enter Array size : ");

scanf ("%d", &m);

for (i = 0; i < m; i++)

printf ("\n Enter array element : ");

scanf ("%d", &a[i]);

}
lsearch (a, m);

break;

case 2:

printf ("\nEnter Array size : ");

scanf ("%d", &n);

printf ("\n Enter array element in sorted order : ");

for (i = 0; i < n; i++)

scanf ("%d", &b[i]);

bsearch (b, n);

break;

default:

printf ("\n Wrong input !!! ");

printf ("\n Want to be continue press non zero no : ");

scanf ("%d", &ch1);

while (ch1 != 0);

void

lsearch (int a[], int m)

int num, i, f = 0;

printf ("\n Enter no to be search : ");

scanf ("%d", &num);

for (i = 0; i < m; i++)

if (a[i] == num)
{

printf ("\n item is found at index %d !!!", i);

f = 1;

break;

if (f == 0)

printf ("\n item is not found !!!");

void

bsearch (int b[], int n)

int beg = 0, num, mid, last;

last = n - 1;

printf ("\n Enter no to be search : ");

scanf ("%d", &num);

mid = (beg + last) / 2;

while (num != b[mid] && beg <= last)

if (num > b[mid])

beg = mid + 1;

else

last = mid - 1;

mid = (beg + last) / 2;

if (num == b[mid])

printf ("%d found at position %d\n", num, mid + 1);

if (beg > last)

printf ("%d not found in array\n", num);

}
Output:-
Enter your choice :

1. linear search

2. binary search1

Enter Array size : 5

Enter array element : 23

Enter array element : 14

Enter array element : 12

Enter array element : 15

Enter array element : 6

Enter no to be search : 15

item is found at index 3 !!!

Want to be continue press non zero no : 1

Enter your choice :

1. linear search

2. binary search2

Enter Array size : 4

Enter array element in sorted order : 2

6
8

Enter no to be search : 5

5 not found in array

Want to be continue press non zero no : 0

...Program finished with exit code 0

You might also like