Binary Search Algorithm | C Programming Example

Поділитися
Вставка
  • Опубліковано 18 жов 2024
  • An implementation of the binary search algorithm in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

КОМЕНТАРІ • 86

  • @parinrava978
    @parinrava978 11 місяців тому +13

    I love your videos. I have a midterm tomorrow on uni course C programming and I have been watching many videos about this topic and nobody can describe it well like you do. I used to hate C cause I didn't understand anything but after watching your videos everything makes sense. It's very detailed and contains only information that we need to know in order to understand the concept. Thank you so much for great work.

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому +2

      I’m glad you enjoy the videos, thank you so much for leaving this nice comment and good luck on your midterm! :-)

  • @aditopguy9785
    @aditopguy9785 11 місяців тому +3

    Thank you for putting in the time and effort to make these videos. You are explain concepts so well, way better than my university lecturer

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому +1

      You’re very welcome, I’m glad you’re enjoying the explanations! :-)

  • @eugeneplay9416
    @eugeneplay9416 2 роки тому +9

    Love your videos. Really easy to follow along and very good explanations.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Thank you very much Eugene, I'm glad to hear you enjoy the videos! :-D

  • @ostryy0560
    @ostryy0560 Рік тому +2

    Thanks my man. I am preparing for a test, and this video helped me AF. Greetings from PL 🇵🇱

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +2

      That's awesome I'm glad to hear it helped you out! And good luck on that test! :-)

  • @TSkateT666
    @TSkateT666 2 роки тому +1

    have an exam soon in "Introduction to C" course in uni, this really helped alot!

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Awesome, I’m glad to hear it was helpful! And good luck on your exam! :-D

  • @elfullin
    @elfullin 4 місяці тому

    I like that you used recursive methods. Thank you

  • @mongraal2272
    @mongraal2272 2 роки тому

    Thaaaaank you...i I watched this video for 3 hours and i finally understood...thank u,ur logic is so impressive

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      You’re welcome, I’m glad you figured it out! :-)

  • @SouvikMandal-tl2to
    @SouvikMandal-tl2to 5 місяців тому

    a better breaking condition for the binary search would be if(l == r) rather than if(l>r) i think !

  • @Mnogojazyk
    @Mnogojazyk 2 роки тому

    Love your videos. Finally do I understand the concepts! Many thanks!

  • @FeroLight-e2n
    @FeroLight-e2n Рік тому +1

    That was amazing .Thank you

  • @pvcksubhashini4984
    @pvcksubhashini4984 2 роки тому +1

    Very Nice Content Useful to all
    Plz make subtitles in english (In this video Subtitiles r translating in dutch when converted to english it doesn't make sense)
    Plz make C++ also , u r explaining very nice!!

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      That's so weird that it auto-generated the subtitles to Dutch! Thank you for letting me know, I will see if there is anything I can to do fix that. :-) Also, I have now started a series of C++ videos as well: ua-cam.com/play/PLA1FTfKBAEX6BdpNaWp2uw-YspHwY7qwW.html

    • @pvcksubhashini4984
      @pvcksubhashini4984 2 роки тому

      @@PortfolioCourses Ya i saw that one also I shared to my friends too. Thanks for the Content ☺

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      It's interesting, but I'm not sure UA-cam will allow me to "re-generate" the subtitles as English. I may need to upload my own or something, what a pain!

  • @nazaraljendli9758
    @nazaraljendli9758 2 роки тому

    best c tutorials out there thank you

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      You're welcome Nazar! 😀 And thank you very much for the positive feedback too.

  • @juanmacias5922
    @juanmacias5922 2 роки тому

    Great video! Explained super well!

  • @OumaamarIsra
    @OumaamarIsra 4 місяці тому

    Thank you man you save my life

  • @bieelzzin
    @bieelzzin Рік тому

    Thank you! Perfect explained

  • @germankoga8640
    @germankoga8640 4 місяці тому

    I didn't understand how you calculated mid, wouldn't you have to sum the two extremes and divided them by 2 to find the mid point?

  • @johnofalltrades2000
    @johnofalltrades2000 Рік тому +1

    the mathematical operation presented in the line:
    int mid = l + (r - 1) / 2;
    doesn't work for me. My compiler seems to go out of memory bounds because of this.
    I've tried:
    int mid = (l + r) / 2;
    and it worked.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      It should be: int mid = l + (r - l) / 2
      So it's an l not a 1.
      It's more complicated, but it's actually better to do it this way instead of (l + r) /2, see this video: ua-cam.com/video/JNFGvjATOUA/v-deo.html

    • @johnofalltrades2000
      @johnofalltrades2000 Рік тому +1

      @@PortfolioCourses oh I see now, what a silly mistake. thank you so much for clarifying it for me.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      No problem! :-)

  • @omatzi7251
    @omatzi7251 11 місяців тому

    Great video and great teacher!

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому

      I’m glad you enjoyed the video and teaching! :-)

  • @epicgameryt4052
    @epicgameryt4052 Рік тому

    You're actually kinda awesome, thanks!

  • @athunasos1649
    @athunasos1649 10 місяців тому

    Using a loop is more memory efficient than using a recursive function

  • @veggieveggie1215
    @veggieveggie1215 6 місяців тому

    what microphone do you use?

  • @magdugalrexon7234
    @magdugalrexon7234 2 роки тому

    Thank you for the video. It was really helpful.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      You're welcome! I'm glad to hear it was helpful for you. :-D

  • @RelAxGamingKing77
    @RelAxGamingKing77 2 роки тому

    Thank you so much, really helped me out!

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      You're very welcome, I'm glad to hear it helped you out! :-D

  • @loser22-q7w
    @loser22-q7w 4 дні тому

    isnt it reasonable to use the sizeof function to find the midpoint?

  • @adamsetterberg5223
    @adamsetterberg5223 2 роки тому +1

    Love the video!

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      This number guessing game might be close to what you're looking for: ua-cam.com/video/41NmcgA1HF0/v-deo.html
      And to my knowledge we can't really use binary search on an unsorted array, unless we sort it first. Here are some folks explaining why: www.quora.com/Can-binary-search-be-applied-on-unsorted-list

  • @DeadPool-jr2er
    @DeadPool-jr2er 2 роки тому

    I love it!! Thank you!

  • @loginashaalan2826
    @loginashaalan2826 4 місяці тому

    what if the value needed is less than all the elements of the array does we do the same as return -1?

  • @adamsetterberg5223
    @adamsetterberg5223 2 роки тому

    Hello Again!
    I having a problem with binary searching......
    So lets say that you tell you computer to "random generate" 10 numbers between 1 - 20.
    And you want to know if the number 7 have been generated.
    Do you need to bubble sort first before you use binary searching?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Yes, the list needs to be sorted before you can use binary search. You could use bubble sort, or you could use something like insertion sort, selection sort, quicksort, or merge sort.
      In terms of performance, for very small arrays like this, using insertion sort or selection sort would be best (better than bubble sort too). For larger arrays, using something like quicksort or merge sort would be better.
      We have videos on all these aglorithms too:
      Insertion Sort: ua-cam.com/video/Tz7vBodZqo8/v-deo.html
      Selection Sort: ua-cam.com/video/YepJ7fDmyjI/v-deo.html
      Quicksort: ua-cam.com/video/0jDiBM68NGU/v-deo.html
      Merge Sort: ua-cam.com/video/LeWuki7AQLo/v-deo.html

  • @dabawocks1121
    @dabawocks1121 2 роки тому

    Thank you for this

  • @CSstudent_1001
    @CSstudent_1001 Рік тому

    Thank u a lot 🌸

  • @emeziekejedidiah5547
    @emeziekejedidiah5547 2 роки тому +1

    This video is great

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Thank you! Very glad to hear you enjoyed it! :-D

  • @erennkose
    @erennkose Рік тому +1

    bro thank you so much

  • @Code-nv8xl
    @Code-nv8xl 9 місяців тому

    Why you didn't use that unsorted array searching :/

    • @elfullin
      @elfullin 4 місяці тому

      Because you can’t use binary search on an unsorted array.

  • @maven5463
    @maven5463 2 роки тому +2

    Hmm, Can't we just write mid = (l + r) / 2 🤔

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +3

      Great question! 😀 You could do it that way too, though what we do in this video is better in terms of preventing an integer overflow: stackoverflow.com/questions/25113506/why-m-l-r-l-2-instead-of-m-lr-2-avoid-overflow-in-c.

    • @maven5463
      @maven5463 2 роки тому

      @@PortfolioCourses oh, understood now. The video was so helpful! 😄 Thank you.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      You’re welcome! :-)

  • @idanmariani8601
    @idanmariani8601 Рік тому

    your awsome :)

  • @haniforange
    @haniforange 9 місяців тому

    2 hours before final exam wish me luck

  • @softvampir3
    @softvampir3 5 місяців тому

    Watching this video for class - please please turn on subtitles at least auto subtitles it takes no work and saves many people who are watching.

    • @PortfolioCourses
      @PortfolioCourses  5 місяців тому

      The video does have auto subtitles, you just have to click the gear icon in the top-right to enable them. :-)

    • @softvampir3
      @softvampir3 5 місяців тому

      @@PortfolioCoursesNo actually when I click the gear icon I see: "Stable Volume" ; "Ambient Mode" ; "Playback speed" ; and "Quality" there is no closed captions or subtitles on the gear icon. Next to the gear icon on the left is "cc" button which is greyed out and when I hover over it the message reads -> "Subtitles/closed captions unavailable".

    • @PortfolioCourses
      @PortfolioCourses  5 місяців тому

      Weird, what type of device/software are you using? I'm using Firefox on MacOS. I'm thinking it could be a difference related to using web vs. app or something like that.

    • @SurajBhandari-px1rv
      @SurajBhandari-px1rv 5 місяців тому

      ​@@PortfolioCoursesIts not available in my device too

    • @PortfolioCourses
      @PortfolioCourses  5 місяців тому

      What type of device are you using?

  • @Aaron-hh8nx
    @Aaron-hh8nx Рік тому +1

    #include
    using namespace std;
    int main()
    { int LIST[25],MAX;
    int FIRST, LAST, MIDDLE, I, ITEM, LOC=-1;
    coutMAX;
    cout

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      That looks like the correct C++ solution to me. :-) Maybe I should make a C++ version of this video too though, that would be fun.

    • @Aaron-hh8nx
      @Aaron-hh8nx Рік тому +1

      @@PortfolioCourses please do, it's from my school text book

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Cool! :-)