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. |
| Big-O notation/bɪɡ əʊ nəʊˈteɪʃn/ | Notation describing an asymptotic upper bound on a function's growth, such as an algorithm's time or space use as input size increases. |
| abstract data types |
우주가 소멸해도 살아남을 알고리즘
- 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.
- 도시 하나를 추가하면 작업량이 25배 증가합니다. 이는 더 빠른 컴퓨터가 필요한 것이 아니라, 어떤 하드웨어에서도 어떤 속도에서도 절대 작동할 수 없는 접근 방식입니다.
- Knowing that before you write the program is what complexity analysis is for. It is the difference between choosing an algorithm and discovering, months later, that yours does not scale.
- 이 수업은 시간 및 공간에 대한 Big-OO이며, ADT가 그에 기반한 알고리즘을 어떻게 형성하는지입니다.

시간 복잡도
- 시간 복잡도는 입력 크기 $n$에 따라 실행 시간이 어떻게 증가하는지를 설명합니다. Big-O 표기법으로 표현되며, 지배적인 항만 남기고 상수를 버립니다.
- $O(1)$ 상수 시간, 시간이 $n$에 전혀 의존하지 않습니다. $O(\log n)$ 로그 시간, 이진 탐색과 같습니다. $O(n)$ 선형 시간, 선형 탐색과 같습니다. $O(n \log n)$, 좋은 정렬 알고리즘입니다. $O(n^2)$ 제곱 시간, 버블 정렬 및 삽입 정렬과 같습니다.
- The reason constants are dropped: they are swamped. An $O(n^2)$ algorithm might beat an $O(n \log n)$ one for $n = 10$, but at $n = 10{,}000$ nothing about the constants can save it.

곡선은 한 번 교차한 후, 이후로는 순서가 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²).
worked example: 입력을 두 배로 늘리는 것
- 한 알고리즘이 1,000개 항목에 대해 4초가 걸립니다. 이것이 $O(n)$일 때, 그리고 $O(n^2)$일 때 2,000개 항목에서의 시간을 추정하십시오.
- $O(n)$: doubling $n$ doubles the time, so about 8 seconds.
- $O(n^2)$: $n$를 두 배로 늘리면 시간이 네 배가 되므로 약 16초입니다. 10,000개 항목에서는 원래의 100배, 약 400초가 될 것입니다.
- **$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)$의 추가 메모리를 사용합니다: They work in place하여 몇 개의 변수만 필요합니다. 병합 정렬은 $O(n)$를 사용하며, 두 번째 배열을 만듭니다.
- Recursion uses stack memory proportional to its depth, because every unfinished call keeps its own frame.
- 종종 시간 및 메모리 트레이드오프가 있습니다: 재계산을 피하기 위해 결과를 저장하는 것은 메모라이제이션처럼 속도를 공간으로 구매합니다.

choice를 결정하는 다른 요소
- Big-O는 성장에 관한 것이며 절대적인 속도가 아닙니다. 작은 $n$에 대해, 단순한 $O(n^2)$ 알고리즘이 복잡한 $O(n \log n)$ 알고리즘보다 이길 수 있으며, 올바르게 작성하기 더 쉽습니다.
- 안정성은 다른 필드에 의해 이미 정렬된 목록이 있을 때 중요합니다. 간결함은 단순한 알고리즘이 버그를 숨기기 쉬운 곳이 적기 때문에 중요합니다.
- The honest answer to "which algorithm" often names the order and the conditions: this one, because $n$ is large and the data arrives unsorted.
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.
알고리즘 내의 ADTs
- 주제 10의 추상 데이터 유형은 알고리즘이 구축되는 기계장치이며, 하나를 선택하는 것이 알고리즘을 형성합니다.
- 스택은 딥 퍼스트 탐색을 제공합니다: 이웃을 푸시하고, 가장 최근에 들어온 것을 취하면, 탐색은 한 경로를 따라 깊게 진입한 후 백트랙합니다. 재귀는 바로 이 목적으로 콜 스택을 사용합니다.
- 큐는 브레드스 퍼스트 탐색을 제공합니다: 이웃을 인Queue하고, 가장 오래된 것을 취하면, 탐색은 원반 형태로 바깥쪽으로 퍼져 나가며, 이는 가중치가 없는 그래프에서 최단 경로를 찾는 것입니다.
- 이진 트리는 값을 순서대로 유지하여 탐색 시 각 단계마다 남은 노드의 절반을 제거하므로, 또한 성장할 수 있는 구조에 대해 이진 검색의 $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.
worked example: 같은 그래프, 두 가지 탐색
- 미로의 한 입구에서 Exploration합니다. 택을 사용하는 것과 큐를 사용하는 것을 대비시키십시오.
- 스택을 사용하면, 가장 최근에 발견된 경로가 다음에 Exploration되므로, 탐색은 한 경로로 깊게 내려가서 데드엔드에 도달하면 백트랙합니다. 경로 깊이에 비례하는 메모리를 사용합니다.
- 큐를 사용하면, 가장 오래된 발견된 경로가 다음에 Exploration되므로, 탐색은 한 단계 떨어진 모든 것을 검토한 후 두 단계 떨어진 모든 것을 검토합니다. 최단 경로를 먼저 찾지만, 현재 거리상의 모든 위치를 메모리에 유지합니다.
- ADT의 이름을 밝히고, resulting exploration order를 밝히고, consequence를 밝히십시오.

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는 입력 크기에 따른 성장을 설명하며, 초를 의미하지 않습니다. "빠르다"는 복잡도의 답이 아닙니다.
- 입력을 두 배로 늘리면 $O(n)$ 시간은 두 배가 되고, $O(n^2)$ 시간은 네 배가 됩니다. 순서에서 추론하십시오.
- 공간 복잡도는 추가 메모리이므로, 배열의 크기가 $n$임에도 불구하고 인플레이스 정렬은 $O(1)$이다.
- 스택은 깊이 우선, 큐는 너비 우선을 제공한다. 이 두 가지가 정확한 순서로 매칭되는 것이 여러 문제의 핵심이다.
이해했습니다
- time complexity in Big-O describes growth with $n$: $O(1)$, $O(\log n)$, $O(n)$, $O(n \log n)$, $O(n^2)$; constants are dropped because at scale the order decides
- $n$를 배가하면 $O(n)$도 배가되고 $O(n^2)$는 네 배가 되며, 더 '나쁜' 알고리즘과의 교차점은 오직 작은 $n$에서만 존재한다.
- 공간 복잡도는 추가 메모리를 의미하며: 인플레이스 정렬은 $O(1)$, 병합 정렬은 $O(n)$이며, 재귀는 스택 깊이를 소모한다.
- 스택은 깊이 우선 탐색을 제공하고, 큐는 너비 우선을 제공하며, 이진 트리는 각 단계마다 남은 노드를 절반으로 줄인다.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.