Module 1: Arrays (The Train Cars)
📚 Module 1: Arrays
Course ID: DSA-103
Subject: The Memory Train
An Array is the most basic way to store a list of items. In memory, it is a single, continuous block of space.
🏗️ Step 1: Contiguous Memory (The “Train Cars”)
🧩 The Analogy: The Train
Imagine a train with 5 cars.
- You know exactly where the 1st car is.
- To find the 4th car, you don’t have to walk through the train. You just calculate:
Start + (3 * CarSize). - Result: You can jump to any seat in the train instantly (O(1) Access).
🏗️ Step 2: The Insertion Problem (The “Shift”)
Arrays are great for reading, but they are terrible for adding data in the middle.
🧩 The Analogy: The Crowded Line
Imagine 10 people in a strict line. A new person wants to cut into the 2nd spot.
- Everyone from the 2nd person to the 10th person must take a step back to make room.
- In Code: If you insert at the beginning of an array, you have to move every single other item (O(n) Time).
🏗️ Step 3: Static vs Dynamic
- Static Array: The size is fixed. If the train has 5 cars, it ALWAYS has 5 cars.
- Dynamic Array (Python List / Java ArrayList): When the array is full, it automatically builds a New, Bigger Array and copies everything over.
🥅 Module 1 Review
- Access: O(1) - Jumping directly to an index.
- Search: O(n) - Looking at every item until you find a match.
- Insertion: O(n) - Because you have to shift other items.
- Deletion: O(n) - Because you have to fill the gap.
:::tip Slow Learner Note If you find yourself constantly adding items to the START of a list, an Array is the wrong choice. Use a Linked List (Module 2) instead! :::