Comparing algorithms and ADTs in algorithms
הוצג
15:34
חיפוש ומיון
ספר טלפונים עם מיליון שמות. אם תבדוק אותם אחד אחד, ייתכן שתבצע מיליון השוואות. אבל כבר ידוע לך הטריק: פתח אותו בחצי…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| hardware/ˈhɑːdweə/ | The physical parts of a computer system that can be touched. |
| complexity analysis | The study of how an algorithm's time or memory needs grow with input size. |
| Big-O/bɪɡ əʊ/ | A way to describe an upper limit on how an algorithm's resource needs grow. |
| ADTs | Types of data defined by their values and allowed operations, without saying how they are built. |
| Time complexity/taɪm kəmˈpleksɪti/ | How an algorithm's running time grows as its input size increases. |
| running time | The amount of time an algorithm takes to finish its work. |
| input size | The amount of data given to an algorithm to process. |
| dominant term | The part of an expression that grows fastest as input size increases. |
| constant/ˈkɒnstənt/ | A quantity whose value remains fixed within a given problem, expression or program. |
| logarithmic/ˌlɒɡəˈrɪθmɪk/ | Involving logarithms, which describe the exponents needed to obtain numbers from a given base. |
| binary search/ˈbaɪnəri sɜːtʃ/ | A search of ordered data that repeatedly compares with the middle item and discards half of the remaining search range. |
| linear/ˈlɪnɪə/ | Describing growth that is proportional to input size, so doubling the input approximately doubles the amount of work for large inputs. |
| linear search/ˈlɪnɪə sɜːtʃ/ | A search that checks items one by one until it finds the target or reaches the end. |
| quadratic/kwɒˈdrætɪk/ | Describes growth that follows the square of the input size. |
| bubble and insertion sort | Two sorting algorithms whose running times can grow with the square of the input size. |
| Space complexity/speɪs kəmˈpleksɪti/ | The extra memory an algorithm needs, beyond the input itself. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| in place/ɪn pleɪs/ | Describes an algorithm that rearranges its input using only a small amount of extra storage. |
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| Merge sort/mɜːdʒ sɔːt/ | A sorting algorithm that divides data into smaller parts, sorts them, and joins them in order. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| Recursion/rɪˈkɜːʃn/ | The use of a rule inside its own definition to express repetition. |
| stack memory | Memory that holds information about function calls that have not yet finished. |
| frame/freɪm/ | A section of stack memory holding information for one function call. |
| time and memory trade-off | A choice between using more memory to save time and using less memory but taking longer. |
| memoisation/ˌmeməʊaɪˈzeɪʃn/ | Saving the results of function calls so repeated calls with the same inputs can reuse an earlier result. |
| Stability/stəˈbɪlɪti/ | The property of a sorting algorithm that keeps equal items in their original order. |
| field/fiːld/ | A named component of a record, with its own value and type. |
| stack/stæk/ | A last-in, first-out data structure in which items are added and removed at the same end. |
| depth-first/depθ fɜːst/ | עומק-תחילה |
| push/pʊʃ/ | To add an item to the top of a stack. |
| call stack/kɔːl stæk/ | A stack that records active function calls and their return information. |
| queue/kjuː/ | A data structure in which items are normally removed in the order they were added. |
| breadth-first/bredθ fɜːst/ | רוחב-תחילה |
| enqueue/enˈkjuː/ | To add an item to the rear of a queue. |
| shortest path/ˈʃɔːtɪst pæθ/ | A route between two nodes with the fewest edges in an unweighted graph. |
| unweighted graph | A graph whose edges have no assigned costs or distances. |
| balanced binary search tree/ˈbælənst ˈbaɪnəri sɜːtʃ triː/ | A binary search tree with balanced branches, allowing values to be found quickly. |
| node/nəʊd/ | A point in a graph or tree that can hold data and connect to other points. |
| backtracks | Returns along an earlier path to explore a different route. |
האלגוריתם שישרוד את היקום
- פרוס שירותים חייב לבקר ב-25 ערים ולהחזיר לביתו בדרך הקצרה ביותר. ניסוי כל סדר אפשרי מוביל לכ-$10^{23}$ סידורים. מכונה שבודקת מיליארד סידורים בשנייה תימשך שלושה מיליון שנים.
- הוספת עיר נוספת מכפילה את העבודה ב-25. זו לא מחשב שצריך להיות מהיר יותר; זו גישה שלא יכולה לעולם לעבוד, בכל מהירות, על כל חומרה.
- ידיעה לפני כתיבת התוכנית היא מה שניתוח המורכבות מיועד לה. זה ההבדל בין בחירת אלגוריתם לבין גילוי, חודשים מאוחר יותר, שהשל שלך אינו נשקל.
- שיעור זה הוא על Big-OO בזמן ובמרחב, ואופן ש-ADTs מעצבים את האלגוריתמים הבנויים עליהם.

הכרטיס הזה תוקן, לכן הוא מוצג באנגלית עד שהתרגום שלו יעודכן.
Time complexity
- Time complexity 时间复杂度 describes how the running time 运行时间 grows with the input size 输入规模 $n$. It is written in Big-O notation, which keeps only the dominant term 主导项 and drops constants 常数.
- $O(1)$ constant 常数, the time does not depend on $n$ at all. $O(\log n)$ logarithmic 对数, as in binary search 二分查找. $O(n)$ linear 线性, as in linear search 顺序查找. $O(n \log n)$, the good sorts. $O(n^2)$ quadratic 平方, as in bubble and insertion sort 冒泡排序和插入排序.
- Constants are dropped to compare growth at large input sizes. An $O(n^2)$ algorithm might beat an $O(n \log n)$ one for some input sizes, depending on the constants.

The curves cross once, and after that the order decides everything
How running time grows with n
Slide n upward and compare the curves: O(1) and O(log n) stay almost flat, O(n) rises steadily, O(n²) explodes. This is why Big-O — not a stopwatch — is how we compare algorithms on large inputs.
Which Big-O describes binary search?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Halving the range each step is logarithmic — O(log n).
Which Big-O describes bubble sort in the worst case?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Two nested loops over n elements give O(n²).
Match each algorithm to its time complexity.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Linear search is O(n), binary search O(log n), bubble sort O(n²).
דוגמה פתורה: מה קורה כשמשלמים את הקלט
- אלגוריתם לוקח 4 שניות עבור 1,000 פריטים. העריך את הזמן עבור 2,000 פריטים אם האלגוריתם הוא $O(n)$, ולאחר מכן אם הוא $O(n^2)$.
- $O(n)$: השלמת $n$ משלמת את הזמן, אז כ-8 שניות.
- $O(n^2)$: השלמת $n$ מארבע פי את הזמן, אז כ-16 שניות. ב-10,000 פריטים זה יהיה פי 100 מהמקורי, כ-400 שניות.
- $O(\log n)$ יוסיף רק שלב אחד, ו-$O(1)$ לא ישתנה כלל. חשב לפי הסדר, לא לפי נוסחה.

An O(n squared) algorithm takes 4 seconds on 1,000 items. Roughly how many seconds will it take on 2,000?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Doubling n quadruples an O(n squared) time. The same doubling would take an O(n) algorithm from 4 seconds to 8.
מורכבות זיכרון
- מורכבות זיכרון היא הזיכרון הנוסף שהאלגוריתם צריך, מעבר לקלט עצמו.
- מיון בועות ומיון הכנסה משתמשים ב-$O(1)$ זיכרון נוסף: הם עובדים במקום, וצריכים רק מספר בודד של משתנים. מיון מיזוג משתמש ב-$O(n)$, שכן הוא בונה מערך שני.
- רקורסיה משתמשת בזיכרון סטק proportionale ל-עומק שלה, כי כל קריאה שלא הושלמה שומרת את הפרימה שלה.
- לעיתים קרובות יש מערכת גישוי בין זמן לזיכרון: אחסון תוצאות כדי למנוע חישוב מחדש, כפי שעושה ממוזציה, קונה מהירות על חשבון זיכרון.

מה עוד קובע את הבחירה
- Big-O עוסק בצמיחה, ולא במהירות מוחלטת. עבור $n$ קטן, אלגוריתם פשוט $O(n^2)$ יכול להפגין ביצועים טובים יותר מאלגוריתם מורכב $O(n \log n)$, וכן הוא קל יותר לכתיבה נכונה.
- יציבות חשובה כאשר רשימה מסודרת כבר לפי שדה אחר. פשיטות חשובה כי לאלגוריתם פשוט יש פחות מקומות להסתיר בהם באג.
- התשובה הכנה ל"איזה אלגוריתם" לעיתים קרובות מציין את הסדר וגם את התנאים: זה אחד, כי $n$ גדול והנתונים מגיעים לא מסודרים.
An "in place" sort:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
In-place algorithms (like bubble and insertion sort) sort within the original array, using constant extra space.
Why is bubble sort's space complexity O(1) even though it sorts an array of n items?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
It sorts in place. Merge sort is O(n) because it builds a second array, and recursion costs memory proportional to its depth.
ADTs בתוך אלגוריתמים
- סוגי הנתונים האבסטרקטיים ממאורע 10 הם המנגנון מה שהוא אלגוריתמים בנויים ממנו, ובחירה שלו מעצבת את האלגוריתם.
- ערימה (stack) מאפשרת חיפש בעומק: דוחפים את השכנים, לוקחים את האחרון שהווסף, והחיפש צולל לאורך מסלול אחד לפני שחוזר. רקורסיה משתמשת בערימת הקריאות בדיוק עבור מטרה זו.
- תור (queue) מאפשר חיפש רחב: מדחסים את השכנים לתור, לוקחים את הישן ביותר, והחיפש מתפשט החוצה בטבעות, וזהו המנגנון הממצא את הדרך הקצרה ביותר בגרף שאינו משוקל.
- עץ בינארי שומר על ערכים בסדר כך שחיפוש יפסל מחצית מהצמתים הנותרים בכל שלב, ומניב את $O(\log n)$ לחיפש בינארי על מבנה שיכול גם לגדול.
Which statements about Big-O are correct? Select כולם that apply.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Big-O says nothing about seconds; it is about growth. That is why the crossover with a simpler algorithm exists at small sizes.
דוגמה פותרת: אותו גרף, שני חיפושים
- מבוכה נחקרת מכניסה אחת. השוו בין השימוש בערימה לבין השימוש בתור.
- עם ערימה, המסלול שנמצא לאחרונה הוא זה שנחקר קודם לכן, ולכן החיפש חודר לעומק לאורך מסלול אחד עד למפגש סתימה, ואז חוזר אחורה. הוא משתמש בזיכרון פרופורציונלי לעומק המסלול.
- עם תור, המסלול שנמצא הראשון הוא זה שנחקר קודם לכן, ולכן החיפוש בודק כל מה שמרחק של צעד אחד, ולאחר מכן כל מה שמרחק של שני צעדים. הוא מוצא את הדרך הקצרה תחילה, אך שומר בכל הזיכרון כל המיקום במרחק הנוכחי.
- קראו את שם סוג הנתונים, קראו את הסדר הנגזר מהחקירה, וקראו את ההשלכה.

A stack (LIFO) naturally drives a depth-first traversal, while a queue (FIFO) drives a breadth-first traversal.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
The ADT you choose decides the search order — stack goes deep first, queue explores level by level.
Match each ADT to the search it produces and its consequence.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Most recent first, or oldest first. That single choice decides whether the search goes deep or wide.
סימונים נופלים מהיד
- Big-O מתאר גידול בהתאם לגודל הקלט, לא שניות. "זה מהיר" אינו תשובה להסתכלות על מורכבות.
- הכפלת הקלט כופלת זמן של $O(n)$ וכופלת ארבע פעמים זמן של $O(n^2)$. יש להסיק זאת מהסדר הגדילה.
- מורכבות מקום היא הזיכרון הנוסף, ולכן מיון in-place הוא $O(1)$ גם אם גודל המערך הוא $n$.
- ערימה נותנת חיפש בעומק, תור נותן חיפש רחב. קבלת הזוג הזה בסדר הנכון היא עניין של שלם מספר שאלות.
הבנתך מושלמת
- מורכבות זמן ב-Big-O מתארת גידול בהתאם ל$n$: $O(1)$, $O(\log n)$, $O(n)$, $O(n \log n)$, $O(n^2)$; קבועים מוותרים כי בקנה מידה גדול הסדר הוא הקובע
- הכפלת $n$ כופלת $O(n)$ וכופלת ארבע פעמים $O(n^2)$; נקודת החיתוך עם אלגוריתם "פחות טוב" קיימת רק עבור קלט קטן $n$
- מורכבות מקום היא הזיכרון הנוסף: מיונים in place הם $O(1)$, merge sort הוא $O(n)$, ורקורסיה דורשת עומק ערימה
- ערימה נותנת חיפש בעומק, תור נותן חיפש רחב, ועץ בינארי מחצית את הצמתים הנותרים בכל שלב
מה השתבש? רק צוות האתר רואה זאת.