מערכים
הוצג
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 מקומות. מערכת ההזמנות שלו אינה מכילה 300 משתנים הנקראים
Seat1A,Seat1B,Seat1C. יש לה מערך אחד, וכרטיס הכניסה שלך הוא כתובת לתוכו: שורה 14, מקום C. - שם אחד, מאות ערכים, כל אחד נמצא על ידי מספר. הוספת שורה ואין צורך לשנות את הקוד; לולאת על המספרים ונבדקו כל המקומות.
- כמעט כל אלגוריתם במבחן Paper 2 עובר על מערך: מחפש בו, מסכם אותו, מיישר אותו או מוצא את הערך הגדול ביותר.
- שיעור זה הוא המילון, ההצהרות וארבעת האלגוריתמים שהבוחן דורש בפסאודוקוד ובמילים.

המילון
- מערך הוא מבנה נתונים המכיל מספר קבוע של אלמנטים מאותו סוג נתונים תחת זיהוי אחד, כאשר כל אלמנט נגיש באמצעות אינדקס.
- הגבול התחתון והגבול העליון הם האינדקס הראשון והאחרון החלים. מספר האלמנטים שווה לגבול העליון פחות הגבול התחתון פלוס 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.
עיבוד מערך 1-ממדי
DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
FOR i ← 1 TO 5
OUTPUT Names[i]
NEXT i
- לולאת
FORמהגבול התחתון ועד הגבול העליון מבקרת בכל אלמנט פעם אחת. - עבור סיכום, ספירה, מקסימום או מינימום, הגדר משתנה מצטבר לפני הלולאה עדכן אותו בתוכה.
מערכים 2-ממדיים
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
Grid[2, 3] ← 99 // row 2, column 3
- האינדקס הראשון הוא השורה, השני הוא העמודה. לולאות מוטבעות מבקרות בכל תא: הלולאה החיצונה בשורות, הפנימית בעמודות.
- השתמש ב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. - החלפה בשלושה קווים באמצעות משתנה זמנית. שני קווים מאבדים ערך.
- הגבול המתכווץ, פחות אחד בכל מעבר, כי הערך הגדול כבר הגיע לקצה.
- במילים, לשאלת טיפול צעד-בצעד: חזור עד למיון; בכל מעבר השוו זוגות סמוכים; החלף כל זוג מחוץ לסדר; לאחר כל מעבר, הערך הגדול ביותר שאינו במיון נמצא בקצה.

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; הצהרת משתנה דורשת גם את ה-סוג (type) וגם את הגבולות. Grid[row, column]: שורה קודמת לעמודה. החלפת הסדר גוררת לקריאה שגויה של תא בכל לולאה מקוננת.- החלפה דורשת משתנה זמנית; חיפוש דורש נתיב "לא נמצא"; בסינון בועות (bubble sort) התהליך נסתיים כאשר סיבוב אינו מבצע שום החלפה, ולא לאחר מספר קבוע של סיבובים.
הבנתך מושלמת
- מערך אחראי למספר קבוע של אלמנטים מאותו סוג תחת מזהה אחד, הנגישים באמצעות אינדקס בין גבול התחתון והעליון; = upper − lower + 1
- 1-D הוא רשימה, 2-D הוא טבלה
[row, column]שעוברת עליה באמצעות לולאות מקוננות; מצהירים עם גבולות וגם סוג. - חיפוש לינארי:
FoundAt ← -1, לולאה, אחסון המקדם, בדיקה לאחר הלולאה; למציאת הערך הגדול ביותר: התחל בA[1], שמור על המיקום. - סינון בועות: מעברים של השוואה והחלפה בין שכנים עם משתנה זמני, דגל
Swapped, גבול הולך וקטן, עד שמעבר לא ביצע החלפות.
מה השתבש? רק צוות האתר רואה זאת.