Tech Unleashed

Wohoo!! We're on YouTube

Tuesday, 7 November 2023

C++ : Lecture 4: Binary Search Algorithm : C++ Confusion Buster !

 C++ Confusion Buster: Your Easy Guide to C++ !


Binary Search Algorithm code [C++] :


// Shyam Sunder Kanth
// Insta : still_23.6_8
// ? Binary Search

#include<iostream>

using namespace std;

int binarySearch(int arr[], int target, int start, int end){
    if(start>end){
        return -1;
    }

    int mid = (start+end)/2;

    if(target == arr[mid]){
        return mid;
    }

    if(target > arr[mid]){
        return binarySearch(arr,target,mid+1,end);
    }

    if(target < arr[mid]){
        return binarySearch(arr,target, start, mid-1);
    }

    return -1;
}

int main()
{
    int arr[5] = {4,8,9,12,45};
    int len = sizeof(arr)/sizeof(arr[0]);
    int target = 10;

    int res = binarySearch(arr,target,0,len-1);

    if(res == -1){
        cout<<"Element not found";
    }else{
        cout<<target<<" is found at position "<<res+1<<endl;
    }
    return 0;
}

Hey, thanks for watching our video about Binary search algorithm ! In this video we’ll walk you through:

- Concept

- How It works ?

- Implementation

- Code


All codes are available at my GitHub account, check them out here:

https://github.com/shyamkanth/Placements


Check out other videos from DS Revealed playlist: https://www.youtube.com/playlist?list=PLNXqJgOsTCZOB60T9HDhMf_o8RXbNWgqS


Check out our channel here: https://www.youtube.com/@DevelopersByte


Find us at: https://shyamkanth.github.io/


Timestamps:

0:00 Intro

1:28 Concept

2:15 Difference btw LS and BS

4:50 Logic

17:30 Summary

19:25 Code

24:50 Outro


About our channel:

Our channel is about Revealing the secrets of Data Structure. We cover lots of cool stuff such as Codes, Concepts and Implementations.

Check out our channel here: https://www.youtube.com/@DevelopersByte

Don’t forget to subscribe!


Follow me on social media:

Get updates or reach out to Get updates on our Social Media Profiles!

GitHub: https://github.com/shyamkanth/

Instagram: https://instagram.com/still_23.6_8

Instagram personal: https://instagram.com/itz_sammmii

No comments:

Post a Comment