0% found this document useful (0 votes)
49 views

Binary Search Algo Explained

The document explains the binary search algorithm through four iterations of searching for the value 73 in a sorted array of numbers, tracking the first and last index in each iteration and narrowing the search range until the value is found in the fourth iteration. It also provides code examples for implementing binary search on an array as well as for common binary tree operations like search, insert, and delete.

Uploaded by

Chian Soonkai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Binary Search Algo Explained

The document explains the binary search algorithm through four iterations of searching for the value 73 in a sorted array of numbers, tracking the first and last index in each iteration and narrowing the search range until the value is found in the fourth iteration. It also provides code examples for implementing binary search on an array as well as for common binary tree operations like search, insert, and delete.

Uploaded by

Chian Soonkai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Written by Dr.

Mohd Zamri Osman

BINARY SEARCH ALGORITHMS EXPLAINED

index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
data[] 9 16 20 32 39 43 51 52 69 73

Assume the search key is 73

first = 0
last = 9

ITERATION 1: The first index is smaller and equal to the last index

mid = (0+9)/2; // mid = 4

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

Since the search key is not match to data[mid], then compare if 73


is smaller than 69. 73 is greater than data[mid], modify first as
first = mid + 1 = 8 + 1 = 9

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

The value of first, last, mid and number of comparison

iteration first last Mid last[mid] # comparisons


1 0 9 4 39 2
2 5 9 7 52 2
3 8 9 8 69 2
4 9 9 9 73 1 (found)

Total number of comparison is: 7

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;
}

printf("\n\nSorry, no match found !");


printf("\nNumber of comparison: %d",count);
return;
}
Written by Dr. Mohd Zamri Osman

Binary Tree Code:


#include <stdio.h>
#include <stdlib.h>

struct btree_node
{
int number;
struct btree_node *left,*right;
};

struct btree_node *rootptr,*newptr,*currentptr,*previousptr,*tmpptr;

struct btree_node* SEARCH(struct btree_node *,int);


void INSERT(int);
void INORDER (struct btree_node *);
void DELETE (int);
struct btree_node* COPY(struct btree_node *);

void main ()
{

int element,choice;
rootptr=NULL;

while (choice !=0)


{
printf("\n\n MENU ");
printf("\n 0: Quit");
printf("\n 1: Search");
printf("\n 2: Insert");
printf("\n 3: Display In Order");
printf("\n 4: Display Pre Order");
printf("\n 5: Display Post Order");
printf("\n 6: Delete by copying");
printf("\n\n Enter your choice: ");
scanf("\t%d",&choice);

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;

case 2 : printf ("\n Insert");


printf("\n Enter an integer number: ");
scanf("%d",&element);
INSERT(element);
printf ("\n Insertion is successful");
break;

case 3 : printf("\n Display number in In order \n\n");


INORDER(rootptr);
break;
Written by Dr. Mohd Zamri Osman

case 4 : printf("\n Display number in pre order \n\n");


PREORDER(rootptr);
break;

case 5 : printf("\n Display number in post order \n\n");


POSTORDER(rootptr);
break;

case 6 : printf ("\n Delete BY COPY");


printf("\n Enter a number to delete: ");
scanf("%d",&element);
DELETE(element);
break;

default: printf("\n Please enter number only from the MENU");

}
}

}
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);
}
}

void INSERT(int number)


{
newptr = (struct btree_node*) malloc (sizeof(struct btree_node));
newptr->left = NULL;
newptr->right = NULL;
newptr->number = number;

currentptr =rootptr;
while (currentptr!=NULL) //search the location to insert
{
previousptr = currentptr;

if (number > currentptr->number)


currentptr = currentptr->right;
else
currentptr = currentptr->left;
}

if (rootptr==NULL)
rootptr = newptr;
else if (number > previousptr->number)
Written by Dr. Mohd Zamri Osman

previousptr->right = newptr;
else
previousptr->left = newptr;
}

void INORDER (struct btree_node *node)


{
if (node!=NULL)
{
INORDER(node->left);
printf("\n -- %d",node->number);
INORDER(node->right);
}

struct btree_node* SEARCH(struct btree_node *node,int number)


{
if(node!=NULL)// node is not null
{
if(number == node->number)
return node;

else if(number > node->number)


return SEARCH(node->right,number);

else
return SEARCH(node->left,number);
}

else
return NULL;

void DELETE(int number)


{
currentptr = rootptr;

while (currentptr!=NULL)//search the node to delete


{
if (currentptr->number == number)
break;

previousptr = currentptr;//previousptr hold the address of node to


delete

if (number > currentptr->number)


currentptr = currentptr->right;
else
currentptr = currentptr->left;
}

if (currentptr==NULL)
{ printf ("\n Number %d is not in the tree!!!",number);
return;
}

else if (currentptr->number == number)


{ if (currentptr == rootptr)
rootptr=COPY(rootptr);
Written by Dr. Mohd Zamri Osman

else if (previousptr->left == currentptr)


previousptr->left=COPY(previousptr->left);
else
previousptr->right=COPY(previousptr->right);
}

struct btree_node* COPY (struct btree_node *node)


{
struct btree_node *previousptr2;
tmpptr=node;

if(node->right==NULL)//no right node


node = node->left;

else if (node->left==NULL)//no left node


node = node->right;

else
{
tmpptr = node->left;
previousptr2 =node;
while (tmpptr->right!=NULL) //find the rightmost node of the left
subtree
{
previousptr2 = tmpptr;
tmpptr = tmpptr->right;
}

node->number = tmpptr->number;//copy the element to node

if (previousptr2 == node)
previousptr2->left = tmpptr->left;

else
previousptr2->right = tmpptr->left;
}

free(tmpptr);
return node;
}

You might also like