Binary Search Algo Explained
Binary Search Algo Explained
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
first = 0
last = 9
ITERATION 1: The first index is smaller and equal to the last index
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
first mid last
Since search key (73) is greater than data[mid] which is 39, then
the search area will be at right side. The value of first is modify
while last is fixed.
first = mid + 1 = 4 + 1 = 5;
search area
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
first last
ITERATION 2: The first index is still smaller and equal to the last
index
Since the first index is changed from 0 to 5 and the last index
remains the same:-
first = 5
last = 9;
calculate the mid as
mid = (5 + 9) / 2; // mid = 7
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
first mid last
Since the search key is not match to data[mid], then compare if (73
is smaller than data[mid]). Since 73 is greater than data[mid],
modify first as
Written by Dr. Mohd Zamri Osman
first = mid + 1 = 7 + 1 = 8
ITERATION 3: The first index is still smaller and equal to the last
index
Since the first index is changed from 5 to 8 and the last index
remains the same: -
first = 8
last = 9;
calculate the mid as
mid = (8 + 9) / 2; // mid = 8
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
first,
last
mid
ITERATION 4: The first index is still smaller and equal to the last
index
Since the first value is changed from 8 to 9 and the last value
remains the same:-
first = 9
last = 9;
calculate the mid as
mid = (9+9) / 2; // mid = 9
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73
First,
Last,
mid
Compare search key with data[mid] and the result is true. This means
that the search key is found, and the loop is terminated.
Written by Dr. Mohd Zamri Osman
Code:
void binary_search(int data[],int size, int searchValue)
{
int first=0;
int last=size - 1;
int mid;
int count=0;
while(first<=last)
{
count++;
mid=(first+last)/2;
printf("\nValue for mid: %d",mid);
if(searchValue==data[mid])
{ printf("\n\nSearch value found !");
printf("\nNumber of comparison: %d",count);
return;
}
else
if(searchValue<data[mid])
last=mid - 1;
else
first=mid + 1;
}
struct btree_node
{
int number;
struct btree_node *left,*right;
};
void main ()
{
int element,choice;
rootptr=NULL;
switch(choice)
{
case 1 : printf("\n SEARCH");
printf("\n Enter the searching number:");
scanf("%d", &element);
tmpptr = SEARCH(rootptr,element);
if(tmpptr!= NULL)
printf("\n The number %d is found",tmpptr->number);
else
printf("\n The number %d is not found", element);
break;
}
}
}
void PREORDER(struct btree_node *node)
{
if (node!=NULL)
{
printf("%d\n",node->number);
PREORDER(node->left);
PREORDER(node->right);
}
}
void POSTORDER(struct btree_node *node)
{
if (node!=NULL)
{
POSTORDER(node->left);
POSTORDER(node->right);
printf("%d\n",node->number);
}
}
currentptr =rootptr;
while (currentptr!=NULL) //search the location to insert
{
previousptr = currentptr;
if (rootptr==NULL)
rootptr = newptr;
else if (number > previousptr->number)
Written by Dr. Mohd Zamri Osman
previousptr->right = newptr;
else
previousptr->left = newptr;
}
else
return SEARCH(node->left,number);
}
else
return NULL;
if (currentptr==NULL)
{ printf ("\n Number %d is not in the tree!!!",number);
return;
}
else
{
tmpptr = node->left;
previousptr2 =node;
while (tmpptr->right!=NULL) //find the rightmost node of the left
subtree
{
previousptr2 = tmpptr;
tmpptr = tmpptr->right;
}
if (previousptr2 == node)
previousptr2->left = tmpptr->left;
else
previousptr2->right = tmpptr->left;
}
free(tmpptr);
return node;
}