Howdy, Stranger!

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

binary search

Member Posts: 8
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)
",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);

• Member Posts: 9,765 ✭✭✭
: 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.
:
:

That's the easiest part! a simple loop
[code]
for( int i = 0; i < size; i++)
{
if( array[i] == what_i_am_looking_for)
{
// found it!

}
}
[/code]
• Member Posts: 8
: :
:
: for( int i = 0; i < size; i++)
: {
: if( array[i] == what_i_am_looking_for)
: {
: // found it!
:
: }
: }
:

isnt that linear search. are they one in the same

• Member Posts: 9,765 ✭✭✭
:
: isnt that linear search. are they one in the same
:
:

yes -- they are the same thing.
• Member Posts: 8
: :
: :: :
: :
:
: yes -- they are the same thing.
:
THANKS O.K.
• Member Posts: 67
edited October 2013

.....................
......................
Good thank you.

Read here Binary Search in C Programming