Sorting algorithms
הוצג
15:34
חיפוש ומיון
ספר טלפונים עם מיליון שמות. אם תבדוק אותם אחד אחד, ייתכן שתבצע מיליון השוואות. אבל כבר ידוע לך הטריק: פתח אותו בחצי…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| sort/sɔːt/ | To arrange items in a list into a chosen order. |
| library/ˈlaɪbrəri/ | A collection of reusable routines that programs can call. |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| bubble sort/ˈbʌbl sɔːt/ | מיון בועה |
| insertion sort/ɪnˈsɜːʃn sɔːt/ | מיון הכנסה |
| list/lɪst/ | A collection of items arranged in a particular order. |
| trace/treɪs/ | To follow an algorithm step by step, recording how its data changes. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| pass/pæs/ | One complete run through a part of the list during sorting. |
| swap/swɒp/ | To exchange two values so that each takes the other's place. |
| elements/ˈelɪmənts/ | Individual items in a collection or data structure. |
| inner loop/ˈɪnə luːp/ | A loop inside another loop that repeats steps during each pass. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| sorted section | The part of a list whose items are already in order. |
| key/kiː/ | The item held aside to be placed into the sorted section. |
| best case | The input that allows an algorithm to complete with the least work. |
| extra memory | Memory used by an algorithm in addition to its input data. |
| in place/ɪn pleɪs/ | במקום |
| stable/ˈsteɪbl/ | יציב |
| field/fiːld/ | A named component of a record, with its own value and type. |
| records | Groups of related data stored together as a single item. |
| linear time | Running time that grows in proportion to the number of items. |
הסידור שהאיטיות בו מכוונת
- לכל ספרייה רצינית יש אלגוריתם סידור שאין בתיק נסיעה מבקשים ממך לכתוב. סידור בועות וסידור הכנסה הם שניהם $O(n^2)$, ושניהם מובסים על ידי כל סידור סביר ברשימה גדולה.
- הם מופיעים בתיק anyway, ולטובת סיבה טובה: הם קצרים מספיק כדי לעקוב אחריהם ידנית, ועקוב אחרי אחד מהם הוא הדרך ללמוד מה סידור בעצם עושה למערך.
- יש גם טענה אמיתית לסידור הכנסה. ברשימה קטנה או כזו שכבר מסודרת כמעט לחלוטין, זו באמת התוכנית המהירה ביותר שקיימת, וספריות אמיתיות מעבירות אליה בדיוק עבור המקרים האלה.
- שיעור זה עוסק בסידור בועות ובסידור הכנסה: האלגוריתמים, התנהגותם, ואיפה כל אחד מהם מנצח.
בועות (Bubble sort)
FOR pass ← 1 TO n - 1
swapped ← FALSE
FOR i ← 1 TO n - pass
IF A[i] > A[i + 1] THEN
swap A[i], A[i + 1]
swapped ← TRUE
ENDIF
NEXT i
IF NOT swapped THEN // already sorted
EXIT FOR
ENDIF
NEXT pass
- בכל מעבר משווים זוגות סמוכים ומחליפים כל זוג שיצא מהסדר, כך שהערך הגדול שנותר "מבועבל" לקצה.
- לאחר מעבר $k$ ה$k$ אלמנטים האחרונים הם סופיים, ולכן הלולאה הפנימית עצומה ב$n - \text{pass}$.
- הדגל
swappedמאפשר עצירה מוקדמת: אם במעבר שלם לא נעשתה החלפה, הרשימה מסודרת.
סידור הכנסה
FOR i ← 2 TO n
key ← A[i]
j ← i - 1
WHILE j >= 1 AND A[j] > key DO
A[j + 1] ← A[j] // shift right
j ← j - 1
ENDWHILE
A[j + 1] ← key // drop it in
NEXT i
- הוא בונה חלק מסודר בהתחלה, הגדל באלמנט אחד בכל פעם. כל אלמנט חדש מוחזץ בצד כ
key, אלמנטים גדולים יותר מוזזים ימינה לפתיחת מרווח, והמפתח מוטבע בתוך המרווח. - זוהי הדרך שבה רוב האנשים מסדרים משחקת קלפים, והאנלוגיה שהמבחן מצפה לה.

החלק השמאלי מסודר, החלק הימני אינו נוגע בו, הגבול זז ימינה


אלגוריתמי סינון
compare adjacent, swap if needed
Step through a bubble sort: each pass floats the largest value to the end.
Bubble sort works by:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Each pass swaps adjacent pairs, "bubbling" the largest element to the end.
The average/worst-case time complexity of bubble sort is:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Two nested loops over n elements give O(n²); the best case (already sorted) is O(n) with the early exit.
דוגמה פתורה: עקוב מעבר אחד
- עקוב את המעבר הראשון בסידור בועות על 5, 3, 8, 1.
- השווה בין 5 ל-3: לא בסדר, החלף, מקבלים 3, 5, 8, 1. השווה בין 5 ל-8: בסדר, אין החלפה. השווה בין 8 ל-1: החלף, מקבלים 3, 5, 1, 8.
- לאחר מעבר אחד הערך הגדול ביותר, 8, נמצא במיקומו הסופי, ומעכשיו ניתן לדלג על השוואה אחת בכל מעבר.
- עקוב כעת את השלב השלישי של סידור ההכנסה על 3, 5, 8, 1. המפתח הוא 1. הזז את 8, 5 ו-3 כל אחד במופע אחד ימינה, ולאחר מכן הנח את 1 בראש: 1, 3, 5, 8. הצג את המערך לאחר כל שלב; זהו המקום שבו המטרה היא לקבל נקודות.

Insertion sort builds the sorted result by:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
It grows a sorted prefix on the left, shifting larger elements right to drop each key into place.
How does insertion sort place each new element?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Holding the key aside and shifting is what distinguishes it from bubble sort's repeated adjacent swaps.
ביצועים
| סידור בועות | סידור הכנסה | |
|---|---|---|
| מקרה מיטבי | $O(n)$, מעבר אחד ללא החלפות | $O(n)$, כבר מסודר, ללא הזזות |
| ממוצע וגרוע | $O(n^2)$ | $O(n^2)$ |
| זיכרון נוסף | $O(1)$, בתוך המערך | $O(1)$, בתוך המערך |
| יציבות | כן | כן, יציב |
- בתוך המערך (in place) אומר שהוא דורש רק כמות קבועה של זיכרון נוסף, ומסדר בתוך המערך עצמו. יציב אומר ששתי ערכים שווים שומרים על הסדר היחסי המקורי שלהם, מה שחשוב כאשר רשימה already מסודרת לפי שדה אחר.
- שניהם מגיעים ל$O(n)$ על נתונים שכבר מסודרים, אך רק אם לסידור בועות יש את הדגל
swapped. ללאו, הוא מבצע תמיד את כל המעברים.

After the first pass of a bubble sort on 5, 3, 8, 1, what is the array? Write the four numbers separated by commas.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
5 and 3 swap, 5 and 8 do not, 8 and 1 swap. The largest value has reached the end, so the next pass can be one comparison shorter.
דוגמה פתורה: איזה סידור, ולמה
- רשימה של 200,000 רשומות חייבת להיות מסודרת מאפס. שניהם לא: שניהם הם $O(n^2)$, לכן נדרש סידור מיזוג או מהיר ב$O(n \log n)$. אמור זאת במקום לבחור את הרע מביניהם.
- רשימה מסודרת של 10,000 מקבלת 5 רשומות חדשות בסוף וחייבת להיות מסודרת שוב. סידור הכנסה: הנתונים כמעט מסודרים, כך שכל מפתח חדש יוזז רק למרחק קצר והוא מתקרב ל$O(n)$.
- דוגמה לימודית חייבת להיות מעוקבת ידנית על נייר. סידור בועות: הוא הפשוט ביותר לעקוב אחריו, והוא השימוש העיקרי שנותר לו.
- נמקו על בסיס מצב הנתונים וגודלם, ולא לפי העדפה כללית.

Match each sorting idea to what it means.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Bubble swaps neighbours, insertion grows a sorted prefix; both are O(n²) worst-case; stability is about equal-key order.
Insertion sort runs close to O(n) on small or nearly-sorted arrays, because few elements need to be shifted.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
On nearly-sorted data each new item is already almost in place — which is why insertion sort beats fancier sorts on small inputs.
Which are true of both bubble sort and insertion sort? Select כולם that apply.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
On a large unsorted list an O(n log n) sort wins decisively. Saying so is the right answer, not choosing the least bad of the two.
הכרטיס הזה תוקן, לכן הוא מוצג באנגלית עד שהתרגום שלו יעודכן.
Why one pass is not the whole story
- Both sorts do repeated passes, and the exam distinguishes them by what one pass achieves and by when they stop.
- A bubble sort pass compares adjacent pairs and swaps them, so one pass carries the largest remaining item to its final place. The whole sort is $n - 1$ passes.
- An insertion sort 插入排序 pass takes the next item and moves it back into the already-sorted part, so after $k$ passes the first $k$ items are sorted among themselves but not yet in final position.
- Bubble sort can be improved with a flag: if a pass makes no swaps, the list is already sorted and the algorithm stops. This can reduce the number of passes on nearly-sorted data.
- Without the flag, both are $n^2$ in the worst case, which is why either is a poor choice for a large file and why the exam asks about small ones.
A sorted list of 10,000 records gains 5 new records at the end. Which sort suits re-sorting it?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Nearly sorted data is exactly insertion sort's best case, approaching O(n). Real libraries switch to it for this reason.
Match each sort to what one pass achieves.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
That difference is what a trace question is really testing. A bubble-sort flag also lets it stop early on nearly-sorted data, which insertion sort handles well anyway.
סימונים נופלים מהיד
- מינון בועות משווה זוגות שכנים. תשובה המשווה אלמנט לכל שאר האלמנטים מתארת אלגוריתם אחר.
- הלולאה הפנימית מתקצרת בכל מעבר, כי סוף המערך כבר סופי. הסבירו למה.
- מינון הכנסה מזיז אלמנטים ימינה כדי לפתוח מקום; הוא אינו מחליף שוב ושוב. ההבדל הזה הוא ליבת האלגוריתם.
- שתי השיטות הן $O(n^2)$ בערך הממוצע ובמקרה הרע, והן $O(n)$ במקרה הטוב. ציינו את המקרה עם הסדר.
הבנתך מושלמת
- מינון בועות: מעברים חוזרים המשווים זוגות שכנים ומחליפים אותם, הגדול מועף לסוף, הלולאה הפנימית מתקצרת בכל מעבר, עם סימן
swappedליציאה מוקדמת - מינון הכנסה: צומח חלק ממוין בראש, מחזיק כל אלמנט צירוף בצד, מזיז אלמנטים גדולים יותר ימינה ומכניס את האלמנט לצירוף למקום הפתוח
- שתי השיטות הן $O(n^2)$ בערך הממוצע ובמקרה הרע, $O(n)$ במקרה הטוב, במקום ויציבותיות
- מינון הכנסה מנצח באמת ברשימות קטנות או קרובות למיון; עבור רשימה גדולה ולא ממוינת, אף אחת מהן היא התשובה הנכונה
מה השתבש? רק צוות האתר רואה זאת.