C++ Programming: Binary Search Algorithm

Sdílet
Vložit
  • čas přidán 29. 06. 2012
  • Code can be found at pastebin.com/fsmGF1hp
    Concepts:
    How the binary search algorithm works
    Performance of binary search in comparison to linear search
    Binary search cuts the search space in half on each comparison
    Implementation of binary search in C++
    Binary search has log(n) running time (time complexity).

Komentáře • 143

  • @christianvillamor6273
    @christianvillamor6273 Před 11 lety +31

    WOW. Thank you! Programmers tend to be really snobby when it comes to helping. WE NEED MORE PROGRAMMERS LIKE YOU!

  • @yasina63
    @yasina63 Před rokem +3

    I have seen the Linear search algorithm you completely figured out for me. Your teaching way is a piece of cake.
    Watching from Ethiopia. Thanks a bunch.

  • @rajcodes100
    @rajcodes100 Před 6 lety +1

    Thanks so much - I have listened to your videos on arrays and passing arrays to functions - now I finally think I get the concepts - the explanations were clear and systematic - keep posting more videos - kudos sir for sharing your knowledge in a way that communicates to the student.

  • @uzumaki9t
    @uzumaki9t Před 8 lety +23

    Very well explained. I thought binary search was something awful when I saw it on my last exam. Thank you very much!

  • @opiumslave
    @opiumslave Před 4 lety

    I've my Computer Science boards practical exam on 1st Feb. U helped me a lot.... Thanks man

  • @mountgraph1403
    @mountgraph1403 Před 6 lety +1

    Pls dont stop making this videos. They really help!

  • @babbalgts
    @babbalgts Před 8 lety

    you are good at teaching...... i didn't find any video that made it as clear as you did!.... thanks a lot;... coz tomorrow is my 12th grade final exam.

  • @eaglebaize
    @eaglebaize Před 11 lety +2

    Thank you so much! I have a comprehensive c++ final today (it will cover material from both this and the previous semesters). Cheers!

  • @studyfreak8429
    @studyfreak8429 Před 3 měsíci

    this is year 2024 and this stuff is still relevant, man c++ IS evergreen

  • @grigorebordea1212
    @grigorebordea1212 Před 5 lety +2

    Thank you so much i've tried to understand this algorithm for 2 days even though this isnt that hard

  • @NatsukiSY
    @NatsukiSY Před 2 lety +1

    I love you man
    Even my doctor couldn't make it this simple

  • @davidporterrealestate
    @davidporterrealestate Před 7 lety +169

    Thank God you have an American accent

  • @WalterCueva
    @WalterCueva Před 10 lety

    I have a Two-Dimensional Arrays. Do you have a video showing how to use linear and binary search on a Two-Dimensional Arrays?

  • @bluesaint9163
    @bluesaint9163 Před 8 lety

    How could I store all the words in my project? if I will go for a dcitonary?

  • @aovlover415
    @aovlover415 Před 6 lety

    well done explained. Should the number sequence ? coz i try random number and false answer.
    what is your application ? i'm using borland 5.02 ,about your code 'using namespace std' ,borland say that 'namescpace name expected'
    i'm bit consfused, please answer :)

  • @salehabuhussein5229
    @salehabuhussein5229 Před 3 lety

    what software do you use to run cpp file?

  • @robbiecastillo7495
    @robbiecastillo7495 Před 7 lety +3

    Well done sir! Very helpful.

  • @cristopheririas1509
    @cristopheririas1509 Před 5 lety

    I really love your way of explaning. I should tell you I am not a native English Speake however, I've been able to understand most. Congratulation !!!! pd: GREETINGS FORM HONDURAS!

  • @Lolablue1986
    @Lolablue1986 Před 6 lety +1

    Great video been looking for something like this all day lol

  • @corrondo25
    @corrondo25 Před 8 lety

    watch 4 videos looking for a simple detail concerning the algorithm. You had it.

  • @rafiullahqallander
    @rafiullahqallander Před 8 lety +1

    I just can say, you are THE BEST.
    Many thanks for your great contribution, May God bless you :)

  • @lamp_x
    @lamp_x Před rokem +1

    So basically, we can't do binary search if the array isn't sorted from low to high?
    Unlike linear search that can do even though if the array isn't sorted (random number)

  • @billzhang1892
    @billzhang1892 Před 11 lety

    Thanks very much for so simplified and excellent lecture!

  • @1314rom
    @1314rom Před 10 lety

    Awesome video. Clear and to the point.

  • @TheVerbalAxiom
    @TheVerbalAxiom Před 8 lety

    Wonderful, perfect explanation.

  • @hokutoueda6215
    @hokutoueda6215 Před 3 lety

    you are wonderful person and i really like your videos.

  • @stephenkamenar
    @stephenkamenar Před 11 lety

    Oops, I think that was just a typo on my part and it still doesn't work, right?

  • @escaravar7417
    @escaravar7417 Před 9 lety +1

    Thank you for this video, it's so helpful!!

  • @Brad6013-
    @Brad6013- Před rokem

    You explained this very well. Thank you

  • @snipere2009
    @snipere2009 Před 8 lety

     Using a C++ array of STRUCTUREs, write a program that takes input of student information - for 10 students - like:
    1) Student ID.
    2) Student name.
    3) Course marks (5 courses for each student).
     The program provides below functionality:
    1) Show all records.
    2) Search and display a student record on ID.
    3) Modify the record of a particular student.
    4) Show the passing percentage for each course.
    5) Show the names of students who failed in a particular course.
    6) Show the total marks, the percentage, and the overall letter grade for individual students.
    7) Show the names and the letter grades of all students in each course.
    9) Show the student names for each letter grade in each course [ A >= 90% - B >= 80% - C >= 70% - D >= 60% - F < 60% ].
     It is required to write a modular program.

  • @Gooneryz
    @Gooneryz Před 11 lety

    @ReelLearning you made it so simple and logical to understand.Thanks for the video it helped me a lot. :) By the way what is the program you're using to write the code and compile it?

  • @ranjhi369
    @ranjhi369 Před 11 lety

    excellent and simplified presentation. thanx

  • @DesignAndDevops
    @DesignAndDevops Před rokem

    What about last element

  • @spicytuna08
    @spicytuna08 Před 6 lety

    with C++, there is no need for binary search. Just store data into set STL data structure and use its set::find() member function. But thanks for explaining binary search algo.

  • @user-kr1wc6kk8g
    @user-kr1wc6kk8g Před 2 měsíci

    admiration come it self if you competent like you .thanks

  • @hyunbinpark3382
    @hyunbinpark3382 Před 7 lety +1

    Thank you for posting!

  • @erictronic
    @erictronic Před 8 lety

    Thank you for great lesson!

  • @stevezes
    @stevezes Před 5 lety +1

    your mid point calculation can cause overflow

  • @delvindavid2804
    @delvindavid2804 Před 7 lety

    Thank You so much Derek, I wouldn't be able to answer this question in exam ,if I had not found your channel!

  • @tarikabughalib1263
    @tarikabughalib1263 Před rokem

    Thank you so much this is unbelievable helpful

  • @vulcuemil4367
    @vulcuemil4367 Před 4 lety +6

    When dealing with huge arrays you risk to overflow when calculating mid. A safer way is: mid = low + (high - low) / 2 .

    • @Asterite
      @Asterite Před 3 lety

      what does overflowing mean?

  • @kingsapo
    @kingsapo Před 11 lety

    Thanks a bunch, got really stuck on this, your video helped a lot!

  • @whateveriwannabe1
    @whateveriwannabe1 Před 7 lety +1

    These are very helpful videos

  • @JBMJaworski
    @JBMJaworski Před 11 lety

    Thank you for sharing good quality teaching. :-)
    Regards!
    Jarek Jaworski

  • @AhmedHadiPADI_scuba_instructor

    what the result of log2(64000) represents ? i mean the 15.966. thanks a lot !

    • @phatchunk99
      @phatchunk99 Před 7 lety +1

      It takes 16 loops to search a number within an array of 64000 numbers.

  • @raghurammuthyala1023
    @raghurammuthyala1023 Před 8 lety

    Great video! Amazing! Thank you for uploading sir!
    Sir which one do you think is the best compiler ( user friendly and easy to understand)?

  • @snyfalcryo524
    @snyfalcryo524 Před 4 lety

    My question is, would binary search still works if there's double or more of the specific data you're looking for?
    If so, how/what's the algorithm?

    • @pendyalaabhishek6273
      @pendyalaabhishek6273 Před 3 lety

      for example if you are looking for 2 numbers you can give search value as input 2 times and run code 2 times ..

  • @HarisHussainkhan
    @HarisHussainkhan Před 10 lety

    Thanxz very Good Instructions Given ..... Thank you Very Much.....

  • @talhaghaffar1324
    @talhaghaffar1324 Před 10 lety

    my great video lecture ever...

  • @NadaAhmed-zx1ru
    @NadaAhmed-zx1ru Před 10 lety

    This was great and simply explained, than you :)

  • @fortunesuwedi6714
    @fortunesuwedi6714 Před 9 měsíci

    Good explanation

  • @sakspan3265
    @sakspan3265 Před 10 lety

    Sir can you explain why there is high = size-1 because I am little bit confuse. What is the use of high = size-1?
    10:01

    • @aarjavbaxi2299
      @aarjavbaxi2299 Před 3 lety +1

      Here, size stands for the number of elements in the array ; So according to that , your high index value would be 1 less than the number of elements.

  • @aliabid2839
    @aliabid2839 Před 5 lety

    Great work man.

  • @markbones00
    @markbones00 Před 11 lety

    i tried, 55 is at index 4 and 98 is at index 7...

  • @Kiran200002
    @Kiran200002 Před 11 lety

    very nice presentation. Thankyou

  • @techwithehusain5967
    @techwithehusain5967 Před 6 lety +1

    You are awesome. Honestly!!

  • @bermudatriangle94
    @bermudatriangle94 Před 4 lety

    dude you are amazing!

  • @roasted_guava5706
    @roasted_guava5706 Před 4 lety

    This is so helpful! Thanks!

  • @ariskoutsoukis7849
    @ariskoutsoukis7849 Před 5 lety

    Why you update low and high to mid+1 or mid-1; and not just update low or high to mid; see for 55 would much faster

  • @kironsaha2679
    @kironsaha2679 Před 4 lety +1

    Amazing video, helped a lot for a project.

    • @abdurraheem2444
      @abdurraheem2444 Před 4 lety

      your project was based upon binary search only🤣🤣

  • @Jordan-og5kd
    @Jordan-og5kd Před 6 lety +1

    great explanation

  • @dawitmekuria5349
    @dawitmekuria5349 Před 5 lety

    Sir, that's nice video but it not working for descending array values. Any solution for that?

    • @kkn5523
      @kkn5523 Před 5 lety

      Reverse that array by storing it in another array like this:
      Say your original array is:array1[size];
      int array2[size],k=0;
      for(int i=size-1;i>=0;i--)
      {
      array2[k]=array1[i];
      k++;
      }
      Apply the sort on array2. That' s what i do. Hope it helps! You can also manipulate the binary sort algorithm but this method seems simpler

  • @halah1995
    @halah1995 Před 8 lety

    Thank you very much sir.

  • @kpippink
    @kpippink Před 10 lety

    Thank you for posting :D

  • @efim_bistrov
    @efim_bistrov Před 11 lety

    Thank you very much, it helped me with homework)

  • @jeongmooyoo8692
    @jeongmooyoo8692 Před 6 lety

    very well explained~!!!

  • @ledues3336
    @ledues3336 Před 7 lety

    10:32, what if an overflow happens?

  • @hilaritas1544
    @hilaritas1544 Před 8 lety

    Can anybody explain me, why "return -1"?

  • @ankitkumardubey0095
    @ankitkumardubey0095 Před 5 lety

    Thank you so much brother.

  • @braindeadjoe
    @braindeadjoe Před 9 lety

    Great work, this really helped me out thank you! Instant like!

  • @alsayedalsisi2709
    @alsayedalsisi2709 Před 7 lety

    But now what would the purpose of the binary search algorithm if the array is not sorted?? The binary search algorithm forces you to have the array sorted, and if you have to sort the array then it would be faster to use linear search than sorting then searching the array.

    • @aNz0r2
      @aNz0r2 Před 6 lety

      Let's assume that you have your target value at the end of your brute (unsorted) array. Linear search will be in worst case, which will give maximum complexity. Meanwhile, a quicksort function before using binary search will be much more efficient, as the complexity will be at least modest.
      Sorting an array doesn't always mean using 2 for() loops.

    • @nexgen2816
      @nexgen2816 Před 6 lety

      you can use bubble sort algorithm to sort it before search

  • @rehanasghar5181
    @rehanasghar5181 Před 9 lety

    Hey thanks for this great video. It clears my confusions
    Can you please tell me how do you edit this video?
    What software you were using to teach us by writing on screen ?

  • @debashismondal7536
    @debashismondal7536 Před 7 lety

    what software are you using sir?

  • @jasonpinto8664
    @jasonpinto8664 Před 7 lety +1

    excellent!!

  • @blind_neighbourhoodNerd

    Thanks so much for this!!

  • @user-yk3sf9is2u
    @user-yk3sf9is2u Před 8 lety

    Great video

  • @nouraaliabuhlega4023
    @nouraaliabuhlega4023 Před 5 lety

    great work ,

  • @aarjavbaxi2299
    @aarjavbaxi2299 Před 3 lety +1

    It was really cool ! But here is the question though : What if the list/array is unsorted ; I mean if there is a large search space , say 50000 elements ; would we be able to sort it manually ? - NO. So, why don't we have a function for sorting too?
    Thinking practically , there are not gonna be arrays with just 8 or 10 or 50 elements, so i think we need it.
    Comment down your thoughts on this. :)

    • @LandonGernand
      @LandonGernand Před 3 lety +2

      there are functions for sorting collections called sorting algorithms. there are a lot of good videos on youtube about them

  • @4sky
    @4sky Před 10 lety

    yo dis my go to video for binary search

  • @jasonderero3922
    @jasonderero3922 Před 7 lety

    what happened to you why did you disappear we missed you

  • @hilaritas1544
    @hilaritas1544 Před 8 lety

    Thank you, man!

  • @talhaghaffar1324
    @talhaghaffar1324 Před 10 lety

    sir 1 question can u tell me the name of your compiler. which u use in that video...

    • @gluedtogames
      @gluedtogames Před 10 lety +1

      Since he didn't ever get back to you, it's Eclipse.

    • @ledues3336
      @ledues3336 Před 7 lety

      Gabe Payne that's not a compiler

    • @ledues3336
      @ledues3336 Před 7 lety

      Talha Ghaffar gcc

  • @stephenkamenar
    @stephenkamenar Před 11 lety

    Whoa, has nobody noticed that what he's showing doesn't even work? Using his example array, try searching for 55, or 98; it can't find it. The check for (low >= high) needs to be just after you check (value == arr[mid])

  • @superqaxclub
    @superqaxclub Před 5 lety

    Very helpful

  • @fisslewine1222
    @fisslewine1222 Před 8 lety

    Good tutorial.

  • @AnasMations
    @AnasMations Před 3 lety

    you're awesome!

  • @hanzalajamash5376
    @hanzalajamash5376 Před 7 lety

    Thank you Brother

  • @rupal3628
    @rupal3628 Před 9 lety

    Thank you!!

  • @ayuparpe1580
    @ayuparpe1580 Před 6 lety

    how about this way no function than main is used .
    int n, i, arr[50], search, first, last, middle;
    coutn;
    cout

  • @satyamjindal8483
    @satyamjindal8483 Před 8 lety

    Amazing!!

  • @Ruhgtfo
    @Ruhgtfo Před 7 lety

    Thanks Sir~

  • @eXeMutey
    @eXeMutey Před 6 lety

    Thanks a lot man.

  • @mosdomveteran9323
    @mosdomveteran9323 Před 4 lety

    Belissimo!

  • @bellsandoor
    @bellsandoor Před 7 lety

    you are amazing :)

  • @davidgaster
    @davidgaster Před 7 lety

    Note that the way you are updating hi and lo means there could be integer overflow. In practice you should use high/2 + low/2. Using (high + low)/2 could potentially cause an overflow if high + low is larger than the maximum representable value: 32 bits = 2^32 -1.

    • @ledues3336
      @ledues3336 Před 7 lety

      DaveyJones I commented that too! I didn't think about how to do it otherwise, thanks

    • @AlyssaMarie-vr8cc
      @AlyssaMarie-vr8cc Před rokem

      Ok, interesting - I thought it was mid= low+(high-low)/2 -- is this the same thing as high/2 + low/2 ??

  • @raoulazimullah1898
    @raoulazimullah1898 Před 11 lety

    Big Up!

  • @ramzimohammed3580
    @ramzimohammed3580 Před 7 měsíci

    Thanks

  • @yemimasandra4796
    @yemimasandra4796 Před 4 lety

    thanks u save my life :')

  • @nguyenvo7953
    @nguyenvo7953 Před 4 lety

    thanks

  • @crummmycheese
    @crummmycheese Před 11 lety

    execellent