Azhagu-swe

  • Home
  • About
  • Experience
  • Skills
  • Projects
  • Contact

  • Tutorial
  • Blog
Intermediate
30 min read

The Foundations of Efficient Code: An Intro to Data Structures & Algorithms

Published on July 20, 2025

What are Data Structures and Algorithms?

At its heart, programming is about processing data. Data Structures are specialized formats for organizing, processing, retrieving, and storing data. Algorithms are a set of well-defined instructions or rules designed to solve a specific problem or perform a computation.

Think of it like a library:

  • Data Structures are the shelves and cataloging systems. A well-organized system (like shelving books by genre and then alphabetically) makes it easy to find and manage books. A poorly organized system (throwing books into a pile) makes it incredibly difficult.
  • Algorithms are the processes you follow. The process for finding a specific book is an algorithm. The process for checking out a book is another algorithm.

Choosing the right data structure and algorithm can have a massive impact on your application's performance and scalability. This is why DSA is a fundamental topic in computer science and a key part of technical interviews.


1. Data Structures: Organizing Your Data

Let's look at two of the most fundamental data structures.

Arrays

An Array is the simplest data structure. It's a collection of items stored at contiguous memory locations. You can think of it as a numbered list of boxes, where each box can hold one item.

Array Data Structure

  • Strengths:
    • Fast Access: Accessing an element by its index (e.g., myArray[2]) is very fast because the computer can calculate its exact memory location. This is called O(1) or constant time complexity.
  • Weaknesses:
    • Fixed Size: In many languages, arrays have a fixed size that you must define when you create them.
    • Slow Inserts/Deletes: If you want to insert or delete an element in the middle of an array, you have to shift all the subsequent elements, which can be very slow for large arrays.
java
// Declaring an array of integers in Java
int[] numbers = new int[5]; // Creates an array that can hold 5 integers

// Assigning values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

// Accessing a value
System.out.println(numbers[1]); // Prints 20

Linked Lists

A Linked List is a linear data structure where the elements are not stored at contiguous memory locations. Instead, each element (called a node) contains the data and a pointer (or a "link") to the next node in the sequence.

Linked List Data Structure

  • Strengths:
    • Dynamic Size: Linked lists can grow and shrink easily.
    • Fast Inserts/Deletes: Inserting or deleting a node is very fast. You just need to change a couple of pointers, without shifting any other elements.
  • Weaknesses:
    • Slow Access: To access an element at a specific position, you have to start from the head (the first node) and traverse the list one by one until you reach it. This is O(n) or linear time complexity.
    • More Memory: Each node needs to store a pointer to the next node, which uses extra memory compared to an array.

2. Algorithms: Solving Problems

An algorithm is just a step-by-step procedure for solving a problem. Let's consider a common problem: sorting an array of numbers.

A Simple Sorting Algorithm: Bubble Sort

Bubble Sort is a simple (though not very efficient) sorting algorithm. It 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.

Let's see how it would sort the array [5, 1, 4, 2].

  • Pass 1:
    • Compare 5 and 1. Swap. Array is now [1, 5, 4, 2]
    • Compare 5 and 4. Swap. Array is now [1, 4, 5, 2]
    • Compare 5 and 2. Swap. Array is now [1, 4, 2, 5]
  • Pass 2:
    • Compare 1 and 4. No swap.
    • Compare 4 and 2. Swap. Array is now [1, 2, 4, 5]
    • Compare 4 and 5. No swap.
  • Pass 3:
    • No swaps are made, so the algorithm knows the array is sorted.

Here's a simple implementation in JavaScript:

javascript
function bubbleSort(arr) {
    let n = arr.length;
    let swapped;
    do {
        swapped = false;
        for (let i = 0; i < n - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                // Swap the elements
                let temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
    return arr;
}

const unsortedArray = [5, 1, 4, 2, 8];
const sortedArray = bubbleSort(unsortedArray);
console.log(sortedArray); // Prints [1, 2, 4, 5, 8]

3. Big O Notation: Analyzing Efficiency

How do we measure if an algorithm is "good"? We use Big O Notation. It describes the performance or complexity of an algorithm as the input size grows.

  • O(1) - Constant Time: The algorithm takes the same amount of time, regardless of the input size. (e.g., accessing an array element by index).
  • O(n) - Linear Time: The runtime grows linearly with the input size n. (e.g., searching for an element in an unsorted array).
  • O(n²) - Quadratic Time: The runtime is proportional to the square of the input size. This is common in algorithms that involve nested loops, like our Bubble Sort example. It gets slow very quickly.
  • O(log n) - Logarithmic Time: The runtime grows logarithmically. These algorithms are extremely efficient. (e.g., Binary Search).

Understanding Big O helps you choose the right algorithm for the job and predict how your code will perform at scale.


Conclusion

You've just been introduced to the fundamental concepts of Data Structures and Algorithms. You've learned about arrays and linked lists, how an algorithm like Bubble Sort works, and how to analyze efficiency using Big O Notation.

This is a deep and critical area of computer science. Mastering DSA will not only help you pass technical interviews but will fundamentally change how you approach problem-solving and write high-quality, efficient code.

Table of Contents
  • 1. What are Data Structures and Algorithms?
  • 2. Data Structures: Organizing Your Data
  • 2.1 Arrays
  • 2.2 Linked Lists
  • 3. Algorithms: Solving Problems
  • 3.1 A Simple Sorting Algorithm: Bubble Sort
  • 4. Big O Notation: Analyzing Efficiency
  • 5. Conclusion
Back to All Tutorials
Azhagu-swe

A Full Stack Developer passionate about creating modern, scalable web applications.

Quick Links
Connect With Me

© 2025 Azhagu-swe. All rights reserved.

Crafted with ❤️ By Azhagu-swe