Linear search and 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
Find one mark, then order the list
- Syllabus topic 10.2 asks for two algorithms on an array: a linear search and a bubble sort.
- A linear search looks at every cell. Remember the index when the value matches. If it never matches, the index stays 0.
- Searching does not move the values. Sorting does.
ไทย
หาตำแหน่งหนึ่งจุด แล้วเรียงลำดับรายการ
- หัวข้อ 10.2 ของหลักสูตรต้องการอัลกอริทึมสองแบบสำหรับ array: linear search และ bubble sort
- Linear search ตรวจสอบทุกเซลล์ จำ index ไว้เมื่อค่าตรงกัน ถ้าไม่ตรงกับใครเลย index จะยังคงเป็น 0
- การค้นหาไม่ย้ายค่า แต่การจัดอันดับจะย้ายค่า
DECLARE A : ARRAY[1:4] OF INTEGER
DECLARE I, Pos : INTEGER
A[1] ← 4
A[2] ← 9
A[3] ← 1
A[4] ← 7
Pos ← 0
FOR I ← 1 TO 4
IF A[I] = 9 THEN
Pos ← I
ENDIF
NEXT I
OUTPUT Pos
English
Bubble the largest to the end
- Compare each pair of neighbours. When the left one is larger, swap them through a temporary variable.
- One comparison is not the whole sort. Repeat the pass until every pair has been handled.
- For four values, an outer loop of three passes and an inner loop across the pairs is enough.
ไทย
ดันค่าที่ใหญ่ที่สุดไปท้ายรายการ
- เปรียบเทียบคู่เพื่อนบ้านทีละคู่ เมื่อตัวทางซ้ายมากกว่า ให้สลับกันโดยใช้ตัวแปรชั่วคราว
- การเปรียบเทียบครั้งเดียวไม่ใช่การเรียงทั้งหมด ทำซ้ำรอบการผ่านจนกว่าคู่ทุกคู่จะถูกจัดการ
- สำหรับสี่ค่า ลูปภายนอกทำสามรอบ และลูปภายในวนผ่านคู่คือเพียงพอแล้ว
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
Common mistakes
- A swap needs three assignments. Copying the right cell over the left one loses the left value.
THENstays on theIFline in this guide. The innerNEXTnamesJ, the outer namesI.
ไทย
ข้อผิดพลาดที่พบบ่อย
- การสลับต้องการสามคำสั่ง copy การ-copy เซลล์ขวาทับเซลล์ซ้ายจะทำให้สูญเสียค่าทางซ้าย
THENยังคงอยู่บนบรรทัดIFในคู่มือนี้ lopo ในNEXTตั้งชื่อJ, lopo นอกตั้งชื่อI
English
Now you try
- First report where
9sits. Then sort four values into ascending order, one per line.
ไทย
ลองดูเลย
- รายงานตำแหน่งที่
9อยู่ก่อน จากนั้นเรียงสี่ค่าจากน้อยไปมาก โดยแต่ละบรรทัดมีหนึ่งค่า
The array A holds 4, 9, 1, 7. Set Pos to the index of 9 (it is 2) and output Pos. If you never find it, Pos stays 0. · Array A เก็บ 4, 9, 1, 7. ตั้ง Pos เป็นดัชนีของ 9 (มันคือ 2) และเอาต์พุต Pos. หากคุณไม่เคยพบมัน, Pos ยังคง 0
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 เพื่อดูผลลัพธ์ที่นี่