Thuật toán Sắp xếp
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| Sorting/ˈsɔːtɪŋ/ | Sắp xếp |
| selection sort/sɪˈlekʃn sɔːt/ | Sắp xếp theo lựa chọn |
| insertion sort/ɪnˈsɜːʃn sɔːt/ | Sắp xếp chèn |
| in place/ɪn pleɪs/ | Tại chỗ |
Putting things in order
- Sorting 排序 rearranges elements into order (smallest to largest, say).
- The AP course covers two simple sorts: selection sort 选择排序 and insertion sort 插入排序.
- Both use nested loops and swap or shift elements into place.
- Sorting first is what lets you use fast binary search afterwards.
Selection sort
- Find the smallest remaining element and swap it to the front.
- Then find the smallest of what's left, swap it into the next spot, and so on.
- The front of the array grows sorted; the back shrinks unsorted.
- One swap per pass — but it still scans the rest each pass.

An insertion sort, shifting each key into place pass by pass
Insertion sort
- Take the next element and shift it back into its correct place among the already-sorted front.
- Like sorting a hand of cards: slot each new card where it belongs.
- The sorted region grows by one each pass.
- Fast when the data is already nearly sorted.
How much work
- Both sorts use nested loops, so they do about
n²comparisons in the worst case. - That's fine for small arrays but slow for very large ones.
- They sort in place 原地 — no second array needed.
- The exam wants you to trace them, not just name them.
Selection sort SWAPS the smallest remaining element to the front; insertion sort SHIFTS each new element back into the sorted part — don't mix them up. Both are O(n²) nested-loop sorts and both sort in place. Trace them step by step (the AP exam asks for the array's state after each pass), rather than memorising a name.
Selection sort on [3, 1, 2]:
- Pass 1: smallest is
1; swap to front →[1, 3, 2]. - Pass 2: smallest of
[3, 2]is2; swap →[1, 2, 3]. - Sorted — the front grew one element per pass.
Sorting orders elements. Selection sort repeatedly swaps the smallest remaining element to the front; insertion sort shifts each new element back into the sorted part. Both are nested-loop, in-place, O(n²) sorts. Sorting enables fast binary search afterwards.
Lặp qua một lần sắp xếp
Mỗi lượt đặt thêm một phần tử vào đúng vị trí.
Selection sort hoạt động bằng cách...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Selection sort chọn giá trị tối thiểu và đảo vị trí nó lên trước.
Insertion sort hoạt động bằng cách...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Insertion sort chèn mỗi phần tử vào vị trí đã sắp xếp của nó.
Trong trường hợp xấu nhất, cả hai thuật toán thực hiện khoảng...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Vòng lặp lồng nhau cho O(n²).
Cả selection sort và insertion sort đều làm việc tại chỗ (không cần mảng phụ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chúng sắp xếp lại cùng một mảng.
Sắp xếp thứ tự các lượt của selection sort trên [3, 1, 2].
Độ khó 3/5 · 200 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Phần đầu tăng dần đã sắp xếp, một phần tử mỗi lượt.
Sắp xếp trước là hữu ích vì nó cho phép bạn sau này sử dụng...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tìm kiếm nhị phân cần dữ liệu đã sắp xếp.