Log In Sign Up
Day 5

Day 5: Multi-Dimensional Arrays and Sorting Algorithms

5/60 Days

Day 5: Multi-Dimensional Arrays and Sorting Algorithms #

Welcome to Day 5 of our 60 Days of Coding Algorithm Challenge! Today, we’ll delve into the world of multi-dimensional arrays (such as matrices) and learn about some fundamental sorting algorithms. These are essential concepts in computer science that will provide you with the tools to solve more complex problems.

Multi-Dimensional Arrays #

A multi-dimensional array is essentially an array of arrays. The most common example is a two-dimensional array, often referred to as a matrix. Multi-dimensional arrays are used in a wide range of applications, from representing grids in games to storing data in scientific computing.

Example: 2D Array (Matrix) #

Let’s explore how to create and manipulate a 2D array:

 1# Initializing a 2D array (3x3 matrix)
 2matrix = [
 3    [1, 2, 3],
 4    [4, 5, 6],
 5    [7, 8, 9]
 6]
 7
 8# Accessing an element at row 1, column 2
 9print(matrix[1][2])  # Output: 6
10
11# Traversing the matrix and printing all elements
12for row in matrix:
13    for element in row:
14        print(element, end=" ")
15    print()  # Newline after each row

Common Operations on 2D Arrays …