Bubble sort · 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.
การจัดเรียงทำให้รายการเป็นระเบียบ
- การจัดเรียง จัดลำดับรายการใหม่ — จากค่าน้อยไปมาก为例
- การเรียงแบบฟองอากาศ (Bubble sort) เป็นวิธีมาตรฐานที่ถูกระบุในหลักสูตร
- มันจะเปรียบเทียบ เพื่อนบ้าน (neighbors) และสลับรายการใดที่มีลำดับผิด
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.
Bubble sort: เปรียบเทียบเพื่อนบ้าน
- เดินผ่านรายการโดยเปรียบเทียบคู่ของเพื่อนบ้านทีละคู่
- ถ้ารายการทางซ้ายมีค่ามากกว่า ให้สลับกัน — ค่าที่ใหญ่ที่สุดจะ "ลอย" ไปยังท้ายรายการ
- ทำซ้ำรอบการเดินจนกว่ารายการจะเป็นระเบียบแล้ว
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.
ในการสอบ: Bubble sort
- การสอบจะเขียน Bubble sort เป็น伪代码พร้อมลูปซ้อนและการสลับ
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.
ลองดูเลย
- สลับสองค่า จากนั้นจัดเรียงรายการทั้งหมด
- กด Check answer เพื่อทดสอบโค้ดของคุณ
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่