<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>binary search | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/binary-search/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 04:29:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>C Program for Binary Search and Towers of Hanoi using Recursion</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/c-program-for-binary-search-and-towers-of-hanoi-using-recursion/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/c-program-for-binary-search-and-towers-of-hanoi-using-recursion/#respond</comments>
		
		<dc:creator><![CDATA[Ansten Lobo]]></dc:creator>
		<pubDate>Sat, 09 Jun 2012 16:55:20 +0000</pubDate>
				<category><![CDATA[Advanced programs]]></category>
		<category><![CDATA[Towers of Hanoi]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[c program]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=3319</guid>

					<description><![CDATA[<p>C Program for Binary Search and Towers of Hanoi using Recursion. Source: Dr. G T Raju, Professor &#038; Head, Dept. of CSE, RNSIT main() { int n,a[50],key,opn,i,pos; do { clrscr(); printf(" \n\n Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit\n"); scanf("%d",&#038;opn); switch(opn) { case 1: printf(" How Many Elements?"); scanf("%d",&#038;n); printf("</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/c-advanced/c-program-for-binary-search-and-towers-of-hanoi-using-recursion/">C Program for Binary Search and Towers of Hanoi using Recursion</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>C Program for Binary Search and Towers of Hanoi using Recursion.<br />
Source: Dr. G T Raju, Professor &#038; Head, Dept. of CSE, RNSIT</p>
<pre lang="C" line="1">
main()
{
    int n,a[50],key,opn,i,pos;
    do
    {
        clrscr();
        printf(" \n\n Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit\n");
        scanf("%d",&opn);
        switch(opn)
        {
        case 1: printf(" How Many Elements?");
            scanf("%d",&n);
            printf(" Read all the elements is ASC order \n");
            for(i=1;i<=n;i++)
                scanf("%d",&#038;a[i]);
            printf(" Read the Key Element\n");
            scanf("%d",&#038;key);
            pos=BS(a,key,1,n);
            if(pos)	 printf(" Success: %d found at %d \n", key,pos);
            else	 printf(" Falure: %d Not found in the list ! \n",key);
            break;
        case 2: printf("\n\n How Many Disks ?");
            scanf("%d", &#038;n);
            printf("\n\n Result of Towers of Hanoi for %d Disks \n",n);
            tower(n,'A','B','C');
            printf("\n\n Note: A-> Source, B-> Intermediate, C-> Destination\n");
            break;
        case 3: printf(" Terminating \n"); break;
        default: printf(" Invalid Option !! Try Again !! \n");
        }
        printf(" Press a Key. . . ");  getch();
    }while(opn != 3);
}
int BS(int a[], int key,int low,int high)
{
    int mid;
    if(low > high) return 0; /* failure */
    else
    {
        mid=(low+high)/2;
        if(a[mid]== key) return mid; /* Success */
        if(key < a[mid]) return(BS(a,key,low,mid-1));
        return(BS(a,key,mid+1,high));
    }
}
tower(int n, char src, char intr, char dst)
{
    if(n > 0)
    {
        tower(n-1,src,dst,intr);
        printf("Move disk %d from %c to %c \n", n,src,dst);
        tower(n-1,intr,src,dst);
    }
}
</pre><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/c-advanced/c-program-for-binary-search-and-towers-of-hanoi-using-recursion/">C Program for Binary Search and Towers of Hanoi using Recursion</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/c-program-for-binary-search-and-towers-of-hanoi-using-recursion/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>C Program for Binary Search Tree Creation and Traversals</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c/data-structures-c/c-program-for-binary-search-tree-creation-and-traversals/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c/data-structures-c/c-program-for-binary-search-tree-creation-and-traversals/#comments</comments>
		
		<dc:creator><![CDATA[Ansten Lobo]]></dc:creator>
		<pubDate>Sat, 09 Jun 2012 16:52:49 +0000</pubDate>
				<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[Tree Creation]]></category>
		<category><![CDATA[Tree Traversals]]></category>
		<category><![CDATA[C profram]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=3317</guid>

					<description><![CDATA[<p>C Program for Binary Search Tree Creation and Traversals. Source: Dr. G T Raju, Professor &#038; Head, Dept. of CSE, RNSIT #include typedef struct tnode { int data; struct tnode *right,*left; }TNODE; TNODE *CreateBST(TNODE *, int); void Inorder(TNODE *); void Preorder(TNODE *); void Postorder(TNODE *); main() { TNODE *root=NULL; /* Main Program */ int opn,elem,n,i;</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/data-structures-c/c-program-for-binary-search-tree-creation-and-traversals/">C Program for Binary Search Tree Creation and Traversals</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>C Program for Binary Search Tree Creation and Traversals.<br />
Source: Dr. G T Raju, Professor &#038; Head, Dept. of CSE, RNSIT</p>
<pre lang="C" line="1">
#include <stdlib.h>
typedef struct tnode
{
    int data;
    struct tnode *right,*left;
}TNODE;

TNODE *CreateBST(TNODE *, int);
void Inorder(TNODE *);
void Preorder(TNODE *);
void Postorder(TNODE *);
main()
{
    TNODE *root=NULL;		 /* Main Program */
    int opn,elem,n,i;
    do
    {
        clrscr();
        printf("\n ### Binary Search Tree Operations ### \n\n");
        printf("\n Press 1-Creation of BST");
        printf("\n       2-Traverse in Inorder");
        printf("\n       3-Traverse in Preorder");
        printf("\n       4-Traverse in Postorder");
        printf("\n       5-Exit\n");
        printf("\n       Your option ? ");
        scanf("%d",&opn);
        switch(opn)
        {
        case 1: root=NULL;
            printf("\n\nBST for How Many Nodes ?");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                printf("\nRead the Data for Node %d ?",i);
                scanf("%d",&#038;elem);
                root=CreateBST(root,elem);
            }
            printf("\nBST with %d nodes is ready to Use!!\n",n);
            break;
        case 2: printf("\n BST Traversal in INORDER \n");
            Inorder(root); break;
        case 3: printf("\n BST Traversal in PREORDER \n");
            Preorder(root); break;
        case 4: printf("\n BST Traversal in POSTORDER \n");
            Postorder(root); break;
        case 5: printf("\n\n Terminating \n\n"); break;
        default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
            break;
        }
        printf("\n\n\n\n  Press a Key to Continue . . . ");
        getch();
    }while(opn != 5);
}
TNODE *CreateBST(TNODE *root, int elem)
{
    if(root == NULL)
    {
        root=(TNODE *)malloc(sizeof(TNODE));
        root->left= root->right = NULL;
        root->data=elem;
        return root;
    }
    else
    {
        if( elem < root->data )
            root->left=CreateBST(root->left,elem);
        else
            if( elem > root->data )
                root->right=CreateBST(root->right,elem);
            else
                printf(" Duplicate Element !! Not Allowed !!!");

        return(root);
    }
}
void Inorder(TNODE *root)
{
    if( root != NULL)
    {
        Inorder(root->left);
        printf(" %d ",root->data);
        Inorder(root->right);
    }
}

void Preorder(TNODE *root)
{
    if( root != NULL)
    {
        printf(" %d ",root->data);
        Preorder(root->left);
        Preorder(root->right);
    }
}

void Postorder(TNODE *root)
{
    if( root != NULL)
    {
        Postorder(root->left);
        Postorder(root->right);
        printf(" %d ",root->data);
    }
}
</pre><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/data-structures-c/c-program-for-binary-search-tree-creation-and-traversals/">C Program for Binary Search Tree Creation and Traversals</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c/data-structures-c/c-program-for-binary-search-tree-creation-and-traversals/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Binary Search</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/binary-search/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/binary-search/#comments</comments>
		
		<dc:creator><![CDATA[Chitra]]></dc:creator>
		<pubDate>Mon, 15 Aug 2011 17:43:26 +0000</pubDate>
				<category><![CDATA[Advanced programs]]></category>
		<category><![CDATA[c program]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[Searching programs]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1591</guid>

					<description><![CDATA[<p>A binarysearch program locates the position of an item in a sorted array. Logic: The Binary search starts by testing the element at the middle of the array. Let us consider an array ‘a’ with all elements arranged in ascending order. Let low and high are the lower and upper indices of the array ‘a’, respectively. We want to search an item</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/c-advanced/binary-search/">Binary Search</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>A binarysearch program locates the position of an item in a sorted array.</p>
<p>Logic: The Binary search starts by testing the element at the middle of the array. Let us consider an array ‘a’ with all elements arranged in ascending order. Let low and high are the lower and upper indices of the array ‘a’, respectively. We want to search an item K in it. To do this, we calculate a middle index with the values of low and high, let it be mid. Then we compare if K=a[mid] or not.</p>
<p>If it is true, then search is done. Otherwise, the search is to be repeated on left or right half depending on the K&lt;a[mid] or K&gt;a[mid], respectively. In case of low =high and K a[mid], the search terminates unsuccessfully.</p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-1641 size-full" title="Binary Search flow" src="https://studentprojects.in/wp-content/uploads/2011/08/BinarySearch.png" alt="Binary Search flow" width="583" height="207" /></p>
<p>Example 1:</p>
<p>Consider the following elements stored in an array,</p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-1642 size-full" title="Binary Search example" src="https://studentprojects.in/wp-content/uploads/2011/08/BinarySearch_Ex1.png" alt="Binary Search example" width="581" height="402" /></p>
<p>Example 2:</p>
<p>Consider the following elements stored in an array,</p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-1643 size-full" title="Binary Search example" src="https://studentprojects.in/wp-content/uploads/2011/08/BinarySearch_Ex2.png" alt="Binary Search example" width="584" height="430" /></p>
<p>Program:</p>
<pre lang="c" line="1" escaped="true">#include"stdio.h"
#include"conio.h"
void main()
{
	int a[25], i, n, K, flag = 0, low, high, mid;
	clrscr();
	printf("Enter the number of elementsn");
	scanf("%d", &amp;n);
	printf("Enter the elementsn");
	for(i = 0; i&lt;n; i++)
	{
		scanf("%d",&amp;a[i]);
	}
	printf("Enter the key to be searchedn");
	scanf("%d",&amp;K);
	low = 0;
	high = n - 1;
	while(low &lt;= high)
	{
		mid = (low+high)/2;
		if(a[mid] == K)
		{
			flag = 1;
			break;
		}
		else if(K&lt;a[mid])
			high = mid-1;
		else
			low = mid + 1;
	}
	if(flag == 1)
	{
		printf("Key element is found");
	}
	else
	{
		printf("Key element not found");
	}
	getch();
}
</pre><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c/c-advanced/binary-search/">Binary Search</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c/c-advanced/binary-search/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Program to implement Binary Search Algorithm</title>
		<link>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-advanced/program-to-implement-binary-search-algorithm/</link>
					<comments>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-advanced/program-to-implement-binary-search-algorithm/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Tue, 01 Feb 2011 11:44:15 +0000</pubDate>
				<category><![CDATA[Advanced programs]]></category>
		<category><![CDATA[C Programs]]></category>
		<category><![CDATA[Binary Search Algorithm]]></category>
		<category><![CDATA[binary search]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1223</guid>

					<description><![CDATA[<p>#include template int binarySearch(T a[], int n, T&#038; x) { int left = 0; // left end of segment int right = n - 1; // right end of segment while (left a[middle]) left = middle + 1; else right = middle - 1; } return -1; // x not found } int main() {</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-advanced/program-to-implement-binary-search-algorithm/">Program to implement Binary Search Algorithm</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<pre lang="cpp">
#include <iostream.h>
template<class T>
int binarySearch(T a[], int n, T& x)
{
	int left = 0;                       // left end of segment
	int right = n - 1;                  // right end of segment
	while (left <= right)
	{
		int middle = (left + right)/2;   // middle of segment
		if (x == a[middle]) return middle;
		if (x > a[middle]) left = middle + 1;
		else right = middle - 1;
	}
	return -1; // x not found
}
int main()
{
	int a[10],n,t;
	cout<<"Enter the size:";
	cin>>n;
	cout<<"enter the elements in sorted order:";
	for(int i=0;i<n;i++)
		cin>>a[i];
	cout<<"enter the element to search:";
	cin>>t;
	int f=binarySearch(a,n,t);
	if(f==-1)
		cout<<"element not found";
	else
		cout<<"element found at index:"<<f;
}
</pre><p>The post <a href="https://studentprojects.in/software-development/cpp/cpp-programs/cpp-advanced/program-to-implement-binary-search-algorithm/">Program to implement Binary Search Algorithm</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/cpp-programs/cpp-advanced/program-to-implement-binary-search-algorithm/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
