Bubble sort · בירור בועות
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
Put the smallest time first
- Three race times sit in an array:
14,9,11. The smallest should come first. - Compare each pair of neighbours. When the left one is larger, swap them.
- That single comparison does not finish the list. The next pair is compared after it.
עברית
סדר את הזמן הקטן קודם
- שלושה זמני ריצה נמצאים במערך:
14,9,11. הקטן צריך לבוא ראשון. - השווא כל זוג של שכנים. כאשר השמאלי גדול יותר, החלף אותם.
- ההשוואה הבודדת הזו אינה גומרת את הרשימה. הזוג הבא מושווה לאחריו.
DECLARE Score : ARRAY[1:3] OF INTEGER
DECLARE J, Temp : INTEGER
Score[1] ← 14
Score[2] ← 9
Score[3] ← 11
FOR J ← 1 TO 2
IF Score[J] > Score[J + 1]
THEN
Temp ← Score[J]
Score[J] ← Score[J + 1]
Score[J + 1] ← Temp
ENDIF
NEXT J
OUTPUT Score[1]
OUTPUT Score[2]
OUTPUT Score[3]
English
Repeat the pass
- One pass of the inner loop bubbles the largest value it sees toward the end.
- The outer loop repeats the pass. For four values, three passes are enough.
- This is the syllabus's bubble sort. A linear search, which you wrote earlier, only looks. A sort rearranges.
עברית
חזור על מעבר
- מעבר אחד של לולאת הפנימית בועלת את הערך הגדול ביותר שראה לכיוון הסוף.
- הלולאה החיצונית חוזרת על המעבר. עבור ארבע ערכים, שלושה מעברים מספיקים.
- זוהי מיון בועות מהסילבוס. חיפוש ליניארי, שכתבת לפני כן, רק בוחן. מיון מסדר מחדש.
DECLARE Score : ARRAY[1:4] OF INTEGER
DECLARE I, J, Temp : INTEGER
Score[1] ← 4
Score[2] ← 1
Score[3] ← 3
Score[4] ← 2
FOR I ← 1 TO 3
FOR J ← 1 TO 3
IF Score[J] > Score[J + 1]
THEN
Temp ← Score[J]
Score[J] ← Score[J + 1]
Score[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
OUTPUT Score[1]
OUTPUT Score[2]
OUTPUT Score[3]
OUTPUT Score[4]
English
Watch out
- Swap with a
Tempvariable.Score[J] ← Score[J + 1]alone deletes the left value. - The inner
NEXTnamesJ. The outerNEXTnamesI.
עברית
שימו לב
- החלף עם משתנה
Temp.Score[J] ← Score[J + 1]לבדו מחק את הערך השמאלי. - ה-
NEXTהפנימי קורא ל-J. ה-NEXTהחיצוני קורא ל-I.
English
Common mistakes
- The test is "left is greater", so the smaller value moves left. The other way sorts into reverse order.
- Arrays in this course count from 1. The last pair is the cell just before the end, and the last cell.
עברית
טעויות נפוצות
- הבדיקה היא "שמאלי גדול", ולכן הערך הקטן נע שמאלה. הדרך האחרת ממיינת בסדר הפוך.
- במערכים בקורס זה הספירה מתחילה ב-1. הזוג האחרון הוא התא ממש לפני הסוף והתא האחרון.
English
Now you try
- The first task is one pass. The second is the full sort. Output each value on its own line.
- Press Check answer to test it.
עברית
כעת תנסו בעצמכם
- המשימה הראשונה היא מעבר אחד. השנייה היא המיון המלא. הדפס כל ערך בשורה עצמאית.
- לחץ על בדיקת תשובה כדי לבדוק אותה.
The array Score holds 14, 9, 11. Make one pass that swaps a neighbour when the left value is larger. Output the three values, one per line (9, then 11, then 14). · המערך Score מכיל 14, 9, 11. ביצע מעבר אחד שמחליף שכנים כאשר הערך השמאלי גדול מהימני. הוצא את שלושת הערכים, כל אחד בשורה (9, ולאחריו 11, ולאחריו 14).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
The array Score holds 4, 1, 3, 2. Bubble-sort it into ascending order and output the four values, one per line. · המערך Score מכיל את 4, 1, 3, 2. מיינו את בועות בסדר עולה והדפיסו את ארבע הערכים, אחד בכל שורה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.