Computer Science

IT Matura Course | Algorithms #2


categories: computer science

Hello everyone,
Below you will find a video with a detailed explanation of the binary search algorithm. Its operation, time complexity, and usefulness. Below you will also find ready-made code to copy if needed.
Code:


a = [1, 2, 3, 4, 5]

#------------------------------
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#------------------------------
def binarySearch(x, a):
    if x < a[0]:
        return False
    elif x > a[len(a) - 1]:
        return False

    poczatek = 0
    koniec = len(a)

    while poczatek != koniec:
        srodek = (poczatek + koniec) // 2
        if x < a[srodek]:
            koniec = srodek - 1
        elif x > a[srodek]:
            poczatek = srodek + 1
        if x == a[srodek]:
            return True

    return False

if binarySearch(b[4], a):
    print("tak")
else:
    print("nie")
                  

Video:

Thank you for reading!


Read more
Administrator

This post was written by the administrator

Recent Posts

Proof Problems in Mathematics The Beginning Integrals, Course Practical Physics for Engineers IT Matura Course | Algorithms IT Matura Course | Databases IT Matura Course | Theory IT Matura Course | Spreadsheet

Archive

Year 2022

Comments