נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

אלגוריתמים וסיבוכיותAlgorithms & complexity

מפגש 34: חיפוש בינארי, מיון, Big-O ובחירת מבנה נתונים.Session 34: Binary search, sorting, Big-O and choosing a data structure.

מה נעשה היוםToday's plan

  • למהWhy
  • Big OBig O
  • בינאריBinary
  • מבניםStructures
  • מיוןSorting
  • רקורסיהRecursion
נושאTopic 1

למהWhy

למה יעילות?Why efficiency?

  • אלגוריתם = מתכוןAn algorithm = a recipe
  • ב-10 ספרים הכול מהירWith 10 books everything is fast
  • ב-מיליון, ההבדל בין שנייה לשעהWith a million, the gap between a second and an hour

אנלוגיהAnalogy

לחפש ספר מדף אחרי מדף זה כמו לחפש מכונית בחניון ענק שורה אחרי שורה. אם יודעים את מספר הקומה והשורה, מגיעים מיד.Searching shelf by shelf is like hunting for your car in a huge car park row by row. If you know the floor and row, you're there at once.
נושאTopic 2

Big OBig O

Big OBig O

  • O(1): קבוע, לא תלוי בגודלO(1): constant, independent of size
  • O(n): לולאה אחתO(n): one loop
  • O(n²): לולאה בתוך לולאהO(n²): a loop inside a loop

אנלוגיהAnalogy

O(n) הוא כמו לחלק עלונים בכל בית ברחוב: רחוב כפול, עבודה כפולה. O(n²) הוא כמו שכל שכן יכיר כל שכן: רחוב כפול, פי ארבעה היכרויות.O(n) is like delivering flyers to every house on a street: double the street, double the work. O(n²) is like every neighbour meeting every neighbour: double the street, four times the introductions.
נושאTopic 3

בינאריBinary

חיפוש בינאריBinary search

  • רק ברשימה ממוינתOnly on a sorted list
  • בודקים אמצע, זורקים חציCheck the middle, throw away half
  • מיליון → 20 צעדיםA million → 20 steps

אנלוגיהAnalogy

חיפוש בינארי הוא כמו משחק "גבוה-נמוך" עם מספר בין 1 למיליון: כל ניחוש באמצע חוצה את האפשרויות, ותוך 20 ניחושים מגיעים.Binary search is like playing "higher or lower" with a number between 1 and a million: each middle guess halves the options, and 20 guesses get you there.
נושאTopic 4

מבניםStructures

מבנה הנתונים הנכוןThe right data structure

  • רשימה: סדר, אינדקס. in הוא O(n)List: order, index. in is O(n)
  • set ו-dict: גיבוב, in הוא O(1)set and dict: hashing, in is O(1)
  • Counter, deque, heapq לצרכים מיוחדיםCounter, deque, heapq for special needs

אנלוגיהAnalogy

רשימה היא כמו ערימת מכתבים: כדי למצוא אחד מחפשים. dict הוא כמו תיבות דואר עם שמות: הולכים ישר לתיבה הנכונה.A list is like a pile of letters: to find one, you search. A dict is like named mailboxes: walk straight to the right box.
נושאTopic 5

מיוןSorting

מיוןSorting

  • מיון בועות: O(n²), טוב ללמידהBubble sort: O(n²), good for learning
  • sorted של פייתון: O(n log n)Python's sorted: O(n log n)
  • key= לכל סדר שרוציםkey= for any order you want

אנלוגיהAnalogy

מיון בועות הוא כמו לסדר ילדים בתור לפי גובה כשכל אחד יכול רק להתחלף עם השכן שלו: זה עובד, אבל לוקח הרבה סיבובים.Bubble sort is like lining children up by height when each may only swap with a neighbour: it works, but takes many rounds.

טעות נפוצהCommon mistake

sorting.py
print(sorted([412, "96", 240]))
מה Python עונהWhat Python says
TypeError: '<' not supported between instances of 'str' and 'int'
TypeError: '<' not supported between instances of 'str' and 'int': פייתון לא יודעת אם "96" קטן מ-412. ממירים קודם: sorted(int(x) for x in items).TypeError: '<' not supported between instances of 'str' and 'int': Python can't tell whether "96" is less than 412. Convert first: sorted(int(x) for x in items).
נושאTopic 6

רקורסיהRecursion

רקורסיהRecursion

  • פונקציה שקוראת לעצמהA function that calls itself
  • חייב מקרה בסיסIt must have a base case
  • מתאים לעצים ותיקיותFits trees and folders

אנלוגיהAnalogy

רקורסיה היא כמו בובות בבושקה: כדי לספור את כולן, פותחים בובה, סופרים אותה, ומבקשים מהבובה שבתוכה לספור את עצמה.Recursion is like Russian dolls: to count them all, open a doll, count it, and ask the doll inside to count itself.

טעות נפוצהCommon mistake

recursion.py
def countdown(n):
    countdown(n - 1)

countdown(5)
מה Python עונהWhat Python says
RecursionError: maximum recursion depth exceeded
RecursionError: maximum recursion depth exceeded: אין תנאי עצירה, והפונקציה ממשיכה לקרוא לעצמה עד שפייתון עוצרת אותה (בערך 1000 רמות). מוסיפים if n == 0: return.RecursionError: maximum recursion depth exceeded: there's no stopping condition, so the function keeps calling itself until Python stops it (about 1000 levels). Add if n == 0: return.

אלגוריתם = מתכוןAn algorithm = a recipe

ב-10 ספרים הכול מהירWith 10 books everything is fast

ב-מיליון, ההבדל בין שנייה לשעהWith a million, the gap between a second and an hour

O(1): קבוע, לא תלוי בגודלO(1): constant, independent of size

O(n): לולאה אחתO(n): one loop

O(n²): לולאה בתוך לולאהO(n²): a loop inside a loop

רק ברשימה ממוינתOnly on a sorted list

בודקים אמצע, זורקים חציCheck the middle, throw away half

מיליון → 20 צעדיםA million → 20 steps

רשימה: סדר, אינדקס. in הוא O(n)List: order, index. in is O(n)

set ו-dict: גיבוב, in הוא O(1)set and dict: hashing, in is O(1)

Counter, deque, heapq לצרכים מיוחדיםCounter, deque, heapq for special needs

מיון בועות: O(n²), טוב ללמידהBubble sort: O(n²), good for learning

sorted של פייתון: O(n log n)Python's sorted: O(n log n)

key= לכל סדר שרוציםkey= for any order you want

פונקציה שקוראת לעצמהA function that calls itself

מה למדנוWhat we learned

  • אלגוריתם = מתכוןAn algorithm = a recipe
  • O(1): קבוע, לא תלוי בגודלO(1): constant, independent of size
  • רק ברשימה ממוינתOnly on a sorted list
  • רשימה: סדר, אינדקס. in הוא O(n)List: order, index. in is O(n)
  • מיון בועות: O(n²), טוב ללמידהBubble sort: O(n²), good for learning
  • פונקציה שקוראת לעצמהA function that calls itself

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich