Skip to main content

Implement Binary Search on Integers / Names Program and Algorithm Computer Science Engineering VTU

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

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...