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 |
อัลกอริทึมที่จะอยู่รอดเกินอายุขัยของจักรวาล
- ผู้ขายของต้องเดินทางไปยัง 25 เมืองและกลับสู่บ้านด้วยเส้นทางที่สั้นที่สุด ลองจัดลำดับทุกแบบจะมีทั้งหมดประมาณ $10^{23}$ แบบ หากมีเครื่องคอมพิวเตอร์ตรวจสอบได้หนึ่งพันล้านครั้งต่อวินาที จะใช้เวลาสามล้านปี
- เพิ่มอีกหนึ่งเมืองงานจะคูณด้วย 25 นั่นไม่ใช่คอมพิวเตอร์ที่ต้องเร็วขึ้น; แต่มันเป็นแนวทางที่ไม่สามารถใช้งานได้เลย ไม่ว่าความเร็วหรือฮาร์ดแวร์ใด
- การรู้สิ่งนี้ ก่อนเขียนโปรแกรมคือสิ่งที่การวิเคราะห์ความซับซ้อนใช้ทำ มันคือความแตกต่างระหว่างการใช้เลือกอัลกอริทึมกับการค้นพบว่าอัลกอริทึมของคุณไม่สามารถขยายขนาดได้ในภายหลัง
- This lesson is Big-OO for time and space, and how ADTs shape the algorithms built on them.

การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
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²).
ตัวอย่างฝึกหัด: ผลของการเพิ่มข้อมูลเข้าเป็นสองเท่า
- อัลกอริทึมใช้เวลา 4 วินาทีกับ 1,000 รายการ. ประมาณการเวลาสำหรับ 2,000 รายการถ้ามันคือ $O(n)$, จากนั้นถ้ามันคือ $O(n^2)$.
- $O(n)$: การเพิ่มข้อมูลเข้าเป็นสองเท่า $n$ จะทำให้เวลาเป็นสองเท่า, ดังนั้นประมาณ 8 วินาที.
- $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.
ความซับซ้อนของพื้นที่
- ความซับซ้อนของพื้นที่คือหน่วยความจำ เพิ่มเติมที่อัลกอริทึมต้องการ นอกเหนือจากข้อมูลเข้าเอง
- Bubble และ insertion sort ใช้ $O(1)$ หน่วยความจำเพิ่มเติม: They work in place, ต้องการตัวแปรเพียงไม่กี่ตัว Merge sort ใช้ $O(n)$, เพราะสร้างอาร์เรย์เสริม
- การเรียกซ้ำใช้ stack memory สัดส่วนกับความ ลึก, เพราะทุก call ที่ยังไม่เสร็จคง frame ของมันไว้
- มักมี time and memory trade-off: การเก็บผลลัพธ์เพื่อหลีกเลี่ยงการคำนวณซ้ำ, เช่น memoisation, ซื้อความเร็วด้วยพื้นที่

สิ่งอื่นที่ตัดสินใจการเลือก
- Big-O is about growth, not absolute speed. For a small $n$, a simple $O(n^2)$ algorithm can beat a complicated $O(n \log n)$ one, and it is easier to write correctly.
- Stability matters when a list is already ordered by another field. Simplicity matters because a simple algorithm has fewer places to hide a bug.
- 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 inside algorithms
- The abstract data types from topic 10 are the machinery algorithms are built from, and choosing one shapes the algorithm.
- A stack gives depth-first search: push the neighbours, take the most recent, and the search plunges down one path before backing up. Recursion uses the call stack for exactly this.
- A queue gives breadth-first search: enqueue the neighbours, take the oldest, and the search spreads outward in rings, which is what finds the shortest path in an unweighted graph.
- A binary tree keeps values in order so that a search discards half the remaining nodes at each step, giving binary search's $O(\log n)$ over a structure that can also grow.
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: the same graph, two searches
- A maze is explored from one entrance. Contrast using a stack with using a queue.
- With a stack, the most recently found path is explored next, so the search goes deep down one route until it dead-ends, then backtracks. It uses memory proportional to the depth of the path.
- With a queue, the oldest found path is explored next, so the search examines everything one step away, then everything two steps away. It finds the shortest route first, but holds every position at the current distance in memory.
- Name the ADT, name the resulting order of exploration, and name the 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 describes growth with input size, not seconds. "It is fast" is not a complexity answer.
- Doubling the input doubles an $O(n)$ time and quadruples an $O(n^2)$ one. Reason from the order.
- ความซับซ้อนของพื้นที่ (Space complexity) คือ หน่วยความจำเพิ่มเติม ดังนั้นการเรียงลำดับแบบใน-place จึง $O(1)$ แม้ว่าจะมีขนาดอาร์เรย์เป็น $n$ ก็ตาม
- Stack ให้การค้นหาแบบลึกก่อน (depth-first), Queue ให้การค้นหาแบบกว้างก่อน (breadth-first). การจับคู่สองสิ่งนี้ให้ถูกต้องเป็นหัวใจสำคัญที่พบได้บ่อยในหลายข้อสอบ
คุณเข้าใจแล้ว
- ความซับซ้อนของเวลา (time complexity) ในรูปแบบ Big-O อธิบายถึงอัตราการเติบโตเมื่อ $n$: $O(1)$, $O(\log n)$, $O(n)$, $O(n \log n)$, $O(n^2)$; ค่าคงที่จะถูกตัดทิ้งไปเพราะเมื่อสเกลใหญ่ขึ้น ลำดับความสำคัญ (order) จะกำหนดพฤติกรรมหลัก
- การเพิ่ม $n$ เป็นสองเท่าจะทำให้ $O(n)$ เพิ่มเป็นสองเท่าและ $O(n^2)$ เพิ่มเป็นสี่เท่า; จุดตัดกับ "อัลกอริทึมที่แย่กว่า" มีอยู่เฉพาะสำหรับ $n$ ขนาดเล็กเท่านั้น
- ความซับซ้อนของพื้นที่ คือหน่วยความจำเพิ่มเติม: การเรียงลำดับแบบ in place เป็น $O(1)$, merge sort เป็น $O(n)$, และการเรียกซ้ำ (recursion) ต้องใช้ความลึกของ stack
- stack ให้การค้นหาแบบ ลึกก่อน (depth-first), queue ให้การค้นหาแบบ กว้างก่อน (breadth-first), และ binary tree จะลดจำนวนโหนดที่เหลืออยู่ลงครึ่งหนึ่งในแต่ละขั้นตอน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้