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

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

  • stoberstober 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]
  • starman_68starman_68 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

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

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

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

    Read here Binary Search in C Programming

Sign In or Register to comment.