Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

binary search

starman_68starman_68 Posts: 8Member
this is wat i hve done so far, i have completed the binary search and the bubblesort. i need a little help with the sequential search.




#include
#include

#define SIZE 10

void bubblesort (int [],int);
void binary (int [] , int);
//void sequential (int [], int );

main ()
{
int n[SIZE],i;


printf ("Enter %d integers
",SIZE);

for (i = 0; i < SIZE; i++)
scanf ("%d",&n[i]);

bubblesort (n, SIZE);

binary (n,SIZE);

//sequential (n,SIZE);

return 0;
} //end of main



void bubblesort (int m [], int size)
{
int hold,c,j;

for (c = 0;c < size - 1;c++)
for (j = 0;j < size - 1;j++)
//sort by swaping
if (m[j] > m[j +1]){
hold = m[j];
m[j] = m[j + 1];
m[j + 1] = hold;
}

printf ("%s
%s
%s
","**********","bubblesort","**********");

for (c = 0; c < size; c++)
printf ("%4d",m[c]);

printf ("
");

}//end of buble

void binary (int m[],int size)
{
int mid,found,key,i;

found = 0;
mid = size / 2;

printf ("Enter searchkey
");
scanf ("%d",&key);
//search for key
for (i = 0;i < size;i++){
if (key == m[mid])//check if key is the middle number
found = mid;
else if (key > m[mid]) //if key is greater than middle num search upper half
mid++;
else //else search lower half
mid--;
}

printf ("%s
%s
%s
","**********","binary search","**********");
if (found == 0)
printf ("%d not found
",key);
else
printf ("%d found in element %d

",key,mid);
} //end of binary


void sequential (int m[], int size)
{
int key;

printf ("Enter searchkey
");
scanf ("%d",&key);


Comments

Sign In or Register to comment.