배열(Data arrays)
도입됨
17:29
데이터 타입 및 구조
프로그램이 저장하는 모든 값에는 데이터 타입이 필요합니다. 올바른 타입을 선택하는 것은 중요합니다. 예를 들어, 품목의 재고 여부를 저장한다고 해 봅시다. '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개의 좌석이 있습니다. 예약 시스템에는
Seat1A,Seat1B,Seat1C라는 이름의 300개의 변수가 있는 것이 아니라, 배열 하나만 있으며, 티켓은 해당 배열 안의 주소입니다: 14행 C좌석. - 하나의 이름, 수백 개의 값, 각각 숫자로 검색됩니다. 행을 추가하면 코드는 변하지 않으며, 숫자를 순환하여 모든 좌석을 확인했습니다.
- Paper 2의 거의 모든 알고리즘은 배열을 순회합니다: 검색, 합산, 정렬, 최대값 찾기 등.
- 이 수업은 어휘, 선언문, 그리고 출제자가 가구어와 언어로 요구하는 네 가지 알고리즘입니다.

어휘
- 배열은 하나의 식별자 아래 고정된 개수의 동일한 데이터 타입을 가진 요소들을 저장하는 데이터 구조이며, 각 요소는 인덱스로 접근합니다.
- **하한(lower bound)**과 **상한(upper bound)**은 유효한 인덱스의 첫 번째와 마지막입니다. 요소의 개수는 상한 − 하한 + 1입니다.
- **차원(dimension)**은 요소가 필요한 인덱스의 수입니다: 리스트는 하나, 표는 두 개.
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.
worked example: 작업이 필요한 배열 선언하기
- 선언에는 식별자, 경계와 데이터 타입이 필요합니다.
- 소수점이 있을 수 있는 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.
1-D 배열 처리하기
DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
FOR i ← 1 TO 5
OUTPUT Names[i]
NEXT i
- 하한부터 상한까지
FOR루프를 돌면 각 요소를 한 번씩 방문합니다. - 합계, 카운트, 최대값 또는 최소값을 구할 때는 루프 전에 running variable을 설정하고 루프 내부에서 업데이트하십시오.
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.
worked example: 버블 정렬 마커 위치
- 스왑(swap)이 이루어지지 않을 때까지 반복되는 바깥쪽 루프; each pass 시작 시 on the
Swappedflag is reset toFALSEand setTRUEinside theIF. - 임시 변수를 통한 세 줄의 스와핑. 두 줄에서 값이 손실됩니다.
- 축소되는 한계, 매 패스마다 하나씩 감소하는데, 이미 가장 큰 값이 끝에 도달했기 때문입니다.
- 단계별 세분화(stepwise-refinement) 문제의 경우, 언어로 설명하면: 정렬될 때까지 반복; 각 패스에서 인접한 쌍을 비교; 순서가 틀린 쌍은 스와핑; 각 패스 후 가장 큰 미정렬 값이 끝에 배치됩니다.

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.
worked example: 제거 및 삽입
- 항목 제거: 선형 검색(linear search)으로 인덱스를 찾습니다; 이후 모든 요소를 한 자리씩 앞으로 이동시켜 빈 공간을 메니다; 마지막 요소를 사용 불가(unused)로 표시하거나 카운트를 줄입니다.
- 정렬된 배열에 삽입: 요소가 더 큰 첫 번째 인덱스를 찾습니다; 마지막부터 시작하여 해당 요소와 이후 모든 요소를 한 자리씩 뒤로 이동시킵니다; 빈 공간에 새로운 값을 저장합니다.
- 빈 공간을 열 때는 끝에서, 닫을 때는 시작에서 이동해야 합니다. 그렇지 않으면 이동하려는 값을 덮어씌우게 됩니다.

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입니다; 선언에는 경계뿐만 아니라 **타입(type)**도 필요합니다. Grid[row, column]: 행(row)을 먼저. 순서를 바꾸면 중첩 루프(nested loop)에서 모든 셀을 잘못 읽게 됩니다.- 스와핑에는 임시 변수가 필요하며, 검색에는 '찾지 못함(not found)' 경로가 필요하며, 버블 정렬은 고정된 횟수의 패스가 아닌, 한 패스로 스와핑이 이루어지지 않을 때 종료됩니다.
이해했습니다
- **배열(array)**은 하나의 식별자 아래 고정된 개수의 동일 타입 **요소(element)**를 보관하며, **하한(lower bound)**과 상한(upper bound) 사이의 **인덱스(index)**를 통해 접근합니다; count = upper − lower + 1
- 1-D는 목록(list), 2-D는 중첩 루프(nested loops)로 탐색하는 표(table)
[row, column]입니다; 경계와 타입을 명시하여 선언합니다. - 선형 검색:
FoundAt ← -1, 루프, 인덱스를 저장, 루프 후 테스트; 가장 큰 값:A[1]에서 시작하여 위치를 유지합니다. - 버블 정렬: 임시 변수와
Swapped플래그, 축소되는 한계를 사용하여 인접 비교-스 와핑(pairs compare-and-swap) 패스를, 한 패스로 스와핑이 이루어지지 않을 때까지 반복합니다.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.