Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views3 pages

Searching Programs

Uploaded by

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

Searching Programs

Uploaded by

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

Linear Search

#include <stdio.h>

#include <stdlib.h>

int main()

int array[50], search, c, n;

printf("enter number of elements in array\n");

scanf("%d", &n);

printf("enter %d integers\n", n);

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

scanf("%d", &array[c]);

printf("enter a number to search\n");

scanf("%d", &search);

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

if(array[c]== search)

printf("%d is present at location %d.\n", search, c+1);

break;

if(c==n)

printf("%d is not present in the array. \n", search);


return 0;

Binary Search

#include <stdio.h>

#include <stdlib.h>

int main()

int c, first, last, middle,n,search, array[100];

printf("enter number of elements in array\n");

scanf("%d", &n);

printf("enter %d integers\n", n);

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

scanf("%d", &array[c]);

printf("enter a number to search\n");

scanf("%d", &search);

first=0;

last=n-1;

middle =(first+last)/2;

while(first <= last)

if(array[middle]< search)

first = middle+1;

else if(array[middle]== search)


{

printf("%d found at location %d \n", search, middle+1);

break;

else

last = middle-1;

middle =(first +last)/2;

if(first > last)

printf("not found %d in the list.\n", search);

return 0;

You might also like