EC6 雙語互動學習頁Bilingual interactive page 搜尋與排序Searching and Sorting

搜尋與排序Searching and Sorting

根據 EC6 課本製作的互動溫習網站,涵蓋線性搜尋、二分搜尋、交換、選擇排序、插入排序、冒泡排序、合併排序、快速排序及效率比較。

An interactive revision site based on the EC6 textbook, covering linear search, binary search, swapping, selection sort, insertion sort, bubble sort, merge sort, quick sort, and efficiency comparison.

學習路線圖Learning Map

核心概念Core Concepts

理解搜尋、排序、交換、索引指標與旗標。

Understand searching, sorting, swapping, index pointers, and flags.

演算法筆記Algorithm Notes

以步驟、程式碼和例子掌握六種常見演算法。

Use steps, code, and examples to master six common algorithms.

效率比較Efficiency

比較最佳、平均、最壞情況下所需比較次數。

Compare comparisons in best, average, and worst cases.

互動測驗Interactive Quiz

十二題即時評分選擇題,附詳細解析。

Twelve instantly scored multiple-choice questions with explanations.

3D 閃卡3D Flashcards

二十四張重點詞彙與概念卡,可點擊翻轉。

Twenty-four clickable cards for key terms and ideas.

試題練習Exam Practice

按難度篩選課本式題目,查看參考答案。

Filter textbook-style questions by difficulty and reveal model answers.

核心概念速讀Core Concept Briefing

為甚麼需要高效率演算法Why Efficient Algorithms Matter

列表無處不在。資料量愈大,單靠逐項檢查或反覆交換會消耗更多時間和資源。因此,同一個任務可以有多個演算法,而我們需要按資料狀態和用途選擇合適方法。

Lists appear everywhere. As data grows, checking items one by one or swapping repeatedly costs more time and resources. The same task can be solved by different algorithms, so the best choice depends on the data state and purpose.

搜尋與排序的關係Search and Sort Together

二分搜尋比線性搜尋快,但只適用於已排序列表。若列表經常新增項目,先排序可能增加成本;若列表會被多次搜尋,維持排序通常更有利。

Binary search is faster than linear search, but it only works on sorted lists. If a list changes often, sorting first may add cost; if it is searched many times, keeping it sorted is often worthwhile.

索引、旗標與交換Index, Flag, and Swapping

搜尋程式常用索引追蹤目前位置,用布林旗標記錄是否找到目標。排序程式經常需要交換兩個項目的值,可用暫存變量 temp,或在 Python 中使用 x, y = y, x。

Search programs use indexes to track positions and Boolean flags to record whether a target has been found. Sorting often swaps two items, using a temporary variable temp or Python's x, y = y, x.

排序方向Sorting Direction

升序代表由小至大或字母 A 至 Z;降序代表由大至小或 Z 至 A。修改比較符號通常可以改變排序方向。

Ascending order means smallest to largest or A to Z; descending order means largest to smallest or Z to A. Changing comparison operators usually changes the sorting direction.

演算法重點筆記Algorithm Study Notes

線性搜尋Linear Search

從列表開首逐項比較,直到找到目標或到達最後一項。它可用於未排序列表,設計簡單,但列表愈長,最壞情況愈慢。

Compare items from the beginning until the target is found or the last item is reached. It works on unsorted lists and is simple, but the worst case grows with list length.

79508564978880
target = int(input())
found = False
i = 0
while i < len(mark) and found == False:
    if target == mark[i]:
        found = True
    i = i + 1
print(found)

二分搜尋Binary Search

只適用於已排序列表。每次比較中間項目,然後捨棄不可能包含目標的一半搜尋範圍。

Only works on sorted lists. Each comparison checks the middle item, then discards the half of the search range that cannot contain the target.

50516465757980858897
start = 0
end = len(mark) - 1
found = False
while start <= end and found == False:
    mid = int((start + end) / 2)
    if mark[mid] == target:
        found = True
    elif target < mark[mid]:
        end = mid - 1
    else:
        start = mid + 1

選擇排序Selection Sort

在未排序部分找出最大或最小值,與未排序部分第一項交換,重複直到完成。

Find the maximum or minimum in the unsorted part, swap it with the first unsorted item, and repeat.

for i in range(0, len(mark)):
    max_index = i
    for j in range(i+1, len(mark)):
        if mark[j] > mark[max_index]:
            max_index = j
    mark[i], mark[max_index] = mark[max_index], mark[i]

插入排序Insertion Sort

把列表分成已排序和未排序部分,取出未排序部分第一項作 key value,插入已排序部分的正確位置。

Divide the list into sorted and unsorted parts, take the first unsorted item as the key value, and insert it into the correct sorted position.

for i in range(1, len(mark)):
    key = mark[i]
    j = i - 1
    while j >= 0 and key > mark[j]:
        mark[j+1] = mark[j]
        j = j - 1
    mark[j+1] = key

冒泡排序Bubble Sort

逐對比較相鄰項目,若順序錯誤便交換。一輪完成後,最小或最大項目會移到已排序區。

Compare adjacent items and swap if they are in the wrong order. After each pass, one smallest or largest item moves into the sorted part.

for i in range(0, len(mark)-1):
    for j in range(0, len(mark)-1-i):
        if mark[j] < mark[j+1]:
            mark[j], mark[j+1] = mark[j+1], mark[j]

合併排序與快速排序Merge Sort and Quick Sort

兩者都使用分治和遞迴。合併排序把列表分到每個子列表只剩一項,再合併成有序列表;快速排序選 pivot,把較小和較大項目分到兩邊再遞迴處理。

Both use divide-and-conquer and recursion. Merge sort divides until sub-lists contain one item, then merges sorted lists. Quick sort chooses a pivot, partitions smaller and larger items, then recurses.

1
Divide
把大問題分成子問題。Split a large problem into smaller ones.
2
Conquer
在子列表中排序或分割。Sort or partition sub-lists.
3
Combine
把結果合併成完整有序列表。Combine results into a fully sorted list.

效率與適用情況Efficiency and Suitable Use

演算法Algorithm要求Requirement比較次數重點Comparison Pattern適用情況Best Used When
Linear search
線性搜尋
不需排序No sorting needed最多 \(N\),平均 \(\frac{N+1}{2}\)Maximum \(N\), average \(\frac{N+1}{2}\)項目較少或列表未排序Few items or unsorted list
Binary search
二分搜尋
必須已排序Must be sorted最多約 \(\log_2 N\)Maximum about \(\log_2 N\)列表較長並需多次搜尋Long list with repeated searches
Selection / Insertion / Bubble
選擇 / 插入 / 冒泡
不需預先排序No pre-sort needed最壞情況約 \(\frac{N(N-1)}{2}\)Worst case about \(\frac{N(N-1)}{2}\)學習基本排序概念或小型列表Learning fundamentals or small lists
Merge / Quick
合併 / 快速
使用分治與遞迴Uses divide-and-conquer and recursion最壞情況通常以 \(N\log_2 N\) 理解Often understood as \(N\log_2 N\) in this chapter較長列表和高效率排序Longer lists and efficient sorting

搜尋效率視覺化Search Efficiency Visual

線性搜尋的比較次數隨 \(N\) 近似直線上升;二分搜尋每次把範圍減半,增長較慢。

Linear search comparisons rise roughly linearly with \(N\); binary search halves the range each time and grows much more slowly.

Linear search

Binary search

排序效率視覺化Sorting Efficiency Visual

選擇、插入、冒泡排序在最壞情況下需要大量兩兩比較;合併和快速排序通常更適合大型列表。

Selection, insertion, and bubble sort require many pairwise comparisons in the worst case; merge and quick sort are usually better for large lists.

Basic sorts

Merge / quick sort

互動測驗Interactive Quiz

選擇答案後提交,系統會即時評分並顯示解析。

Choose answers and submit for instant scoring with explanations.

3D 閃卡練習3D Flashcard Practice

點擊卡片翻轉。每張卡同時包含英文與繁體中文,方便背誦術語。

Click a card to flip it. Each card includes English and Traditional Chinese for terminology revision.

課本式試題與參考答案Textbook-Style Questions and Model Answers