Arrays (Copy)
Introduction to Arrays
- Arrays are a type of data structure that can store multiple elements of the same data type.
- Elements within an array are accessed using a single identifier name.
- Each element in an array is referenced by an index, which specifies its position in the array.
- The first element’s index is known as the lower bound, while the last element’s index is the upper bound.
- Some programming languages automatically define the lower bound, while others require explicit declaration.
Types of Arrays
- One-Dimensional (1D) Arrays
- Also known as lists, 1D arrays are simple linear structures.
- Example of a 1D array with nine elements:
Index myList [0] 27 [1] 19 [2] 36 [3] 42 [4] 16 [5] 89 [6] 21 [7] 16 [8] 55 - The lower bound in this example is 0, and the upper bound is 8.
- Declaring a 1D array in pseudocode:
DECLARE myList : ARRAY[0:8] OF INTEGER - Assigning values:
myList[7] ← 16
- Two-Dimensional (2D) Arrays
- A 2D array represents data in a table-like structure with rows and columns.
- Example of a 2D array with 9 rows and 3 columns:
Index (Row, Column) [0,0] 27 [0,1] 31 [0,2] 17 [1,0] 19 [1,1] 67 [1,2] 48 [2,0] 36 [2,1] 98 [2,2] 29 - Declaring a 2D array in pseudocode:
DECLARE myArray : ARRAY[0:8,0:2] OF INTEGER - Assigning values:
myArray[7,0] ← 16 - Uses of 2D arrays: Often used for grids, matrices, and storing tabular data.
Operations on Arrays
- Linear Search in an Array
- A search algorithm that checks each element sequentially until the desired value is found or the end is reached.
- Pseudocode Example:
DECLARE myList : ARRAY[0:8] OF INTEGER DECLARE index, item, found : INTEGER DECLARE lowerBound ← 0, upperBound ← 8 OUTPUT "Enter item to find" INPUT item found ← FALSE index ← lowerBound REPEAT IF myList[index] = item THEN found ← TRUE ENDIF index ← index + 1 UNTIL (found = TRUE) OR (index > upperBound) - The search stops when the element is found or the array end is reached.
- Bubble Sort in an Array
- A simple sorting algorithm that repeatedly compares adjacent elements and swaps them if needed.
- Pseudocode Example:
DECLARE myList : ARRAY[0:8] OF INTEGER DECLARE index, temp, swap : BOOLEAN DECLARE top, lowerBound ← 0, upperBound ← 8 REPEAT FOR index ← lowerBound TO top - 1 swap ← FALSE IF myList[index] > myList[index + 1] THEN temp ← myList[index] myList[index] ← myList[index + 1] myList[index + 1] ← temp swap ← TRUE ENDIF NEXT top ← top - 1 UNTIL (NOT swap) OR (top = 0) - The algorithm continues until no swaps are made, ensuring the array is sorted.
Advantages of Using Arrays
- Efficient data storage: Arrays allow storing multiple elements under a single identifier.
- Fast access: Direct indexing provides quick access to elements.
- Easy traversal: Elements can be looped through efficiently.
Limitations of Arrays
- Fixed size: Once declared, an array cannot dynamically resize.
- Inefficient insertion/deletion: Adding or removing elements from the middle requires shifting elements.
- Single data type: All elements must be of the same data type.
Practical Applications of Arrays
- Storing records in databases
- Representing matrices in mathematical operations
- Holding a list of items in inventory management
- Sorting and searching algorithms
Conclusion
- Arrays are fundamental data structures used for storing multiple values efficiently.
- Understanding the operations of searching and sorting within arrays is essential for algorithm design.
- 1D arrays store elements in a single row, while 2D arrays store elements in a grid format.
- Sorting and searching techniques such as bubble sort and linear search improve the functionality of arrays in programming.
