Howdy, Stranger!

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


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.


#define SIZE 10

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

main ()
int n[SIZE],i;

printf ("Enter %d integers

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

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
else //else search lower half

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

} //end of binary

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

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


Sign In or Register to comment.