Sorting · סינון
Putting items in order
- Sorting arranges a list into order, usually smallest first.
- The key move is a swap: exchange two items.
- In Python:
a[i], a[j] = a[j], a[i]swaps two list items in one line.
סידור פריטים
- מיון מסדר רשימה בסדר, בדרך כלל מהקטן לגדול.
- התנועה המרכזית היא החלפה: החלפת שני פריטים זה עם זה.
- בפיתון:
a[i], a[j] = a[j], a[i]מחליף שני פריטים ברשימה בשורה אחת.
Bubble sort
- Compare each pair of neighbours; swap them if they are out of order.
- After one full pass, the largest item has "bubbled" to the end.
- Repeat the passes until no swaps are needed.
מיון בועות
- השוואה בין כל זוג של שכנים; החלפתם אם הם אינם במיקום הנכון.
- לאחר מעבר מלא אחד, הפריט הגדול ביותר "צף" לקצה הרשימה.
- חזרה על המעברים עד שאין צורך בהחלפות נוספות.
data = [3, 1, 2]
n = len(data)
for i in range(n - 1):
for j in range(n - 1 - i):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
print(data)
Insertion sort
- Treat the left part of the list as already sorted.
- Take the next item and slide it left until it sits in the right place.
- Like sorting playing cards in your hand, one at a time.
מיון הכנסה
- טיפול בחלק השמאלי של הרשימה כסודר כבר.
- לקיחת הפריט הבא והזזתו שמאלה עד שהוא יושב במקום הנכון.
- כמו סידור קלפים בידיים, קלף אחד בכל פעם.
data = [3, 1, 2]
for i in range(1, len(data)):
key = data[i]
j = i - 1
while j >= 0 and data[j] > key:
data[j + 1] = data[j]
j = j - 1
data[j + 1] = key
print(data)
Compare them
- Both check roughly
n × npairs, so both are slow on big lists. - Insertion sort is fast when the list is almost sorted already.
- Faster methods exist, but bubble and insertion are easy to understand.
השווה ביניהם
- שתי השיטות בודקות בערך
n × nזוגות, ולכן הן איטיות ברשימות גדולות. - מיון הכנסה מהיר כאשר הרשימה כמעט סודדה כבר.
- קיימות שיטות מהירות יותר, אך מיון בועות ומיון הכנסה קלים להבנה.
In Cambridge pseudocode
- Bubble sort with a
tempvariable for the swap.
בפסאודוקוד קמברידג'
- מיון בועות עם משתנה
tempעבור ההחלפה.
FOR i ← 0 TO LENGTH(list) - 2
FOR j ← 0 TO LENGTH(list) - 2 - i
IF list[j] > list[j + 1] THEN
temp ← list[j]
list[j] ← list[j + 1]
list[j + 1] ← temp
ENDIF
NEXT j
NEXT i
Common mistakes
- Bubble and insertion sort are both O(n²).
- Trace a small list by hand to check your sort works.
טעויות נפוצות
- מיון בועות ומיון הכנסה הם שניהם O(n²).
- לעקוב אחר רשימה קטנה ביד כדי לוודא שהמיון עובד.
Now you try
- Each task changes the list in place — no need to return it.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- כל משימה משנה את הרשימה במקומה — אין צורך להחזירה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Watch a sort run · צפה בביצוע מיון
Sorting repeatedly compares and swaps until everything is in order. · מיון מבצע תמיד השוואות והחלפות חוזרות עד שכל הדברים בסדר.
Write swap(items, i, j) that exchanges the items at index i and index j in the list. Change the list in place (no return). · כתוב פונקציה swap(items, i, j) שמחליפה את הפריטים באינדקס i ובאינדקס j ברשימה. שנה את הרשימה במקום (ללא החזרה).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write bubble_sort(items) that sorts the list into ascending order using bubble sort. Change the list in place. · כתוב פונקציה bubble_sort(items) שממיין את הרשימה לעלייה באמצעות בבל סורט. שנה את הרשימה במקום.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write insertion_sort(items) that sorts the list into ascending order using insertion sort. Change the list in place. · כתוב insertion_sort(items) שמסדר את הרשימה בסדר עולה באמצעות מיון הכנסה. שנה את הרשימה במקום.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.