Sorting algorithms
แนะนำใหม่
15:34
การค้นหา & การเรียงลำดับ
สมุดโทรศัพท์ที่มีชื่อหนึ่งล้านชื่อ หากคุณตรวจสอบทีละชื่อ อาจต้องเปรียบเทียบถึงหนึ่งล้านครั้ง แต่คุณรู้เทคนิคอยู่แล้ว: เปิดมันใน…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| sort/sɔːt/ | To arrange items in a list into a chosen order. |
| library/ˈlaɪbrəri/ | A collection of reusable routines that programs can call. |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| bubble sort/ˈbʌbl sɔːt/ | การเรียงลำดับแบบฟองอากาศ |
| insertion sort/ɪnˈsɜːʃn sɔːt/ | การเรียงลำดับด้วยการแทรก |
| list/lɪst/ | A collection of items arranged in a particular order. |
| trace/treɪs/ | To follow an algorithm step by step, recording how its data changes. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| pass/pæs/ | One complete run through a part of the list during sorting. |
| swap/swɒp/ | To exchange two values so that each takes the other's place. |
| elements/ˈelɪmənts/ | Individual items in a collection or data structure. |
| inner loop/ˈɪnə luːp/ | A loop inside another loop that repeats steps during each pass. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| sorted section | The part of a list whose items are already in order. |
| key/kiː/ | The item held aside to be placed into the sorted section. |
| best case | The input that allows an algorithm to complete with the least work. |
| extra memory | Memory used by an algorithm in addition to its input data. |
| in place/ɪn pleɪs/ | ในสถานที่ |
| stable/ˈsteɪbl/ | เสถียรภาพ |
| field/fiːld/ | A named component of a record, with its own value and type. |
| records | Groups of related data stored together as a single item. |
| linear time | Running time that grows in proportion to the number of items. |
การเรียงลำดับที่ช้าอย่างตั้งใจ
- คลังข้อมูลระดับมืออาชีพทั้งหมดใช้อัลกอริทึมในการเรียงลำดับที่ไม่ได้สอบให้เขียน Bubble sort และ Insertion sort มีค่า $O(n^2)$ ทั้งคู่ และถูกจัดอันดับต่ำกว่าการเรียงลำดับที่ดีในรายการขนาดใหญ่
- พวกเขาอยู่ในหลักสูตร anyway และเพื่อเหตุผลที่ดี: มีความยาวพอที่จะทำ trace ด้วยมือ และการทำ trace หนึ่งครั้งคือวิธีที่คุณเรียนรู้ว่าการเรียงลำดับ ทำอะไร กับอาเรย์จริงๆ
- ยังมีกรณีการใช้งานจริงสำหรับ insertion sort ในรายการขนาดเล็กหรือที่เรียงลำดับมาเกือบเต็มแล้ว มันเร็วที่สุดในบรรดาทั้งหมด และคลังข้อมูลจริงจะสลับไปใช้สำหรับกรณีเหล่านี้โดยเฉพาะ
- บทเรียนนี้คือ bubble sort และ insertion sort: อัลกอริทึม, พฤติกรรมของแต่ละตัว และจุดที่แต่ละตัวชนะ
การเรียงลำดับแบบฟองอากาศ (Bubble sort)
FOR pass ← 1 TO n - 1
swapped ← FALSE
FOR i ← 1 TO n - pass
IF A[i] > A[i + 1] THEN
swap A[i], A[i + 1]
swapped ← TRUE
ENDIF
NEXT i
IF NOT swapped THEN // already sorted
EXIT FOR
ENDIF
NEXT pass
- แต่ละรอบจะเปรียบเทียบ คู่ที่อยู่ติดกัน และสลับหากไม่ถูกต้อง ทำให้ค่าที่เหลือมากที่สุด "ลอย" ไปยังท้ายรายการ
- หลังผ่าน $k$ Last $k$ elements are final, which is why the inner loop stops at $n - \text{pass}$.
- ตัวบ่งชี้
swappedช่วยให้หยุดก่อนกำหนดได้: หากรอบใดหนึ่งไม่มีการสลับตำแหน่งเลย แสดงว่ารายการเรียงลำดับแล้ว
Insertion sort
FOR i ← 2 TO n
key ← A[i]
j ← i - 1
WHILE j >= 1 AND A[j] > key DO
A[j + 1] ← A[j] // shift right
j ← j - 1
ENDWHILE
A[j + 1] ← key // drop it in
NEXT i
- It builds a sorted section at the front, growing by one each time. Each new element is held aside as
key, larger elements are shifted right to open a gap, and the key is dropped in. - นี่คือวิธีที่คนส่วนใหญ่เรียงไพ่ในมือ ซึ่งเป็นการเปรียบเทียบที่ข้อสอบคาดหวัง

*ฝั่งซ้ายเรียงแล้ว ฝั่งขวา untouched และขอบเขตเคลื่อนที่ไปทางขวา

อัลกอริทึมการจัดอันดับ
compare adjacent, swap if needed
Step through a bubble sort: each pass floats the largest value to the end.
Bubble sort works by:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each pass swaps adjacent pairs, "bubbling" the largest element to the end.
The average/worst-case time complexity of bubble sort is:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Two nested loops over n elements give O(n²); the best case (already sorted) is O(n) with the early exit.
ตัวอย่างวิธีทำ: ทำตามกระบวนการหนึ่งรอบ
- ทำ trace รอบแรกของการเรียงแบบ bubble บน 5, 3, 8, 1
- เปรียบเทียบ 5 และ 3: ไม่ถูกต้อง สลับ ได้ 3, 5, 8, 1 เปรียบเทียบ 5 และ 8: ถูกต้อง ไม่สลับ เปรียบเทียบ 8 และ 1: สลับ ได้ 3, 5, 1, 8
- หลังหนึ่งรอบ ค่าที่ใหญ่ที่สุด 8 จะอยู่ในตำแหน่งสุดท้าย และสามารถข้ามการเปรียบเทียบหนึ่งครั้งต่อรอบตั้งแต่นี้ต่อไป
- ตอนนี้ทำ trace ขั้นตอนที่สามของ insertion sort บน 3, 5, 8, 1 คีย์คือ 1 เลื่อน 8, 5 และ 3 ไปทางขวาทีละตำแหน่ง แล้ววาง 1 ไว้ด้านหน้า: 1, 3, 5, 8 แสดงอาเรย์หลังจากแต่ละขั้นตอน นั่นคือที่คะแนนจะอยู่ที่นั่น

Insertion sort builds the sorted result by:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It grows a sorted prefix on the left, shifting larger elements right to drop each key into place.
How does insertion sort place each new element?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Holding the key aside and shifting is what distinguishes it from bubble sort's repeated adjacent swaps.
ประสิทธิภาพ
| bubble | insertion | |
|---|---|---|
| กรณีที่ดีที่สุด | $O(n)$, หนึ่งรอบโดยไม่มีการสลับ | $O(n)$, เรียงแล้ว ไม่มีการเลื่อน |
| average and worst | $O(n^2)$ | $O(n^2)$ |
| หน่วยความจำเสริม | $O(1)$, in place | $O(1)$, in place |
| มั่นคง | ใช่ | ใช่, stable |
- In place หมายความว่าต้องใช้หน่วยความจำเสริมเพียงจำนวนคงที่ เรียงลำดับภายในอาเรย์เอง Stable หมายความว่าค่าที่เท่ากันสองค่าจะรักษาลำดับสัมพัทธ์เดิมไว้ ซึ่งสำคัญเมื่อรายการถูกเรียงลำดับด้วยฟิลด์อื่นมาก่อน
- ทั้งสองชนิดทำได้ $O(n)$ บนข้อมูลที่เรียงลำดับอยู่แล้ว แต่เฉพาะบับเบิลโซrtsที่มีตัวบ่งชี้
swappedเท่านั้น ถ้าไม่มีตัวบ่งชี้มันจะดำเนินการทุก รอบ เสมอ

After the first pass of a bubble sort on 5, 3, 8, 1, what is the array? Write the four numbers separated by commas.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
5 and 3 swap, 5 and 8 do not, 8 and 1 swap. The largest value has reached the end, so the next pass can be one comparison shorter.
ตัวอย่างวิธีทำ: การเรียงลำดับใด และทำไม
- รายการ 200,000 บันทึกต้องเรียงลำดับใหม่จากศูนย์ ไม่มีทั้งสอง: ทั้งคู่มีค่า $O(n^2)$ ดังนั้นต้องใช้ merge หรือ quick sort ที่ $O(n \log n)$ บอกให้ชัดเจนแทนการเลือกสิ่งที่ไม่แย่ที่สุด
- รายการเรียงลำดับ 10,000 เพิ่มบันทึกใหม่ 5 บันทึกที่ท้ายรายการและต้องเรียงลำดับอีกครั้ง การเรียงลำดับแบบแทรก: ข้อมูลเกือบเรียงลำดับแล้ว ดังนั้นคีย์ใหม่แต่ละตัวจะเลื่อนไประยะสั้น และเข้าใกล้ $O(n)$
- ตัวอย่างการสอนที่ต้องทำ trace ด้วยมือบนกระดาษ Bubble sort: ง่ายต่อการติดตามที่สุด นี่คือการใช้งานที่เหลืออยู่จริง
- อธิบายเหตุผลจาก สถานะของข้อมูล และ ขนาด ไม่ใช่จากความชอบทั่วไป

Match each sorting idea to what it means.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Bubble swaps neighbours, insertion grows a sorted prefix; both are O(n²) worst-case; stability is about equal-key order.
Insertion sort runs close to O(n) on small or nearly-sorted arrays, because few elements need to be shifted.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
On nearly-sorted data each new item is already almost in place — which is why insertion sort beats fancier sorts on small inputs.
Which are true of both bubble sort and insertion sort? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
On a large unsorted list an O(n log n) sort wins decisively. Saying so is the right answer, not choosing the least bad of the two.
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Why one pass is not the whole story
- Both sorts do repeated passes, and the exam distinguishes them by what one pass achieves and by when they stop.
- A bubble sort pass compares adjacent pairs and swaps them, so one pass carries the largest remaining item to its final place. The whole sort is $n - 1$ passes.
- An insertion sort 插入排序 pass takes the next item and moves it back into the already-sorted part, so after $k$ passes the first $k$ items are sorted among themselves but not yet in final position.
- Bubble sort can be improved with a flag: if a pass makes no swaps, the list is already sorted and the algorithm stops. This can reduce the number of passes on nearly-sorted data.
- Without the flag, both are $n^2$ in the worst case, which is why either is a poor choice for a large file and why the exam asks about small ones.
A sorted list of 10,000 records gains 5 new records at the end. Which sort suits re-sorting it?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Nearly sorted data is exactly insertion sort's best case, approaching O(n). Real libraries switch to it for this reason.
Match each sort to what one pass achieves.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
That difference is what a trace question is really testing. A bubble-sort flag also lets it stop early on nearly-sorted data, which insertion sort handles well anyway.
คะแนนที่หลุดหายไป
- Bubble sort เปรียบเทียบคู่ adjacent คำตอบที่เปรียบเทียบองค์ประกอบหนึ่งกับอีกทั้งหมดกำลังอธิบายอัลกอริทึมที่แตกต่างกัน
- ลูปภายในจะสั้นลงทุกครั้งที่วนซ้ำ เพราะส่วนท้ายของอาร์เรย์ถูกกำหนดค่าไว้แล้ว บอกเหตุผล
- Sort แบบแทรก (insertion sort) เลื่อนองค์ประกอบไปทางขวาเพื่อเปิดช่องว่าง; ไม่ได้สลับตำแหน่งซ้ำๆ ความแตกต่างนี้คือหัวใจสำคัญของอัลกอริทึม
- ทั้งสองมี $O(n^2)$ โดยเฉลี่ยและในกรณีแย่ที่สุด, และ $O(n)$ ในกรณีดีที่สุด ให้ระบุกรณีพร้อมลำดับความซับซ้อน
คุณเข้าใจแล้ว
- bubble sort: การวนซ้ำหลายรอบเปรียบเทียบคู่ที่ ติดกัน และสลับตำแหน่ง, ตัวใหญ่ที่สุดลอยขึ้นสู่ส่วนท้าย, ลูปภายในสั้นลงทุกครั้งที่วนซ้ำ, พร้อม
swappedเพื่อออกก่อนกำหนดเมื่อไม่มีการสลับ - insertion sort: ขยายส่วนที่เรียงลำดับแล้วด้านหน้า, เก็บคีย์ปัจจุบันไว้ข้างหนึ่ง, เลื่อนองค์ประกอบที่ใหญ่กว่าไปทางขวา แล้ววางคีย์ลงในช่องว่าง
- ทั้งสองมี $O(n^2)$ โดยเฉลี่ยและกรณีแย่ที่สุด, $O(n)$ กรณีดีสุด, ทำงานในสถานที่ (in place) และ คงที่ (stable)
- insertion sort ชนะจริงในรายการที่มี ขนาดเล็กหรือเรียงลำดับเกือบสมบูรณ์; สำหรับรายการขนาดใหญ่ที่ไม่ได้เรียงลำดับ Neither เป็นคำตอบที่เหมาะสม
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้