Arreglo
แนะนำใหม่
17:29
ประเภทข้อมูล & โครงสร้างข้อมูล
ทุกค่าที่คุณโปรแกรมจัดเก็บต้องมีประเภทข้อมูล — และการเลือกให้ถูกต้องนั้นสำคัญ Suppose คุณต้องการจัดเก็บว่าสินค้ามีในสต็อกหรือไม่ คุณอาจพิมพ์คำว่า yes…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| array/əˈreɪ/ | อาร์เรย์ |
| row/rəʊ/ | A horizontal sequence of entries in a table or array, or a single record in a database table. |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| declarations | Statements that give variables their names and data types, and arrays their bounds. |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| data structure/ˈdeɪtə ˈstrʌktʃə/ | A way of organising and storing data to support particular operations. |
| data type/ˈdeɪtə taɪp/ | A classification of values that determines their representation and permitted operations. |
| identifier/aɪˈdentɪfaɪə/ | A name used to identify a program element such as a variable, function or class. |
| index/ˈɪndeks/ | ดัชนี |
| lower bound/ˈləʊə baʊnd/ | ขอบเขตล่าง |
| upper bound/ˈʌpə baʊnd/ | ขอบเขตบน |
| element/ˈelɪmənt/ | องค์ประกอบ |
| dimension/daɪˈmenʃn/ | มิติ |
| column/ˈkɒlʌm/ | A vertical arrangement of entries, or a named field in a database table. |
| Nested loops/ˈnestɪd luːps/ | Loops placed inside other loops, with the inner loop running for each outer loop cycle. |
| outer loop/ˈaʊtə luːp/ | The loop that contains another loop and controls how often it runs. |
| linear search/ˈlɪnɪə sɜːtʃ/ | การค้นหาเชิงเส้น |
| Initialise/ɪˈnɪʃəlaɪz/ | To give a variable its starting value before it is used. |
| bubble sort/ˈbʌbl sɔːt/ | การเรียงลำดับแบบฟองอากาศ |
| swap/swɒp/ | To exchange two values so that each takes the other's place. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| temporary variable | A variable that holds a value for a short time while other values change. |
| stepwise-refinement | The process of breaking a task into smaller, more detailed steps. |
| sorted array | An array whose elements are arranged in a chosen order. |
| overwrite/ˌəʊvəˈraɪt/ | To replace a stored value with another value, losing the original. |
ที่นั่ง 14C
- โรงหนังมีที่นั่ง 300 ที่ ระบบจองไม่ได้มีตัวแปร 300 ตัวชื่อว่า
Seat1A,Seat1B,Seat1Cมีเพียง อาเรย์ เดียว และตั๋วของคุณคือที่อยู่ภายในอาเรย์นั้น: แถว 14, ที่นั่ง C - ชื่อเดียว, หลายร้อยค่า, แต่ละตัวหาได้จากตัวเลข เพิ่มแถวหนึ่งโค้ดก็ไม่ต้องเปลี่ยน;วนซ้ำตามตัวเลขและคุณตรวจสอบทุกที่นั่งแล้ว
- เกือบทุกอัลกอริทึมในกระดาษข้อสอบ 2 จะวนผ่านอาเรย์: ค้นหา, รวม, เรียงลำดับ, หาค่าสูงสุด
- This lesson is the vocabulary, the declarations, and the four algorithms the examiner asks for in pseudocode and in words.

คำศัพท์เฉพาะ
- อาเรย์ เป็นโครงสร้างข้อมูลเก็บจำนวนคงที่ของ องค์ประกอบ ของประเภทเดียวกันภายใต้ชื่อเดียว แต่ละองค์ประกอบเข้าถึงได้ด้วย ดัชนี
- ขอบเขตล่าง และ ขอบเขตบน คือดัชนีที่ถูกต้องตัวแรกและตัวสุดท้าย จำนวนองค์ประกอบคือ ขอบเขตบน − ขอบเขตล่าง + 1
- มิติ คือจำนวนดัชนีที่องค์ประกอบต้องการ: หนึ่งสำหรับรายการ, สองสำหรับตาราง
- ใน
ThisArray[n] ← 42อาเรย์มีหนึ่งมิติ, ดัชนีคือตัวแปรINTEGERn, และองค์ประกอบที่ดัชนียังรับค่า42

ชื่อเดียว, ดัชนีสำหรับแต่ละองค์ประกอบ, ขอบเขตทั้งสองด้าน

An array stores:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An array is an ordered collection of same-type items accessed by index. (A record groups different types.)
An array is a data structure holding many values of the ______ type under one name.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each value is reached by its index.
DECLARE Marks : ARRAY[0:99] OF INTEGER declares an array of ____ elements.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Upper bound minus lower bound plus one: 99 − 0 + 1 = 100. Both bounds are valid indices.
ตัวอย่างที่แสดงวิธีทำ: การประกาศอาร์เรย์ที่งานหนึ่งต้องการใช้
- การประกาศต้องมีชื่อ, ขอบเขต และ ประเภทข้อมูล
- 120 ค่าอ่านที่อาจมีทศนิยม:
DECLARE Data : ARRAY[1:120] OF REAL - ตาราง 150 แถวและสองคอลัมน์ข้อความ:
DECLARE Names : ARRAY[1:150, 1:2] OF STRING - บอกจำนวนถ้าถูกถาม:
[0:99]เก็บ 100 องค์ประกอบ, ไม่ใช่ 99
Which declaration holds a table of 150 rows and 2 columns of text?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Two dimensions, each with a lower and upper bound, and the element type. The second option is one long list; the third has no type; the fourth has no lower bounds.
ประมวลผล-array 1-D
DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
FOR i ← 1 TO 5
OUTPUT Names[i]
NEXT i
- ลูป
FORจากขอบเขตล่างถึงขอบเขตบน จะเยี่ยมชมแต่ละองค์ประกอบหนึ่งครั้ง - สำหรับการรวม, นับ, สูงสุด หรือ ต่ำสุด ตั้งตัวแปรสะสมก่อนลูปและอัปเดตภายใน
อาร์เรย์ 2-D
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
Grid[2, 3] ← 99 // row 2, column 3
- ดัชนีตัวแรกคือ แถว, ตัวที่สองคือ คอลัมน์ ลูปซ้อน (Nested loops) จะเยี่ยมเซลล์ทุกเซลล์: ลูปนอกวนรอบแถว, ลูปในวนรอบคอลัมน์
- ใช้ 1-D สำหรับลำดับเดียวและ 2-D เมื่อข้อมูลมีสองมิติตามธรรมชาติ เช่น กริดที่นั่งหรือตารางคะแนนตามนักเรียนและวิชา

Grid[row, column], ต้องเรียงตามนั้นเสมอ

Index a 2-D array by [row, column]
A 2-D array is a grid. Grid[row, column] reaches exactly one cell — change the row and column to see which value you land on.
In Grid[2, 3], which cell is accessed?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The first index is the row, the second the column — so row 2, column 3.
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Worked example: a linear search that can say "not found"
- A linear search 线性查找 checks elements in turn; the example below checks the whole array and records the last matching index, if any.
FoundAt ← -1
FOR i ← 1 TO n
IF A[i] = Target THEN
FoundAt ← i
ENDIF
NEXT i
IF FoundAt = -1 THEN
OUTPUT "Not found"
ELSE
OUTPUT "Found at ", FoundAt
ENDIF
-1is outside the indices1ton, so it means "not found" in this example. Initialise 初始化 it before the loop and test it after. A search that never says "not found" loses a mark.
A linear search finds a value by:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A linear search examines elements one by one from the start until it finds the target (or reaches the end).
Setting FoundAt to -1 before a linear search lets the program report "not found" after the loop.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
For an array indexed from 1 to n, -1 is not a valid index, so if it is unchanged after the loop the target was not in the array.
ค่ามากที่สุด และตำแหน่งของมัน
Largest ← A[1]
Position ← 1
FOR i ← 2 TO n
IF A[i] > Largest THEN
Largest ← A[i]
Position ← i
ENDIF
NEXT i
OUTPUT Largest, " at ", Position
- เริ่ม
Largestที่องค์ประกอบแรก อย่าเริ่มที่ 0: อาร์เรย์อาจมีค่าติดลบทั้งหมด - รูปทรงเดียวกันนับหรือแสดงค่าที่ไม่ว่าง: เปรียบเทียบแต่ละค่ากับเครื่องหมายของตำแหน่งที่ไม่ใช้,
""หรือ-1, แล้วนับเฉพาะค่าที่แตกต่างกันเท่านั้น
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Bubble sort
- A bubble sort 冒泡排序 makes repeated passes through the array comparing adjacent pairs and swapping those out of order, until a pass makes no swaps 交换.
- After each pass the largest unsorted value has bubbled to the end, so the next pass can stop one place earlier.
Limit ← n // n is the number of elements in Data
REPEAT
Swapped ← FALSE
FOR Index ← 1 TO Limit - 1
IF Data[Index] > Data[Index + 1] THEN
Temp ← Data[Index]
Data[Index] ← Data[Index + 1]
Data[Index + 1] ← Temp
Swapped ← TRUE
ENDIF
NEXT Index
Limit ← Limit - 1
UNTIL Swapped = FALSE

Each pass carries the largest remaining value to the end
Put the steps of one bubble-sort pass, and its ending, in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Reset the flag, sweep and swap, shrink the limit, stop when a whole pass made no swap.
ตัวอย่างวิธีทำ: ตำแหน่งของเครื่องหมายการเรียงลำดับแบบฟองอากาศ
- ลูปภายนอกที่ทำซ้ำจนกว่าการวนผ่านหนึ่งรอบจะไม่มีการสลับ; ตัวบ่งชี้
Swappedรีเซ็ตเป็นFALSEที่เริ่มต้นของการวนผ่านแต่ละรอบ และถูกตั้งค่าเป็นTRUEภายในIF - การสลับ แบบสามบรรทัดโดยใช้อตัวแปรชั่วคราว สองบรรทัดจะสูญเสียค่าลง
- The shrinking limit, one less each pass, because the largest value has already reached the end.
- ในคำพูด สำหรับคำถามที่เน้นการปรับปรุงแบบขั้นบันได: ทำซ้ำจนกว่าจะเรียงลำดับแล้ว; ในการแต่ละรอบ ให้เปรียบเทียบคู่ขององค์ประกอบที่อยู่ติดกัน; สลับคู่ใดที่มีลำดับผิด; หลังแต่ละรอบ ค่าที่ไม่ได้เรียงลำดับที่มีขนาดใหญ่สุดจะอยู่ที่ปลาย

Which features earn marks in an efficient bubble sort? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Flag, swap with a temporary, shrinking limit: those are the marks. Copying the array is not part of the algorithm.
ตัวอย่างวิธีทำ: การลบและการแทรก
- ลบรายการ: หาดัชนีโดยใช้การค้นหาเชิงเส้น; ย้ายองค์ประกอบทั้งหมดที่อยู่ถัดไปเข้าหาจุดเริ่มต้นหนึ่งตำแหน่งเพื่อให้ช่องว่างปิดลง; ทำเครื่องหมายว่าองค์ประกอบสุดท้ายไม่ถูกใช้งาน หรือลดจำนวนลง
- แทรกลงในอาร์เรย์ที่เรียงลำดับแล้ว: หาดัชนีแรกที่มีองค์ประกอบมีค่ามากกว่า; ย้ายองค์ประกอบนั้นและองค์ประกอบทั้งหมดที่อยู่ถัดไปเข้าหาปลายหนึ่งตำแหน่ง โดยเริ่มจากตัวสุดท้าย; เก็บค่าใหม่ในช่องว่าง
- เคลื่อนย้ายจาก ปลาย เมื่อเปิดช่องว่าง และจาก ต้นทาง เมื่อปิดช่องว่าง มิฉะนั้นจะเขียนทับค่าที่กำลังจะเคลื่อนย้าย

An array holds many items of the SAME type reached by index, while a record groups fields of (possibly) DIFFERENT types reached by name.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A 2-D array suits a grid (rows × columns); a record suits one thing described by several named fields.
คะแนนที่หลุดหายไป
[0:99]เก็บข้อมูลได้ 100 องค์ประกอบ นับขอบเขตทั้งสองข้าง- ดัชนีคือ
INTEGER; การประกาศต้องระบุทั้ง ประเภท และขอบเขตด้วย Grid[row, column]: row first. Swapping them reads the wrong cell in every nested loop.- การสลับต้องการตัวแปรชั่วคราว; การค้นหาต้องการเส้นทาง "ไม่พบ"; การเรียงลำดับแบบฟองอากาศจะจบเมื่อการวนผ่านหนึ่งรอบไม่มีการสลับ ไม่ใช่หลังจากการวนผ่านจำนวนที่กำหนดตายตัว
คุณเข้าใจแล้ว
- อาร์เรย์ เก็บองค์ประกอบชนิดเดียวกันจำนวนคงที่ภายใต้ชื่อเดียว เข้าถึงได้ด้วย ดัชนี ระหว่าง ขอบเขตล่าง และ ขอบเขตบน; จำนวน = ขอบเขตบน − ขอบเขตล่าง + 1
- 1-D คือรายการ, 2-D เป็นตาราง
[row, column]เข้าถึงด้วยลูปซ้อนกัน; ประกาศด้วยขอบเขต และ ประเภทข้อมูล - linear search:
FoundAt ← -1, ลูป, เก็บดัชนี, ทดสอบหลังลูป; ค่าสูงสุด: เริ่มต้นที่A[1], รักษาตำแหน่ง - bubble sort: การวนผ่านเปรียบเทียบและสลับคู่ที่อยู่ติดกันพร้อมตัวแปรชั่วคราว, ตัวบ่งชี้
Swapped, ขีดจำกัดที่หดเล็กลง, จนกว่าการวนผ่านหนึ่งรอบจะไม่มีการสลับ
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้