Binary Search

//This code is written by Souvik Ghosh
//Iterative Binary Search
#include <stdio.h>
//Function for Ascending Binary  Search
int ascendingBinarySearch(int *p, int size, int search)
{
 int low = 0, high = size - 1, mid;
 while (low <= high)
    {
 mid = (low + high) / 2;
 if (search == p[mid])
        {
 return mid;
        }
 else if (search > p[mid])
        {
 low = mid + 1;
        }
 else if (search < p[mid])
        {
 high = mid - 1;
        }
    }
 return -1;
}
//Function for Descending Binary Search
int descendingBinarySearch(int *p, int size, int search)
{
 int low = 0, high = size - 1, mid;
 while (low <= high)
    {
 mid = (low + high) / 2;
 if (search == p[mid])
        {
 return mid;
        }
 else if (search > p[mid])
        {
 high = mid - 1;
        }
 else if (search < p[mid])
        {
 low = mid + 1;
        }
    }
 return -1;
}
void main()
{
 int n, found, search;
 printf("Enter the number of element\n");
 scanf("%d", &n);
 int s[n];
 printf("Enter Elements Either Ascending or Descending Order\n");
 for (int i = 0; i < n; i++)
    {
 scanf("%d", &s[i]);
    }
 printf("Enter searching element\n");
 scanf("%d", &search);
 int low = s[0];
 int high = s[n - 1];
 if (low < high)
    {
 found = ascendingBinarySearch(s, n, search);
    }
 else
    {
 found = descendingBinarySearch(s, n, search);
    }
 found == -1 ? printf("Not Found") : printf("Element %d found at index %d", search, found + 1);
}


//This code is written by Souvik Ghosh
//Recursive Binary Search
#include <stdio.h>
//Recurcive binary search function for searching element in a ascending order array
int ascending(int *p, int left, int right, int search)
{
 int mid = (left + right) / 2;
 if (left > right)
    {
 return -1;
    }
 if (search == p[mid])
    {
 return mid;
    }
 else if (search < p[mid])
    {
 ascending(p, left, mid - 1, search);
    }
 else if (search > p[mid])
    {
 ascending(p, mid + 1, right, search);
    }
}
//Recursive binary search function for searching element in a descending order array
int decending(int *p, int left, int right, int search)
{
 int mid = (left + right) / 2;
 if (left > right)
    {
 return -1;
    }
 if (search == p[mid])
    {
 return mid;
    }
 else if (search < p[mid])
    {
 decending(p, mid + 1, right, search);
    }
 else if (search > p[mid])
    {
 decending(p, left, mid - 1, search);
    }
}

void main()
{

 int size, search, low, high, found;
 printf("Enter the number of elements\n");
 scanf("%d", &size);
 int sgg[size];
 printf("Enter the elements\n");
 for (int i = 0; i < size; i++)
    {
 scanf("%d", &sgg[i]);
    }
 printf("Enter the element you want to search\n");
 scanf("%d", &search);
 low = sgg[0];
 high = sgg[size - 1];
 if (low < high)
    {
 found = ascending(sgg, 0, size - 1, search);
    }
 else
    {
 found = decending(sgg, 0, size - 1, search);
    }
 found == -1 ? printf("Not Found") : printf("Element %d found at index %d", search, found + 1);
}

Related Posts

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

HTML Cheatsheet

Unleash your web development potential with my HTML cheatsheet! Elevate your coding skills, master essential tags and attributes, and build stunning websites with ease. Whether you’re a…

Git&GitHub Cheatsheet

Elevate your Git and GitHub game with my ultimate cheatsheet! Unlock the power of version control, streamline collaboration, and boost productivity. Navigate the complexities of Git commands…

Command Line Cheatsheet

Master the Command Line effortlessly with my comprehensive cheatsheet! Streamline your workflow, boost productivity, and become a command line guru. Unlock essential commands, shortcuts, and tips to…

OpenAI’s APIs Notes

Unlock the potential of OpenAI’s APIs with my comprehensive notes! Dive into the world of cutting-edge artificial intelligence, explore the capabilities of GPT-4 and beyond, and learn…

SQL Notes

Discover the essence of SQL with my comprehensive notes! Dive into the world of Structured Query Language, unraveling the complexities, and mastering database management. Elevate your SQL…

Leave a Reply

Your email address will not be published. Required fields are marked *