The document explains arrays as data structures that store collections of elements of the same data type, accessed via indexes. It covers one-dimensional (1D) and two-dimensional (2D) arrays, including their declaration, access methods, and examples. Additionally, it describes linear search and bubble sort algorithms for searching and sorting elements within arrays.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
arrays
The document explains arrays as data structures that store collections of elements of the same data type, accessed via indexes. It covers one-dimensional (1D) and two-dimensional (2D) arrays, including their declaration, access methods, and examples. Additionally, it describes linear search and bubble sort algorithms for searching and sorting elements within arrays.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Term Definition
A collection of elements of the same
Array data type stored under one identifier The position number used to access Index elements (starts at 0 or 1 depending on the language) A single list of elements (like a row of 1D Array boxes) An array with rows and columns (like a 2D Array table) A method of searching each item one by Linear Search one A simple algorithm that compares and Bubble Sort swaps adjacent elements to sort the array •Think of an array like a row of boxes – each box has a number (index) and contains something (a value). An array is a data structure that stores a collection of elements of the same data type, using a single identifier. • Each element in an array is accessed using an index.
•Store multiple items under one name
•Efficient access and manipulation using indexes
DECLARE Marks : ARRAY[1:5] OF INTEGER
This creates an array of 5 integers named Marks. One-Dimensional Arrays (1D Arrays) •A single row/list of elements. •Each item is accessed by one index.
Two-Dimensional Arrays (2D Arrays) •A table-like structure with rows and columns. •Each item is accessed by two indexes. Rows come first, columns second ([row, column]) DECLARE Table : ARRAY[1:3, 1:2] OF INTEGER Table[1,1] ← 10 Table[1,2] ← 20 Col 1 Col 2 Table[2,1] ← 30 Row 1 10 20 Table[2,2] ← 40 Row 2 30 40 Table[3,1] ← 50 Table[3,2] ← 60 OUTPUT Table[2,2] // Outputs 40 Linear Search in 1D Array Find a specific value in the array Linear search is a method of searching for a target value in an array by checking each element one by one, from start to end.
• DECLARE found : BOOLEAN ← FALSE
• FOR i ← 1 TO 5 • IF Scores[i] = 80 THEN • found ← TRUE • OUTPUT "Found at position: ", i • ENDIF • NEXT i • IF found = FALSE THEN • OUTPUT "ValueMarksnot found" = [90, 85, 75, 60, 95] • ENDIF Search for: 75 It will check from Marks[1] to Marks[5], and find 75 at position 3. Bubble Sort in 1D Array Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. It repeats this process until the array is sorted. • FOR i ← 1 TO 4 • FOR j ← 1 TO 5 - i • IF Scores[j] > Scores[j+1] THEN • temp ← Scores[j] Original: [90, 75, 85, 70, 60] • Scores[j] ← Scores[j+1] Sorted: [60, 70, 75, 85, 90] • Scores[j+1] ← temp • ENDIF • NEXT j • NEXT i •Compare Marks[1] with Marks[2] •If Marks[1] > Marks[2], swap them •Move to the next pair •Repeat for all elements (multiple passes)
[90, 85, 75, 60, 95]
Step-by-step: •90 > 85 → swap → [85, 90, 75, 60, 95] •90 > 75 → swap → [85, 75, 90, 60, 95] •90 > 60 → swap → [85, 75, 60, 90, 95] •90 < 95 → no swap Final sorted array after all passes: [60, 75, 85, 90, 95] HW • Create an array called Names of 3 elements and assign the values "Ali", "Zara", and "Tom".Then print the second name. •Declare a 1D array called Names to store 4 student names.
•Write pseudocode to find if "Ali" is in the Names array.
•Use bubble sort to arrange [20, 5, 15, 10] in ascending order.
•What is the difference between a 1D and a 2D array?
•Which algorithm would you use to find a name in an unsorted list?