23.10.25

5. Binary Search, When Friends Divide and Conquer!

 

hello everyone 

 Remember when we (me, Aarav, and Rahul) were sitting in the lab, tired of typing long Linear Search codes again and again?  That’s when Aarav said, “Dude, why search every element one by one when you can just cut the list in half and win faster?” And boom that’s how our Binary Search adventure began. So today, let’s check out what we made a super-smart program that finds your number in record speed!

What It Does

This program performs a Binary Search on a sorted list of numbers.
It’s like when Rahul divides snacks evenly check the middle, if not there, look left or right 

You just:

  • Enter how many numbers you’ll input (make sure they’re in ascending order).

  • Choose whether to search one number or multiple numbers.

  • The program keeps dividing the list in halves until your number is found (or confirmed missing).

  • You can keep searching, restart, or exit anytime.

How It Works 

  • I (Daksh) write the code.

  • Aarav handles the logic “Cut the list in half again!” he keeps yelling.

  • Rahul tests it by entering weird numbers to break my code 

  • Together, we made a Binary Search that can search single or multiple numbers easily.

Code:

 #include <stdio.h>
 #include <string.h>
 
 int main() {
     int numbers[100];
     int totalElements, targetValue, numSearch, searchValues[100];
     int low, high, mid, isFound;
     char choice[10], searchType[10];
 
     printf("Enter the number of elements in the array: ");
     scanf("%d", &totalElements);
 
     printf("Enter %d sorted elements (in ascending order):\n", totalElements);
     for (int i = 0; i < totalElements; i++) {
         scanf("%d", &numbers[i]);
     }
 
     do {
         printf("Do you want to search 1 element or multiple elements? (1/multiple): ");
         scanf("%s", searchType);
 
         if (strcmp(searchType, "1") == 0) {
             do {
                 isFound = 0;
                 printf("Enter the number to search: ");
                 scanf("%d", &targetValue);
 
                 low = 0;
                 high = totalElements - 1;
 
                 while (low <= high) {
                     mid = (low + high) / 2;
 
                     if (numbers[mid] == targetValue) {
                         printf("Value %d found at position %d.\n", targetValue, mid + 1);
                         isFound = 1;
                         break;
                     } else if (numbers[mid] < targetValue) {
                         low = mid + 1;
                     } else {
                         high = mid - 1;
                     }
                 }
 
                 if (!isFound)
                     printf("Value %d not found in the array.\n", targetValue);
 
                 printf("Do you want to search another number? (yes/no): ");
                 scanf("%s", choice);
 
             } while (strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0);
 
         } else if (strcmp(searchType, "multiple") == 0) {
             do {
                 printf("How many numbers do you want to search? ");
                 scanf("%d", &numSearch);
 
                 printf("Enter %d numbers to search:\n", numSearch);
                 for (int i = 0; i < numSearch; i++) {
                     scanf("%d", &searchValues[i]);
                 }
 
                 for (int i = 0; i < numSearch; i++) {
                     isFound = 0;
                     low = 0;
                     high = totalElements - 1;
 
                     while (low <= high) {
                         mid = (low + high) / 2;
 
                         if (numbers[mid] == searchValues[i]) {
                             printf("Value %d found at position %d.\n", searchValues[i], mid + 1);
                             isFound = 1;
                             break;
                         } else if (numbers[mid] < searchValues[i]) {
                             low = mid + 1;
                         } else {
                             high = mid - 1;
                         }
                     }
 
                     if (!isFound)
                         printf("Value %d not found in the array.\n", searchValues[i]);
                 }
 
                 printf("Do you want to search another set of numbers? (yes/no): ");
                 scanf("%s", choice);
 
             } while (strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0);
 
         } else {
             printf("Invalid choice. Please enter 1 or multiple.\n");
         }
 
         printf("Do you want to restart the search program? (yes/no): ");
         scanf("%s", choice);
 
     } while (strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0);
 
     printf("Okay, exiting the Binary Search program.\n");
     return 0;
 }

Output:

  

← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...