Bubble sort · 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.
ไทย
วางเวลาสั้นที่สุดไว้ก่อน
- เวลาแข่งสามเวลาอยู่ใน Array:
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.
ไทย
ทำซ้ำรอบ
- One pass ของลูปภายในจะฟองค่าที่ใหญ่ที่สุด它就 Goes ไปทางท้าย
- ลูปภายนอกทำซ้ำการ pass สำหรับสี่ค่า สามรอบก็พอ
- นี่คือ Bubble sort ของ syllabus Linear search ที่คุณเขียนไว้ก่อนหน้านี้แค่ดู แต่ Sort จัดเรียงใหม่
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.
ไทย
ระวัง
- สลับกับตัวแปร
TempScore[J] ← Score[J + 1]เพียงอย่างเดียวลบค่าทางซ้ายออก NEXTภายในตั้งชื่อJNEXTนอกตั้งชื่อ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.
ไทย
ข้อผิดพลาดที่พบบ่อย
- การทดสอบคือ "ซ้ายมากกว่า" ดังนั้นค่าที่น้อยกว่าจะย้ายไปทางซ้าย ทิศทางตรงกันข้ามจัดเรียงตามลำดับย้อนกลับ
- Arrays ใน Course นี้เริ่มนับที่ 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.
ไทย
ลองดูเลย
- งานแรกคือการ pass หนึ่งครั้ง งานที่สองคือ Sort เต็ม Output แต่ละค่าบนบรรทัดของตัวเอง
- กด Check answer เพื่อทดสอบ
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array Score holds 4, 1, 3, 2. Bubble-sort it into ascending order and output the four values, one per line. · Array Score เก็บ 4, 1, 3, 2. Bubble-sort它为 ascending order และเอาต์พุตสี่ค่า, แต่ละบรรทัด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่