Skip to main content

Binary Search on Integers / Names77

BinarySearch

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void main()

{

int a[20],low,mid,high,i,n,k,ch;

char b[20][20],key[20];

printf("1. binary search for integer\n2. Binary search for string/name\n");

printf("Enter your choice:\t");

scanf("%d",&ch);

switch(ch)

{

case 1: /* For integers */

            printf("Enter the number of elements in ascending order \n");

            scanf("%d",&n);

           

            printf("Enter the elements\n");

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

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

            printf("Enter the element to be searched\n");

            scanf("%d",&k);

            low = 0;

            high = n - 1;

            while(low <= high)

            {

            mid = (low + high)/2;

            if(a[mid] == k)

            {

                        printf("Successful search, element %d found at %d\n",k,mid+1);

                        exit(0);

            }

            else if(k < a[mid])

            {

                        high = mid - 1;

            }

            else

            {

                        low = mid + 1;

            }

            }

            printf("Unsuucessful search\n");

 

            break;

case 2: /* For strings */

            printf("Enter the number of Names\n");

            scanf("%d",&n);

 

            printf("Enter the Names in ascending order \n");

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

            scanf("%s",b[i]);

            printf("Enter the Name to be searched\n");

            scanf("%s",key);

            low=0;

            high=n-1;

            while(low <= high)

            {

           

            mid=(low + high)/2;

            if(strcmp(key,b[mid])==0)

            {

                        printf("Successful search, Name  %s found at %d\n",key,mid+1);

                        exit(0);

            }

            else if(strcmp(key,b[mid])<0)

            {

                        high=mid-1;

            }

            else

            {

                        low=mid+1;

            }

            }

            printf("Unsuucessful search\n");

            break;

default: printf("Invalid choice\n");

}

}

BinarySearchalg

Start

Read Choice

If choice is 1 goto step A

Else if choice is 2 goto step B

Else print invalid choice

A:        Read n

             for i = 0 to n – 1

                   read a[i]

             Read key

low = 0, high = n – 1

while ( low<=high)

mid= (low + high)/2

                           if (key == a[mid])

found = 1

goto step C

 else if (key > a[mid])

low = mid+ 1

 else

high = mid – 1

end while

C:         if (found) 

print “element is found at location”  goto D

else

print “element not found” goto D

B:         Read n

             for i = 0 to n – 1

                   read str[i]

             Read key

low = 0, high = n – 1

while (high <= low)

mid= (low + high)/2

c = strcmp(key,str[i])

                           if (c == 0)

found = 1

goto step E

 else if (key > a[mid])

low = mid+ 1

 else

high = mid – 1

end while

E:         if (found) 

print “element is found at location”  goto D

else

print “element not found” goto D

D:        Stop

Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are quarantined to prevent the spread of bird flu.

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are  quarantined  to prevent the spread of bird flu. immunized butchered secluded CORRECT mingled