Тёмный

Binary Search: Background & Python Code 

Brian Faure
Подписаться 13 тыс.
Просмотров 19 тыс.
50% 1

Code below... Continuing our series investigating algorithms in Python, in this video we'll cover the Binary Search, a highly efficient searching technique that only applies under certain conditions. In the beginning of the video we'll go over the basics of the binary search, and later we'll open up our coding editor and actually implement the algorithm using Python.
► Python 2 github.com/bfaure/Python_Algo...
► Python 3 github.com/bfaure/Python_Algo...
► Python Data Structures: • Python Data Structures...
► Video series covering GUI development in Python (WIP): • Python GUI Development...
References:
[1] en.wikipedia.org/wiki/Binary_...
[2] en.wikipedia.org/wiki/Search_...
[3] en.wikipedia.org/wiki/Linear_...
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.
Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm. Binary search takes constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number of elements in the array. Although specialized data structures designed for fast searching-such as hash tables-can be searched more efficiently, binary search applies to a wider range of problems.

Опубликовано:

 

10 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 37   
@pdeezz
@pdeezz 6 лет назад
Clear and concise. Subscribed
@BrianFaure1
@BrianFaure1 6 лет назад
Thanks, glad I could help!
@yvonnegrealy
@yvonnegrealy 5 лет назад
Your videos are great Brian, really helpful. Keep up the good work!
@AmanVerma-ci3kg
@AmanVerma-ci3kg 6 лет назад
Brilliant Explanation .
@AlexandrBorschchev
@AlexandrBorschchev 3 года назад
thank you so much bro. im watch your videos and theyre veryy helpful.
@sabrinaoliver2181
@sabrinaoliver2181 6 лет назад
Thank you!! This was so helpful.
@BrianFaure1
@BrianFaure1 6 лет назад
Happy to hear it, thanks for watching!
@mreagle4798
@mreagle4798 6 лет назад
cheers this really helped!!!
@BiswaRanjanNanda
@BiswaRanjanNanda 5 лет назад
WOW.. you rock !! you just earned a subscriber mate.
@lamba5945
@lamba5945 5 лет назад
Hi, I'm from Russia and there is no such good videos in russian RU-vid. Thanks for explanation.
@afrojacky2201
@afrojacky2201 6 лет назад
This is amazing Thanks :)
@xreed8
@xreed8 6 лет назад
FYI, in cases of odd-numbered lists, passing a float wont work in Python 3.6+. You would have to convert the float to an integer first to get this to work (using int(), which will always round it down). Apparently it's ok in Python 2.7 as you demonstrated.
@BrianFaure1
@BrianFaure1 6 лет назад
Oh interesting, thanks for pointing this out Xavier.
@jatinkrmalik
@jatinkrmalik 6 лет назад
or just use // for divide instead of /. Ex: mid = arr[len(arr)//2], this will take care of float.
@MahiAbrarAmer
@MahiAbrarAmer 6 лет назад
Thanks a bunch! Subscribed. Btw can you do linked lists too? And maybe a few more from the cambridge A2 9608 syllabus ;-; theres so little resource for python on it..
@BrianFaure1
@BrianFaure1 6 лет назад
Awesome, thanks for watching! I've got a video on linked lists already actually ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JlMyYuY1aXU.html , I've got a couple vids in the pipeline already but I'll check out the syllabus and add in anything I haven't covered.
@MahiAbrarAmer
@MahiAbrarAmer 6 лет назад
Thanks a bunch
@Neha_H46
@Neha_H46 5 лет назад
Thanks, explanation is to the point. Can you let us know how to implement this with string data type? That is searching for a string in an array of strings.
@BrianFaure1
@BrianFaure1 5 лет назад
Thanks Tanuja! It actually already works with strings, just make sure the list you're searching through is in order. This works because the built-in Python comparators () work with both numbers and strings so you don't even need to change any of the code.
@StoneSSSSS
@StoneSSSSS 4 года назад
I accidentally created this without know it existed
@OcaOca
@OcaOca 5 лет назад
midpoint calculation should use integer division operator //
@dylanmaulucci968
@dylanmaulucci968 5 лет назад
Yep. I ran into Error with just /. Thanks!
@OcaOca
@OcaOca 5 лет назад
@@dylanmaulucci968 it's only for python3 though! So OP probably on py2
@notrllycomedy
@notrllycomedy 5 лет назад
how do you type so fast
@jl_woodworks
@jl_woodworks 4 года назад
Well, why would I want to implement binary search just to return a boolean when I can use "in" for that? Nice video, but I think it's weird to return a boolean instead of the target's index on the list.
@vinoddiwan5792
@vinoddiwan5792 4 года назад
It just depends on demand. you take boolean or index
@Man0fSteell
@Man0fSteell 6 лет назад
can you please post the code for python 3
@BrianFaure1
@BrianFaure1 6 лет назад
Just added it here github.com/bfaure/Python_Algorithms/blob/master/binary_search/Python3/main.py , thanks for watching and for the comment!
@aadishjain6838
@aadishjain6838 6 лет назад
TypeError: unsupported operand type(s) for /: 'list' and 'int' im getting this error,plz help
@BrianFaure1
@BrianFaure1 6 лет назад
Could you post a link to your code?
@aadishjain6838
@aadishjain6838 6 лет назад
oh,i did not use (a[:len(a)//2],value) this before,can u tell me the use of ":" it??
@BrianFaure1
@BrianFaure1 6 лет назад
The ':' for lists is used for slicing off a portion of the list. For example if you had the list a=[1,2,3,4,5,6], "a[0:1]" would return [1], "a[0:2]" would return [1,2], "a[0:3]" would return [1,2,3] and so on. If you omit the first index (such as in a[:len(a//2)] it's the same as having a 0, so a[0:2] is the same as a[:2]. Another cool feature is that you can index up to the end of the list using negative numbers; so a[:-1] would return everything but the last element ([1,2,3,4,5]) and a[:-2] would return [1,2,3,4].
@aadishjain6838
@aadishjain6838 6 лет назад
wow,thankyou so much!
@Smile1oasis
@Smile1oasis 4 года назад
My doubt is like, When I'm searching for a number which is the 0th item of a 10000+ long list, then this binary search won't be effective right? 😁
@andrewagita901
@andrewagita901 4 года назад
big o notation
@nikosiliadis3954
@nikosiliadis3954 5 лет назад
check hear: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SICY5NroY2M.html&t
@jeremiaskotchinski2073
@jeremiaskotchinski2073 4 года назад
Please speak sloooower!
Далее
Sieve of Eratosthenes: Background & Python Code
14:42
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
Python Data Structures #5: Binary Search Tree (BST)
31:54
Compiled Python is FAST
12:57
Просмотров 105 тыс.
Sorting Algorithms Explained Visually
9:01
Просмотров 527 тыс.
Graham Scan: Background & Python Code
14:10
Просмотров 20 тыс.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
AVL Tree: Background & Python Code
24:24
Просмотров 45 тыс.
R vs Python
7:07
Просмотров 315 тыс.