Skip to content

Commit

Permalink
added some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-dhangar committed Oct 2, 2024
1 parent f8a14a1 commit 59a7123
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 141 deletions.
238 changes: 99 additions & 139 deletions docs/arrays/2d-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,195 +7,155 @@ description: "In this blog post, we'll delve into the world of two-dimensional a
tags: [dsa, arrays, 2d arrays]
---

Welcome, curious coder! Today, we embark on an exciting journey through the world of two-dimensional arrays. These versatile data structures are the backbone of numerous algorithmic problems and are a staple in the toolkit of any proficient programmer. Whether you're working on games, simulations, or complex data analysis, understanding 2D arrays is crucial. Let's dive in and explore their structure, uses, and how to manipulate them efficiently.
A two-dimensional (2D) array is an array of arrays, where each element of the main array is another array. It's often visualized as a table or matrix with rows and columns. Two-dimensional arrays are essential in various algorithmic problems, particularly when handling matrices, grids, and dynamic programming solutions.

## What is a Two-Dimensional Array?
In this guide, we will cover the fundamentals of two-dimensional arrays, their applications, and common operations used in data structures and algorithms (DSA).

Imagine a spreadsheet, a grid of rows and columns where each cell can hold a piece of data. This is essentially what a two-dimensional (2D) array is: a collection of data organized in a tabular format. Instead of having a single index like a one-dimensional array, a 2D array uses two indices to access its elements, typically represented as `array[row][column]`.
## 1. What is a Two-Dimensional Array?

In pseudo-code, we define a 2D array as follows:
A two-dimensional array is an array that consists of a collection of elements arranged in rows and columns. Each element can be accessed using two indices – one representing the row and the other representing the column.

```
DECLARE array[rowCount][columnCount]
```
### Representation:
- A two-dimensional array can be visualized as:
```plaintext
[a11, a12, a13]
[a21, a22, a23]
[a31, a32, a33]
```

In C++, this can be represented as:
Here, each row is an individual array, and all rows combined form the 2D array.

```cpp
int array[rowCount][columnCount];
```
## 2. Declaration and Initialization of 2D Arrays

## Initializing a 2D Array

Initialization of a 2D array can be done in multiple ways. Here’s a simple example in pseudo-code:
### Declaration:
In most programming languages, a 2D array is declared by specifying the number of rows and columns. For example, in C++:

```cpp title="C++"
int arr[3][4]; // Declares a 2D array with 3 rows and 4 columns
```
DECLARE array[3][3]
array[0][0] = 1
array[0][1] = 2
array[0][2] = 3
array[1][0] = 4
array[1][1] = 5
array[1][2] = 6
array[2][0] = 7
array[2][1] = 8
array[2][2] = 9

In Python, a 2D array can be initialized using nested lists:

```python title="Python"
arr = [[0] * 4 for _ in range(3)] # Creates a 3x4 matrix filled with zeros
```

And here’s how it looks in C++:
### Initialization:
2D arrays can be initialized at the time of declaration:

```cpp
int array[3][3] = {
```cpp title="C++"
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
{4, 5, 6}
};
```

## Traversing a 2D Array

One of the fundamental operations is traversing a 2D array. This is often done using nested loops. Here’s a pseudo-code example to print all elements of a 3x3 array:
## 3. Accessing and Modifying Elements in a 2D Array

```
FOR row FROM 0 TO 2
FOR column FROM 0 TO 2
PRINT array[row][column]
```
Each element in a 2D array is accessed using two indices – the first for the row and the second for the column.

And in C++:
### Example:

```cpp
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
std::cout << array[row][column] << " ";
}
std::cout << std::endl;
}
int element = arr[1][2]; // Accesses the element at row 1, column 2
arr[0][1] = 10; // Modifies the element at row 0, column 1
```

## Common Uses of 2D Arrays

Two-dimensional arrays shine in various algorithmic problems and real-world applications. Here are a few common scenarios where 2D arrays are indispensable:

### 1. Matrix Operations

Matrices are a classic use case for 2D arrays. Operations like addition, subtraction, and multiplication of matrices are performed using nested loops to iterate over the elements.

Matrix Addition Pseudo-Code:
In Python:

```python
element = arr[1][2]
arr[0][1] = 10
```
DECLARE matrixA[rowCount][columnCount]
DECLARE matrixB[rowCount][columnCount]
DECLARE matrixSum[rowCount][columnCount]

FOR row FROM 0 TO rowCount - 1
FOR column FROM 0 TO columnCount - 1
matrixSum[row][column] = matrixA[row][column] + matrixB[row][column]
```
## 4. Traversing a 2D Array

Matrix Addition in C++:
Traversing a 2D array involves visiting each element of the array. Typically, this is done using nested loops, where the outer loop iterates over the rows, and the inner loop iterates over the columns.

```cpp
int matrixA[2][2] = {{1, 2}, {3, 4}};
int matrixB[2][2] = {{5, 6}, {7, 8}};
int matrixSum[2][2];
### Example in C++:

for (int row = 0; row < 2; ++row) {
for (int column = 0; column < 2; ++column) {
matrixSum[row][column] = matrixA[row][column] + matrixB[row][column];
```cpp
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
```

### 2. Image Processing
Images can be represented as 2D arrays where each element corresponds to a pixel value. Manipulating images, applying filters, or detecting edges often involves traversing and modifying 2D arrays.
### 3. Game Boards
Games like chess, tic-tac-toe, and Sudoku use 2D arrays to represent the game board. Each element in the array represents a cell on the board, storing the state of the game.
Example: Tic-Tac-Toe Board in Pseudo-Code
### Example in Python:
```python
for row in arr:
for element in row:
print(element, end=" ")
print()
```
DECLARE board[3][3]
board[0][0] = 'X'
board[0][1] = 'O'
board[0][2] = 'X'
board[1][0] = 'O'
board[1][1] = 'X'
board[1][2] = 'O'
board[2][0] = 'X'
board[2][1] = 'X'
board[2][2] = 'O'
```
## Algorithmic Challenges Involving 2D Arrays
2D arrays often appear in algorithmic challenges and coding interviews. Here are a couple of classic problems:
### 1. Rotating a Matrix
Given an `N x N` matrix, rotate it 90 degrees clockwise. This involves first transposing the matrix and then reversing the rows.
Rotation Pseudo-Code:

```
DECLARE matrix[N][N]
---

// Transpose the matrix
FOR row FROM 0 TO N - 1
FOR column FROM row TO N - 1
SWAP matrix[row][column] WITH matrix[column][row]
## 5. Common Operations on 2D Arrays

// Reverse each row
FOR row FROM 0 TO N - 1
REVERSE matrix[row]
### a. Matrix Addition
Two matrices of the same dimensions can be added element-wise.
```cpp
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
```

Rotation in C++:
### b. Matrix Multiplication
Matrix multiplication is performed by multiplying the rows of the first matrix by the columns of the second matrix.
```cpp
int matrix[N][N]; // Assume this is initialized
// Transpose the matrix
for (int row = 0; row < N; ++row) {
for (int column = row; column < N; ++column) {
std::swap(matrix[row][column], matrix[column][row]);
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
```

// Reverse each row
for (int row = 0; row < N; ++row) {
std::reverse(matrix[row], matrix[row] + N);
### c. Transposing a Matrix
Transposing a matrix involves flipping its rows and columns.
```cpp
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[j][i] = matrix[i][j];
}
}
```

### 2. Finding the Largest Sum Subgrid
---

Given a `M x N` matrix, find the subgrid with the largest sum. This problem can be solved using dynamic programming and is a 2D extension of the 1D maximum subarray problem (Kadane's algorithm).
## 6. Applications of 2D Arrays in DSA

Largest Sum Subgrid Pseudo-Code:
- **Dynamic Programming**: Many DP problems such as "Knapsack", "Longest Common Subsequence", and "Edit Distance" utilize 2D arrays to store solutions to subproblems.
- **Graph Representation**: Adjacency matrices use 2D arrays to represent graphs.
- **Image Processing**: A grayscale image can be represented as a 2D array of pixel intensities.
- **Game Boards**: Games like chess and tic-tac-toe use 2D arrays to represent the game board.
- **Mathematical Operations**: Matrix operations like addition, multiplication, and transposition are fundamental in linear algebra and often implemented using 2D arrays.

```
DECLARE maxSum = -INFINITY
FOR startRow FROM 0 TO M - 1
DECLARE temp[columnCount]
FOR endRow FROM startRow TO M - 1
FOR column FROM 0 TO N - 1
temp[column] = temp[column] + matrix[endRow][column]
DECLARE currentMax = Kadane(temp)
maxSum = MAX(maxSum, currentMax)
```
---

## 7. Example Problems

## Tips for Working with 2D Arrays
### Problem 1: Rotating a Matrix by 90 Degrees
Given a `n x n` matrix, rotate the matrix by 90 degrees clockwise.

1. **Boundary Checks**: Always ensure your indices are within the bounds of the array to avoid runtime errors.
2. **Memory Considerations**: Be mindful of the memory usage, especially for large 2D arrays, as they can consume a significant amount of memory.
3. **Initialization**: Properly initialize your arrays to avoid unexpected behavior due to garbage values.
### Problem 2: Spiral Matrix Traversal
Given a matrix, print the elements in a spiral order starting from the top-left corner.

## In Conclusion
### Problem 3: Find the Largest Island
Given a binary matrix where `1` represents land and `0` represents water, find the largest connected land mass (island).

### Problem 4: Matrix Multiplication
Write a program to multiply two matrices of dimensions `m x n` and `n x p`.

---

Two-dimensional arrays are a fundamental data structure that every programmer should master. They are used in a wide variety of applications, from simple data storage to complex algorithmic challenges. By understanding how to manipulate and use 2D arrays effectively, you'll be well-equipped to tackle a broad range of programming problems. <br />
## Conclusion

So, next time you encounter a grid-based problem or need to perform operations on tabular data, you'll have the confidence and knowledge to use 2D arrays to their full potential. Happy coding!
Two-dimensional arrays are a foundational data structure in computer science, used in a variety of real-world applications, including graph theory, dynamic programming, and game development. By understanding how to declare, traverse, and manipulate 2D arrays, you can effectively solve complex problems in DSA.
7 changes: 7 additions & 0 deletions docs/arrays/arrays-bubblesort-dsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ description: "Bubble Sort is a simple sorting algorithm that repeatedly steps th
tags: [dsa, arrays, sorting, bubble-sort, algorithm of bubble-sort, pseudocode of bubble-sort, complexity of bubble-sort, example of bubble-sort, live example of bubble-sort, explanation of bubble-sort, quiz of bubble-sort, conclusion of bubble-sort]
---

<AdsComponent />

**Bubble Sort** is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements <mark>bubble</mark> to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to <mark>insertion sort</mark>. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.


Expand Down Expand Up @@ -40,6 +42,8 @@ procedure bubbleSort( A : list of sortable items )
end procedure
```

<AdsComponent />

## Diagram

```mermaid
Expand Down Expand Up @@ -132,6 +136,8 @@ In the above example, we have an array of numbers `[64, 34, 25, 12, 22, 11, 90]`
Change the array values and see how the bubble sort algorithm sorts the array.
:::

<AdsComponent />

:::tip 📝 Note
Bubble sort is not a practical sorting algorithm when the input is large. It is not suitable for large datasets due to its O(n<sup>2</sup>) time complexity.

Expand All @@ -156,6 +162,7 @@ Bubble sort is not an efficient algorithm for large datasets and is generally no

Insertion Sort, Selection Sort, Merge Sort, Quick Sort, etc.

<AdsComponent />

## Quiz

Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ slug: /

Algo is your gateway to mastering Data Structures and Algorithms (DSA). Whether you're a coding enthusiast, a student, or a professional looking to enhance your programming skills, Algo is here to guide you through the intricate world of DSA.

<AdsComponent />

## What Sets Algo Apart?

### Comprehensive Learning Pathways
Expand Down
40 changes: 40 additions & 0 deletions src/components/AdsComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useEffect } from "react";
import Head from "@docusaurus/Head";

declare global {
interface Window {
adsbygoogle: any[];
}
}

const AdsComponent: React.FC = () => {
useEffect(() => {
try {
(window.adsbygoogle = window.adsbygoogle || []).push({});
}
catch (err) {
console.error(err);
}
}, []);
return (
<>
<Head>
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5832817025080991"
crossOrigin="anonymous"
/>
</Head>
<ins
className="adsbygoogle"
style={{ display: "block", textAlign: "center" }}
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-5832817025080991"
data-ad-slot="3270832720"
/>
</>
);
};

export default AdsComponent;
4 changes: 2 additions & 2 deletions src/theme/MDXComponents.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import "react-lite-youtube-embed/dist/LiteYouTubeEmbed.css";
// import AdsComponent from "@site/src/components/AdsComponent";
import AdsComponent from "@site/src/components/AdsComponent";
// import BrowserWindow from "@site/src/components/BrowserWindow";
import ArrayVisualizations from "@site/src/components/DSA/arrays/ArrayVisualizations";
import BubbleSortVisualization from "@site/src/components/DSA/arrays/BubbleSortVisualization";
Expand Down Expand Up @@ -28,5 +28,5 @@ export default {
// Image,
// LiteYouTubeEmbed,
// LinearSearchVisualizer,
// AdsComponent,
AdsComponent,
};

0 comments on commit 59a7123

Please sign in to comment.