binary search - Programmers Heaven

#### 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

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)
",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);

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

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

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

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

Read here Binary Search in C Programming