No video

C++ Programming: Binary Search Algorithm

  Рет қаралды 218,746

ReelLearning

ReelLearning

Күн бұрын

Code can be found at pastebin.com/fs...
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).

Пікірлер: 144
@christianvillamor6273
@christianvillamor6273 11 жыл бұрын
WOW. Thank you! Programmers tend to be really snobby when it comes to helping. WE NEED MORE PROGRAMMERS LIKE YOU!
@yasina63
@yasina63 Жыл бұрын
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.
@studyfreak8429
@studyfreak8429 4 ай бұрын
this is year 2024 and this stuff is still relevant, man c++ IS evergreen
@davidporterrealestate
@davidporterrealestate 7 жыл бұрын
Thank God you have an American accent
@jahitrst2863
@jahitrst2863 4 жыл бұрын
Xdd haha
@temirlanmukatay1765
@temirlanmukatay1765 4 жыл бұрын
👍
@4XD45
@4XD45 4 жыл бұрын
it's interesting that it's always the indians making these types of tutorials.
@rednecksegexe4438
@rednecksegexe4438 3 жыл бұрын
Stfu
@darkwolf988ify
@darkwolf988ify 2 жыл бұрын
@@4XD45 ikr?
@uzumaki9t
@uzumaki9t 8 жыл бұрын
Very well explained. I thought binary search was something awful when I saw it on my last exam. Thank you very much!
@bilalahmad-xp9ck
@bilalahmad-xp9ck 7 жыл бұрын
Tarik Đulić yes
@mountgraph1403
@mountgraph1403 6 жыл бұрын
Pls dont stop making this videos. They really help!
@NatsukiSY
@NatsukiSY 2 жыл бұрын
I love you man Even my doctor couldn't make it this simple
@opiumslave
@opiumslave 4 жыл бұрын
I've my Computer Science boards practical exam on 1st Feb. U helped me a lot.... Thanks man
@eaglebaize
@eaglebaize 11 жыл бұрын
Thank you so much! I have a comprehensive c++ final today (it will cover material from both this and the previous semesters). Cheers!
@rajcodes100
@rajcodes100 6 жыл бұрын
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.
@snipere2009
@snipere2009 8 жыл бұрын
 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.
@babbalgts
@babbalgts 8 жыл бұрын
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.
@vulcuemil4367
@vulcuemil4367 4 жыл бұрын
When dealing with huge arrays you risk to overflow when calculating mid. A safer way is: mid = low + (high - low) / 2 .
@Asterite
@Asterite 3 жыл бұрын
what does overflowing mean?
@grigorebordea1212
@grigorebordea1212 5 жыл бұрын
Thank you so much i've tried to understand this algorithm for 2 days even though this isnt that hard
@lamp_x
@lamp_x Жыл бұрын
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)
@stevezes
@stevezes 5 жыл бұрын
your mid point calculation can cause overflow
@aarjavbaxi2299
@aarjavbaxi2299 3 жыл бұрын
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 3 жыл бұрын
there are functions for sorting collections called sorting algorithms. there are a lot of good videos on youtube about them
@corrondo25
@corrondo25 8 жыл бұрын
watch 4 videos looking for a simple detail concerning the algorithm. You had it.
@user-kr1wc6kk8g
@user-kr1wc6kk8g 3 ай бұрын
admiration come it self if you competent like you .thanks
@spicytuna08
@spicytuna08 6 жыл бұрын
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.
@Lolablue1986
@Lolablue1986 7 жыл бұрын
Great video been looking for something like this all day lol
@talhaghaffar1324
@talhaghaffar1324 10 жыл бұрын
my great video lecture ever...
@rafiullahqallander
@rafiullahqallander 8 жыл бұрын
I just can say, you are THE BEST. Many thanks for your great contribution, May God bless you :)
@delvindavid2804
@delvindavid2804 7 жыл бұрын
Thank You so much Derek, I wouldn't be able to answer this question in exam ,if I had not found your channel!
@cristopheririas1509
@cristopheririas1509 5 жыл бұрын
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!
@WalterCueva
@WalterCueva 10 жыл бұрын
I have a Two-Dimensional Arrays. Do you have a video showing how to use linear and binary search on a Two-Dimensional Arrays?
@robbiecastillo7495
@robbiecastillo7495 7 жыл бұрын
Well done sir! Very helpful.
@sakspan3265
@sakspan3265 10 жыл бұрын
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 3 жыл бұрын
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.
@davidgaster
@davidgaster 7 жыл бұрын
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 7 жыл бұрын
DaveyJones I commented that too! I didn't think about how to do it otherwise, thanks
@AlyssaMarie-vr8cc
@AlyssaMarie-vr8cc Жыл бұрын
Ok, interesting - I thought it was mid= low+(high-low)/2 -- is this the same thing as high/2 + low/2 ??
@snyfalcryo524
@snyfalcryo524 4 жыл бұрын
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 3 жыл бұрын
for example if you are looking for 2 numbers you can give search value as input 2 times and run code 2 times ..
@Brad6013-
@Brad6013- Жыл бұрын
You explained this very well. Thank you
@Gooneryz
@Gooneryz 11 жыл бұрын
@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?
@bluesaint9163
@bluesaint9163 8 жыл бұрын
How could I store all the words in my project? if I will go for a dcitonary?
@raghurammuthyala1023
@raghurammuthyala1023 8 жыл бұрын
Great video! Amazing! Thank you for uploading sir! Sir which one do you think is the best compiler ( user friendly and easy to understand)?
@arumaibright5302
@arumaibright5302 7 жыл бұрын
code blocks is fantastic
@kironsaha2679
@kironsaha2679 4 жыл бұрын
Amazing video, helped a lot for a project.
@abdurraheem2444
@abdurraheem2444 4 жыл бұрын
your project was based upon binary search only🤣🤣
@aovlover415
@aovlover415 6 жыл бұрын
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 :)
@hokutoueda6215
@hokutoueda6215 4 жыл бұрын
you are wonderful person and i really like your videos.
@ranjhi369
@ranjhi369 12 жыл бұрын
excellent and simplified presentation. thanx
@escaravar7417
@escaravar7417 9 жыл бұрын
Thank you for this video, it's so helpful!!
@1314rom
@1314rom 10 жыл бұрын
Awesome video. Clear and to the point.
@TheVerbalAxiom
@TheVerbalAxiom 9 жыл бұрын
Wonderful, perfect explanation.
@billzhang1892
@billzhang1892 11 жыл бұрын
Thanks very much for so simplified and excellent lecture!
@DesignAndDevops
@DesignAndDevops Жыл бұрын
What about last element
@alsayedalsisi2709
@alsayedalsisi2709 7 жыл бұрын
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 6 жыл бұрын
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 6 жыл бұрын
you can use bubble sort algorithm to sort it before search
@ariskoutsoukis7849
@ariskoutsoukis7849 5 жыл бұрын
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
@salehabuhussein5229
@salehabuhussein5229 3 жыл бұрын
what software do you use to run cpp file?
@ntobekodlamini7027
@ntobekodlamini7027 10 күн бұрын
code-blocks
@stephenkamenar
@stephenkamenar 11 жыл бұрын
Oops, I think that was just a typo on my part and it still doesn't work, right?
@AhmedHadiPADI_scuba_instructor
@AhmedHadiPADI_scuba_instructor 7 жыл бұрын
what the result of log2(64000) represents ? i mean the 15.966. thanks a lot !
@phatchunk99
@phatchunk99 7 жыл бұрын
It takes 16 loops to search a number within an array of 64000 numbers.
@whateveriwannabe1
@whateveriwannabe1 7 жыл бұрын
These are very helpful videos
@kingsapo
@kingsapo 11 жыл бұрын
Thanks a bunch, got really stuck on this, your video helped a lot!
@fortunesuwedi6714
@fortunesuwedi6714 10 ай бұрын
Good explanation
@stephenkamenar
@stephenkamenar 11 жыл бұрын
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])
@talhaghaffar1324
@talhaghaffar1324 10 жыл бұрын
sir 1 question can u tell me the name of your compiler. which u use in that video...
@gluedtogames
@gluedtogames 10 жыл бұрын
Since he didn't ever get back to you, it's Eclipse.
@ledues3336
@ledues3336 7 жыл бұрын
Gabe Payne that's not a compiler
@ledues3336
@ledues3336 7 жыл бұрын
Talha Ghaffar gcc
@hyunbinpark3382
@hyunbinpark3382 7 жыл бұрын
Thank you for posting!
@tarikabughalib1263
@tarikabughalib1263 Жыл бұрын
Thank you so much this is unbelievable helpful
@erictronic
@erictronic 8 жыл бұрын
Thank you for great lesson!
@dawitmekuria5349
@dawitmekuria5349 5 жыл бұрын
Sir, that's nice video but it not working for descending array values. Any solution for that?
@kkn5523
@kkn5523 5 жыл бұрын
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
@markbones00
@markbones00 11 жыл бұрын
i tried, 55 is at index 4 and 98 is at index 7...
@Kiran200002
@Kiran200002 11 жыл бұрын
very nice presentation. Thankyou
@JBMJaworski
@JBMJaworski 11 жыл бұрын
Thank you for sharing good quality teaching. :-) Regards! Jarek Jaworski
@notarealhandle123
@notarealhandle123 9 жыл бұрын
This is an incomplete explanation, because you rely on your C++ compiler's functionality in converting variable mid into the index. You need to be explicit about what index is actually being used, because it may be different in other compilers and certainly in other languages, which in turn changes the algorithm outcome completely. Is it FLOOR, CEIL or ROUND? - one must know in order to adopt the algorithm in other systems. For example, in Java Script you have to make this change: mid = Math.round((low + high) / 2); otherwise the algorithm won't work.
@rehanasghar5181
@rehanasghar5181 9 жыл бұрын
Vitaly Tomilov Do you read the title of this video ??? It is about the binary search uses in C++ language , not in java ....
@notarealhandle123
@notarealhandle123 9 жыл бұрын
rehan asghar These tutorials are all about algorithms, not C++ language. They are meant to be applicable to any platform. The fact that C++ helps them hide some of the very important logic of the algorithm doesn't really help others understand how it works.
@aarjavbaxi2299
@aarjavbaxi2299 3 жыл бұрын
@@notarealhandle123 Maybe you shouldn't have come here if you didn't want c++ to be the language .
@debashismondal7536
@debashismondal7536 7 жыл бұрын
what software are you using sir?
@davidgaster
@davidgaster 7 жыл бұрын
He's using Eclipse
@HarisHussainkhan
@HarisHussainkhan 10 жыл бұрын
Thanxz very Good Instructions Given ..... Thank you Very Much.....
@rehanasghar5181
@rehanasghar5181 9 жыл бұрын
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 ?
@NadaAhmed-zx1ru
@NadaAhmed-zx1ru 10 жыл бұрын
This was great and simply explained, than you :)
@techwithehusain5967
@techwithehusain5967 6 жыл бұрын
You are awesome. Honestly!!
@roasted_guava5706
@roasted_guava5706 4 жыл бұрын
This is so helpful! Thanks!
@ayuparpe1580
@ayuparpe1580 6 жыл бұрын
how about this way no function than main is used . int n, i, arr[50], search, first, last, middle; coutn; cout
@4sky
@4sky 11 жыл бұрын
yo dis my go to video for binary search
@halah1995
@halah1995 9 жыл бұрын
Thank you very much sir.
@ledues3336
@ledues3336 7 жыл бұрын
10:32, what if an overflow happens?
@sakirarahman3091
@sakirarahman3091 4 жыл бұрын
Low + (high - low) / 2
@millionasseghegn770
@millionasseghegn770 Жыл бұрын
now my eyes are widely open
@blind_neighbourhoodNerd
@blind_neighbourhoodNerd 5 жыл бұрын
Thanks so much for this!!
@bermudatriangle94
@bermudatriangle94 4 жыл бұрын
dude you are amazing!
@hilaritas1544
@hilaritas1544 8 жыл бұрын
Can anybody explain me, why "return -1"?
@sawyermcbride1522
@sawyermcbride1522 8 жыл бұрын
+Hilaritas if the element is not found
@aliabid2839
@aliabid2839 5 жыл бұрын
Great work man.
@jasonderero3922
@jasonderero3922 7 жыл бұрын
what happened to you why did you disappear we missed you
@Jordan-og5kd
@Jordan-og5kd 6 жыл бұрын
great explanation
@efim_bistrov
@efim_bistrov 11 жыл бұрын
Thank you very much, it helped me with homework)
@ankitkumardubey0095
@ankitkumardubey0095 5 жыл бұрын
Thank you so much brother.
@markbones00
@markbones00 11 жыл бұрын
It's low
@jeongmooyoo8692
@jeongmooyoo8692 6 жыл бұрын
very well explained~!!!
@jasonpinto8664
@jasonpinto8664 7 жыл бұрын
excellent!!
@kpippink
@kpippink 10 жыл бұрын
Thank you for posting :D
@superqaxclub
@superqaxclub 5 жыл бұрын
Very helpful
@braindeadjoe
@braindeadjoe 9 жыл бұрын
Great work, this really helped me out thank you! Instant like!
@ramzimohammed3580
@ramzimohammed3580 8 ай бұрын
Thanks
@nouraaliabuhlega4023
@nouraaliabuhlega4023 5 жыл бұрын
great work ,
@user-yk3sf9is2u
@user-yk3sf9is2u 9 жыл бұрын
Great video
@hanzalajamash5376
@hanzalajamash5376 7 жыл бұрын
Thank you Brother
@fisslewine1222
@fisslewine1222 8 жыл бұрын
Good tutorial.
@nguyenvo7953
@nguyenvo7953 4 жыл бұрын
thanks
@AnasMations
@AnasMations 3 жыл бұрын
you're awesome!
@hilaritas1544
@hilaritas1544 8 жыл бұрын
Thank you, man!
@rupal3628
@rupal3628 9 жыл бұрын
Thank you!!
@shtprof2000
@shtprof2000 5 жыл бұрын
What's up Derek from speech class
@Ruhgtfo
@Ruhgtfo 7 жыл бұрын
Thanks Sir~
@eXeMutey
@eXeMutey 6 жыл бұрын
Thanks a lot man.
@satyamjindal8483
@satyamjindal8483 8 жыл бұрын
Amazing!!
@mosdomveteran9323
@mosdomveteran9323 4 жыл бұрын
Belissimo!
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 159 М.
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 77 МЛН
Мы сделали гигантские сухарики!  #большаяеда
00:44
Ik Heb Aardbeien Gemaakt Van Kip🍓🐔😋
00:41
Cool Tool SHORTS Netherlands
Рет қаралды 9 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 795 М.
Binary Search tutorial (C++ and Python)
27:41
Errichto Algorithms
Рет қаралды 254 М.
Learn Binary Search in 10 minutes 🪓
10:04
Bro Code
Рет қаралды 110 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Quicksort | C++ Example
26:50
Portfolio Courses
Рет қаралды 7 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
The size of your variables matters.
11:03
Core Dumped
Рет қаралды 113 М.
Running "Hello World!" in 10 FORBIDDEN Programming Languages
18:07