Comparing algorithms and ADTs in algorithms
導入済み
15:34
探索 & ソート
百万人の名前が入った電話帳があります。一つずつ確認していくと、百万回の比較が必要になるかもしれません。しかし、すでにそのコツはご存知でしょう:…で開いてみてください。
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| hardware/ˈhɑːdweə/ | The physical parts of a computer system that can be touched. |
| complexity analysis | The study of how an algorithm's time or memory needs grow with input size. |
| Big-O/bɪɡ əʊ/ | A way to describe an upper limit on how an algorithm's resource needs grow. |
| ADTs | Types of data defined by their values and allowed operations, without saying how they are built. |
| Time complexity/taɪm kəmˈpleksɪti/ | How an algorithm's running time grows as its input size increases. |
| running time | The amount of time an algorithm takes to finish its work. |
| input size | The amount of data given to an algorithm to process. |
| dominant term | The part of an expression that grows fastest as input size increases. |
| constant/ˈkɒnstənt/ | A quantity whose value remains fixed within a given problem, expression or program. |
| logarithmic/ˌlɒɡəˈrɪθmɪk/ | Involving logarithms, which describe the exponents needed to obtain numbers from a given base. |
| binary search/ˈbaɪnəri sɜːtʃ/ | A search of ordered data that repeatedly compares with the middle item and discards half of the remaining search range. |
| linear/ˈlɪnɪə/ | Describing growth that is proportional to input size, so doubling the input approximately doubles the amount of work for large inputs. |
| linear search/ˈlɪnɪə sɜːtʃ/ | A search that checks items one by one until it finds the target or reaches the end. |
| quadratic/kwɒˈdrætɪk/ | Describes growth that follows the square of the input size. |
| bubble and insertion sort | Two sorting algorithms whose running times can grow with the square of the input size. |
| Space complexity/speɪs kəmˈpleksɪti/ | The extra memory an algorithm needs, beyond the input itself. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| in place/ɪn pleɪs/ | Describes an algorithm that rearranges its input using only a small amount of extra storage. |
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| Merge sort/mɜːdʒ sɔːt/ | A sorting algorithm that divides data into smaller parts, sorts them, and joins them in order. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| Recursion/rɪˈkɜːʃn/ | The use of a rule inside its own definition to express repetition. |
| stack memory | Memory that holds information about function calls that have not yet finished. |
| frame/freɪm/ | A section of stack memory holding information for one function call. |
| time and memory trade-off | A choice between using more memory to save time and using less memory but taking longer. |
| memoisation/ˌmeməʊaɪˈzeɪʃn/ | Saving the results of function calls so repeated calls with the same inputs can reuse an earlier result. |
| Stability/stəˈbɪlɪti/ | The property of a sorting algorithm that keeps equal items in their original order. |
| field/fiːld/ | A named component of a record, with its own value and type. |
| stack/stæk/ | A last-in, first-out data structure in which items are added and removed at the same end. |
| depth-first/depθ fɜːst/ | 深さ優先探索 |
| push/pʊʃ/ | To add an item to the top of a stack. |
| call stack/kɔːl stæk/ | A stack that records active function calls and their return information. |
| queue/kjuː/ | A data structure in which items are normally removed in the order they were added. |
| breadth-first/bredθ fɜːst/ | 幅優先探索 |
| enqueue/enˈkjuː/ | To add an item to the rear of a queue. |
| shortest path/ˈʃɔːtɪst pæθ/ | A route between two nodes with the fewest edges in an unweighted graph. |
| unweighted graph | A graph whose edges have no assigned costs or distances. |
| balanced binary search tree/ˈbælənst ˈbaɪnəri sɜːtʃ triː/ | A binary search tree with balanced branches, allowing values to be found quickly. |
| node/nəʊd/ | A point in a graph or tree that can hold data and connect to other points. |
| backtracks | Returns along an earlier path to explore a different route. |
宇宙より長生きするアルゴリズム
- A salesman must visit 25 cities and return home by the shortest route. Try every order and there are about $10^{23}$ of them. A machine checking a billion a second would take three million years.
- 都市を1つ増やすと作業量は25倍になります。これはコンピュータを速くすればいい問題ではなく、どのハードウェア・速度においても決して機能しないアプローチです。
- プログラムを書く前にこれを知ることが複雑度解析の目的です。アルゴリズムを選択することと、数ヶ月後に自分のものがスケールしないことに気づくことの差です。
- この授業は時間と空間のBig-OO、およびADTがどのようにアルゴリズムを形作るかです。

このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
Time complexity
- Time complexity 时间复杂度 describes how the running time 运行时间 grows with the input size 输入规模 $n$. It is written in Big-O notation, which keeps only the dominant term 主导项 and drops constants 常数.
- $O(1)$ constant 常数, the time does not depend on $n$ at all. $O(\log n)$ logarithmic 对数, as in binary search 二分查找. $O(n)$ linear 线性, as in linear search 顺序查找. $O(n \log n)$, the good sorts. $O(n^2)$ quadratic 平方, as in bubble and insertion sort 冒泡排序和插入排序.
- Constants are dropped to compare growth at large input sizes. An $O(n^2)$ algorithm might beat an $O(n \log n)$ one for some input sizes, depending on the constants.

The curves cross once, and after that the order decides everything
How running time grows with n
Slide n upward and compare the curves: O(1) and O(log n) stay almost flat, O(n) rises steadily, O(n²) explodes. This is why Big-O — not a stopwatch — is how we compare algorithms on large inputs.
Which Big-O describes binary search?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Halving the range each step is logarithmic — O(log n).
Which Big-O describes bubble sort in the worst case?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Two nested loops over n elements give O(n²).
Match each algorithm to its time complexity.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Linear search is O(n), binary search O(log n), bubble sort O(n²).
演習例: 入力を倍にしたとき
- あるアルゴリズムが1,000個のアイテムで4秒かかります。それが $O(n)$ なら2,000個での時間を推定し、次に $O(n^2)$ の場合も推定せよ。
- $O(n)$: $n$ を倍にすると時間も倍になるため、約8秒。
- $O(n^2)$: doubling $n$ quadruples the time, so about 16 seconds. At 10,000 items it would be 100 times the original, about 400 seconds.
- $O(\log n)$ は単一ステップを追加するだけで、$O(1)$ は一切変化しません。式からではなく順序から推論せよ。

An O(n squared) algorithm takes 4 seconds on 1,000 items. Roughly how many seconds will it take on 2,000?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Doubling n quadruples an O(n squared) time. The same doubling would take an O(n) algorithm from 4 seconds to 8.
空間的複雑度
- 空間的複雑度は、入力自体以外の追加メモリーをアルゴリズムが必要とする量です。
- バブルソートと挿入ソートは $O(1)$ の追加メモリを使用します:インプレイスで動作するため、変数数個で十分です。マージソートは $O(n)$ を使用します。なぜなら第二の配列を構築するためです。
- 再帰呼び出しは未完了な每一次が独自のフレームを保持するため、スタックメモリーは深さに比例します。
- 多くの場合、时间与空间的トレードオフがあります: 計算を避けるために結果を保存する(メモ化など)ことは、スペースでスピードを買うことです。

choiceを決める他の要因
- Big-Oは成長に関するものであり、絶対的な速度ではありません。小さな $n$ に対しては、単純な $O(n^2)$ アルゴリズムが複雑な $O(n \log n)$ より優位になり、正しく記述することも容易です。
- 安定性は、リストが別のフィールドで既に整列している場合に重要です。簡潔さは、単純なアルゴリズムにはバグを隠す場所が少ないため重要です。
- 「どのアルゴリズムか」への誠実な回答は、秩序と条件の両方を名指すことが多いです: これを選ぶのは、$n$ が大きくデータが未整列だからである。
An "in place" sort:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
In-place algorithms (like bubble and insertion sort) sort within the original array, using constant extra space.
Why is bubble sort's space complexity O(1) even though it sorts an array of n items?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It sorts in place. Merge sort is O(n) because it builds a second array, and recursion costs memory proportional to its depth.
アルゴリズム内のADT
- トピック10の抽象データタイプは、アルゴリズムの基盤であり、一つを選ぶことがアルゴリズムを形作ります。
- スタックは深さ優先探索を実現します: 隣接ノードをpushし、最も最近のものをpopすることで、探索は一つの経路を深く進み、行き詰まったらバックトラックします。再帰呼び出しスタックはまさにこれに使われます。
- キューは幅優先探索を実現する:隣接ノードをエンキューし、最も古い要素を取り出し、探索が輪状に拡大することで、無重グラフの最短経路が見つかる。
- バイナリ木は値を順序立てて保持するため、検索は各ステップで残りのノードの半分を除外でき、成長可能な構造に対してバイナリ検索の$O(\log n)$を実現する。
Which statements about Big-O are correct? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Big-O says nothing about seconds; it is about growth. That is why the crossover with a simpler algorithm exists at small sizes.
演習例: 同じグラフ、2つの探索
- 迷路を一つの入口から探検します。スタックを使うこととキューを使うことを比較せよ。
- スタックでは、最後に見つけた経路が次の探索対象となるため、探索は一つのルートで深く進み、行き詰まりに達したらバックトラックします。メモリー使用量は経路の深さに比例します。
- キューでは、最初に見つけた経路が次の探索対象となるため、探索は1ステップ先のすべて、次に2ステップ先のすべてを確認します。最短の経路を最初に発見しますが、現在の距離にあるすべての位置をメモリに保持します。
- ADTの名前、得られる探索順序、そしてその帰結を答えよ。

A stack (LIFO) naturally drives a depth-first traversal, while a queue (FIFO) drives a breadth-first traversal.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The ADT you choose decides the search order — stack goes deep first, queue explores level by level.
Match each ADT to the search it produces and its consequence.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Most recent first, or oldest first. That single choice decides whether the search goes deep or wide.
見落としやすいポイント
- Big-Oは入力サイズに対する成長を記述するものであり、秒数ではありません。「速い」というだけでは複雑度の回答にはなりません。
- Doubling the input doubles an $O(n)$ time and quadruples an $O(n^2)$ one. Reason from the order.
- 空間复杂度は、追加で必要なメモリのことです。そのため、配列のサイズが$n$であっても、インプレイスソートは$O(1)$となります。
- スタックは深さ優先、キューは幅優先を担います。 このペアを逆にしてしまうと、複数の問題で失点する原因になります。
理解できましたか
- Big-Oにおける時間复杂度は、$n$に対する成長度合いを表します:$O(1)$, $O(\log n)$, $O(n)$, $O(n \log n)$, $O(n^2)$;定数項は無視されます。なぜなら、スケールが大きくなるとオーダー(次数)が決定要因になるからです。
- doubling $n$ doubles $O(n)$ and quadruples $O(n^2)$; the crossover with a "worse" algorithm only exists for small $n$
- 空間复杂度は追加のメモリです:インプレイスソートは$O(1)$、マージソートは$O(n)$であり、再帰呼び出しにはスタック深さ分のコストがかかります。
- スタックは深さ優先探索を、キューは幅優先探索を、そして二項木は各ステップで残りのノード数を半分に減らします。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。