Advanced Arrays
Go beyond basic arrays. Work with 2D matrices, implement sorting algorithms, and master binary search.
Learning Objectives:
- β’Declare and traverse 2D arrays (matrices)
- β’Implement bubble sort from scratch
- β’Use binary search on sorted arrays
- β’Transpose a matrix
- β’Solve classic matrix problems (saddle point)
Going Deeper with Arrays
In Level 1 you learned the basics of arrays: declare, access, loop. Now we go further β 2D arrays, classic sorting, and efficient searching.
2D Arrays (Matrices)
A 2D array is an array of arrays. Think of it as a grid with rows and columns.
python# A 3x3 matrix matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(matrix[1][2]) # Row 1, Column 2 β 6
To read a matrix from input (M rows, N columns):
pythonM, N = map(int, input().split()) matrix = [] for i in range(M): row = list(map(int, input().split())) matrix.append(row)
Transposing a Matrix
The transpose of a matrix swaps rows and columns: element [i][j] moves to [j][i].
python# Transpose a 3x3 matrix def transpose(matrix): n = len(matrix) result = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): result[j][i] = matrix[i][j] return result
Bubble Sort
Bubble sort repeatedly compares adjacent elements and swaps them if they're in the wrong order.
pythondef bubble_sort(arr): n = len(arr) for i in range(n): for j in range(n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr
Time complexity: O(nΒ²) β fine for small arrays, too slow for large ones (that's why we'll learn better algorithms in Level 3).
Binary Search
If an array is already sorted, you can find an element much faster than checking every element. Binary search divides the search space in half at each step.
pythondef binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid # Found at index mid elif arr[mid] < target: left = mid + 1 # Target is in the right half else: right = mid - 1 # Target is in the left half return -1 # Not found
Time complexity: O(log n) β searching 1 billion elements takes at most 30 steps!
The Saddle Point
A saddle point in a matrix is a value that is simultaneously the minimum of its row and the maximum of its column. Not every matrix has one.
plaintextMatrix: 1 2 3 4 5 2 β Row 1, min = 2 (column 2) 3 1 4 Column 2: [3, 2, 4] β max = 4 (not 2) No saddle point in column 2. Try again: 1 2 3 4 5 6 β Row 1, min = 4 (column 0) 7 8 9 Column 0: [1, 4, 7] β max = 7 (not 4) No saddle point here either.
Finding saddle points is a great exercise in nested loops and matrix reasoning.
Exercises
Exercise 1: Matrix Sum
Read an M by N matrix and print the sum of all its elements.
Exercise 2: Maximum of Each Row
Read a matrix and print the maximum value of each row.
Exercise 3: Transpose a Matrix
Read a square matrix and print its transpose.
Exercise 4: Bubble Sort
Sort the given integers in ascending order using bubble sort.
Exercise 5: Binary Search
Use binary search to report whether each query appears in the sorted array.
Exercise 6: Saddle Point
Find the saddle point or points in the given matrix.