Bubble sort · בירור בועות
Sorting puts items in order
- Sorting rearranges a list into order — smallest to largest, say.
- Bubble sort is the standard method named in the syllabus.
- It compares neighbours and swaps any that are the wrong way round.
מיון סדר פריטים
- מיון מסדר מחדש רשימה לפי סדר — ממינימום למקסימום, לדוגמה.
- מיון בועות הוא השיטה הסטנדרטית המצוינת במסלול הלימודים.
- הוא משווה שכנים ומחליף כל שניים שהם בסדר הפוך.
Swapping two values
- To sort, you must swap two items.
- Python can swap in one line:
a, b = b, a.
החלפת ערכים זוגיים
- כדי לבצע מיון, יש להחליף שני פריטים.
- Python מאפשרת ביצוע החלפה בשורה אחת:
a, b = b, a.
a = 5
b = 9
a, b = b, a
print(a, b) # 9 5
Bubble sort: compare neighbours
- Walk the list comparing each pair of neighbours.
- If the left one is bigger, swap them — the biggest "bubbles" to the end.
- Repeat the passes until the list is in order.
מיון בועות: השוואת שכנים
- נדד דרך הרשימה תוך השוואת כל זוג של שכנים.
- אם השמאלי גדול יותר, החלף ביניהם — הערך הגדול "בוע" לקצה.
- חזור על מעברים עד שהרשימה מסודרת.
nums = [5, 1, 4, 2]
for i in range(len(nums) - 1):
for j in range(len(nums) - 1 - i):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
print(nums) # [1, 2, 4, 5]
In the exam: bubble sort
- The exam writes bubble sort in pseudocode with nested loops and a swap.
בבחינה: מיון בועות
- בבחינה כותבים את מיון הבועות בפסאו-קוד עם לולאות תלויות והחלפה.
FOR I ← 1 TO N - 1
FOR J ← 1 TO N - 1
IF List[J] > List[J + 1]
THEN
Temp ← List[J]
List[J] ← List[J + 1]
List[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
Watch bubble sort work · צפה בעבודה של מיון בועות
Compare neighbours and swap any out of order; the biggest bubbles to the end. · השווה שכנים והחלף כל זוג שאינו בסדר; הערכים הגדולים "מבועלים" לקצה.
Common mistakes
- Each pass moves the largest remaining value to the end.
- If a whole pass makes no swaps, the list is already sorted.
טעויות נפוצות
- כל מעבר מוביל את הערך הגדול הנותר לקצה.
- אם מעבר שלם אינו מבצע החלפות, הרשימה כבר מסודרת.
Now you try
- Swap two values, then sort a whole list.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- החלף שני ערכים, ולאחר מכן סדר את כל הרשימה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Swap the values of a and b, so that afterwards a is 9 and b is 5. · החלף את הערכים של a ו-b, כך שבסוף a יהיה 9 ו-b יהיה 5.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Sort the list nums into ascending order (smallest first) using a bubble sort. The result should be [1, 2, 4, 5]. · סדר את הרשימה nums לסדר עולה (הקטן ראשון) באמצעות מיון בועות. התוצאה תהיה [1, 2, 4, 5].
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write is_sorted(nums) that returns True if the list is already in ascending order, otherwise False. · כתוב is_sorted(nums) שמחזירה True אם הרשימה כבר מסודרת בסדר עולה, אחרת מחזירה False.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.