Data Structures and Algorithms (DSA)

0
(0)

1. What Is a Data Structure?

Definition

A data structure is a systematic way of organizing, storing, and managing data in computer memory so that the data can be accessed and modified efficiently.

In simple terms:

A data structure determines how data is arranged and how we can work with that data.

For example, suppose we have five student marks:

75, 82, 91, 68, 88

We could store them in an array:

Index:    0    1    2    3    4
          ↓    ↓    ↓    ↓    ↓
Marks:   [75] [82] [91] [68] [88]

The structure tells the computer how the individual values are organized.

Why do we need data structures?

Consider a university with 100,000 students.

We may need to:

  • store student information
  • search for a particular student
  • insert new students
  • remove students
  • sort students
  • update records
  • retrieve information quickly

Different data structures provide different efficiencies.

For example:

Data structureParticularly useful for
ArrayFast indexed access
Linked listFrequent insertion/deletion
StackLast-in-first-out processing
QueueFirst-in-first-out processing
Hash tableFast key-based lookup
TreeHierarchical data
GraphNetworks and relationships

Data structure and memory

Conceptually:

                 COMPUTER MEMORY
                       │
        ┌──────────────┴──────────────┐
        │                             │
     Data                        Organization
  10, 20, 30...                 Array/List/etc.
        │                             │
        └──────────────┬──────────────┘
                       ↓
              Efficient operations

A data structure is therefore not merely “data.” It includes the organization of that data and the operations used to manipulate it.

Example in C++

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {75, 82, 91, 68, 88};

    cout << marks[2];

    return 0;
}

Output:

91

Here:

  • 75, 82, 91, 68, 88 → data
  • marks → array
  • array → data structure
  • marks[2] → accessing an element

2. What Is an Algorithm?

Definition

An algorithm is a finite, ordered sequence of precise instructions used to solve a problem or perform a computation.

In simple terms:

A data structure organizes the data; an algorithm tells us what to do with the data.

For example, suppose we want to find the largest number:

10, 25, 7, 40, 18

An algorithm could be:

1. Assume the first number is the largest.
2. Compare it with the second number.
3. If the second is larger, make it the largest.
4. Continue comparing with every remaining number.
5. Return the largest number.

Result:

Largest = 40

Algorithm flow

             START
               │
               ▼
       Read the numbers
               │
               ▼
    Assume first = largest
               │
               ▼
       Compare next number
               │
          ┌────┴────┐
          │         │
       Larger?      No
          │         │
         Yes        │
          │         │
          ▼         │
    Update largest  │
          │         │
          └────┬────┘
               ▼
        More numbers?
          │       │
         Yes      No
          │       │
          └───────┤
                  ▼
          Output largest
                  │
                  ▼
                 END

Example in C++

int numbers[] = {10, 25, 7, 40, 18};

int largest = numbers[0];

for (int i = 1; i < 5; i++) {
    if (numbers[i] > largest) {
        largest = numbers[i];
    }
}

cout << largest;

Output:

40

The array is the data structure.

The loop and comparison logic implement the algorithm.

3. Characteristics of a Good Algorithm

Definition

The characteristics of a good algorithm are the properties that make an algorithm correct, understandable, efficient, and practical.

A good algorithm should satisfy several important requirements.

3.1 Input

Definition

Input is the data supplied to an algorithm.

An algorithm may have:

  • zero inputs
  • one input
  • multiple inputs

Example:

Input:
A = 10
B = 20

Algorithm:

Add A and B

3.2 Output

Definition

Output is the result produced by an algorithm.

Example:

Input:
10, 20

Process:
10 + 20

Output:
30

3.3 Definiteness

Definition

Definiteness means every step of the algorithm must be clear, precise, and unambiguous.

Bad instruction:

1. Process the numbers somehow.

Good instruction:

1. Compare A and B.
2. If A > B, output A.
3. Otherwise, output B.

Every operation should have a clear meaning.

3.4 Finiteness

Definition

Finiteness means an algorithm must eventually terminate after a finite number of steps.

Bad:

1. Print "Hello".
2. Repeat forever.

Good:

1. Set i = 1.
2. Print i.
3. Increase i by 1.
4. Repeat while i <= 10.
5. Stop.

3.5 Effectiveness

Definition

Effectiveness means each operation should be sufficiently basic that it can actually be executed by a computer or a person following the algorithm.

For example:

Read A
Read B
Add A and B
Display result

These are executable operations.

3.6 Correctness

Definition

Correctness means the algorithm produces the right result for every valid input.

For example, an algorithm designed to calculate:

Area = length × width

must correctly calculate the area for all valid lengths and widths.

3.7 Efficiency

Definition

Efficiency measures how effectively an algorithm uses computational resources.

The two major resources are:

Time
  ↓
How long does it take?

Space
  ↓
How much memory does it use?

This leads to:

  • Time complexity
  • Space complexity

For example:

Linear search      → O(n)
Binary search      → O(log n)

For large datasets, the difference can be substantial.

3.8 Generality

Definition

Generality means an algorithm should solve a class of problems rather than only one specific example.

For example, this is not very general:

Add 10 + 20

A general algorithm is:

Read A
Read B
Result = A + B
Display Result

It can solve:

10 + 20
50 + 80
125 + 375
...

Summary

             GOOD ALGORITHM
                   │
     ┌─────────────┼─────────────┐
     ↓             ↓             ↓
   Input         Output       Definiteness
     │             │             │
     └─────────────┼─────────────┘
                   ↓
              Correctness
                   ↓
              Finiteness
                   ↓
              Effectiveness
                   ↓
               Efficiency
                   ↓
               Generality

4. Algorithm vs. Program

Definition: Algorithm

An algorithm is a logical, language-independent procedure for solving a problem.

Definition: Program

A program is a set of instructions written in a programming language that a computer can execute.

Therefore:

An algorithm is the solution strategy; a program is an implementation of that strategy.

Example problem

Problem: Find the sum of two numbers.

Algorithm

1. Start
2. Read A
3. Read B
4. Calculate S = A + B
5. Display S
6. Stop

Program

#include <iostream>
using namespace std;

int main() {
    int A, B;

    cin >> A >> B;

    int S = A + B;

    cout << S;

    return 0;
}

Relationship

             PROBLEM
                │
                ▼
             ALGORITHM
       "How should we solve it?"
                │
                ▼
       PROGRAMMING LANGUAGE
                │
                ▼
             PROGRAM
       "Executable implementation"
                │
                ▼
             COMPUTER

Key differences

AlgorithmProgram
Logical solutionImplementation
Language-independentLanguage-dependent
Describes what/how to solveGives executable instructions
Can be written in pseudocodeWritten in C++, Java, Python, etc.
May be expressed using diagramsMust follow programming-language syntax

5. Data Types vs. Data Structures

These two concepts are related but not the same.

5.1 Data Type

Definition

A data type specifies what kind of value a variable can store and what operations can be performed on that value.

Examples:

int
float
double
char
boolean

C++ example:

int age = 20;
float temperature = 25.5;
char grade = 'A';
bool passed = true;

Conceptually:

Variable
   │
   ├── Data type
   │      │
   │      └── int
   │
   └── Value
          │
          └── 20

5.2 Data Structure

Definition

A data structure organizes one or more pieces of data in memory so they can be efficiently stored and manipulated.

Example:

int marks[5] = {70, 80, 90, 75, 85};

Here:

int
 ↓
Data type

int[]
 ↓
Array data structure

Comparison

Data typeData structure
Defines type of dataDefines organization of data
Usually represents individual valuesUsually manages collections/relationships
Examples: int, char, floatArray, stack, queue, tree, graph
Focuses on values and operationsFocuses on organization and operations
Example: int xExample: int arr[10]

Important relationship

A data structure can be built using data types.

              DATA STRUCTURE
                    │
                    ▼
                 ARRAY
                    │
          ┌─────────┼─────────┐
          ↓         ↓         ↓
        int       int       int
         10        20        30

Think of it this way:

Data type = what the data is.
Data structure = how multiple pieces of data are organized.

6. Abstract Data Types (ADTs)

Definition

An Abstract Data Type (ADT) is a logical description of a data structure that specifies:

  1. what data is stored, and
  2. what operations can be performed

without specifying exactly how those operations are implemented.

The key idea is:

ADT describes WHAT; implementation describes HOW.

Example: Stack ADT

A stack follows:

LIFO — Last In, First Out

Imagine a stack of plates:

       ┌───────┐
       │ Plate │ ← Last added
       ├───────┤
       │ Plate │
       ├───────┤
       │ Plate │ ← First added
       └───────┘

The Stack ADT might define:

push(x)    → add x
pop()      → remove top item
peek()     → inspect top item
isEmpty()  → determine whether stack is empty

But the ADT does not require a particular implementation.

It could be implemented using:

             STACK ADT
                 │
        ┌────────┴────────┐
        ↓                 ↓
      Array           Linked List
        │                 │
        └────────┬────────┘
                 ↓
          Same logical behavior

Example

#include <stack>
using namespace std;

stack<int> s;

s.push(10);
s.push(20);
s.push(30);

cout << s.top();

Output:

30

The user cares about the stack operations rather than the internal implementation.

ADT layers

┌─────────────────────────────┐
│        User / Program       │
└──────────────┬──────────────┘
               │
               ▼
┌─────────────────────────────┐
│            ADT              │
│  push, pop, peek, isEmpty   │
└──────────────┬──────────────┘
               │
               ▼
┌─────────────────────────────┐
│       Implementation        │
│     Array / Linked List     │
└──────────────┬──────────────┘
               │
               ▼
┌─────────────────────────────┐
│          Memory             │
└─────────────────────────────┘

Common ADTs

ADTTypical operations
ListInsert, delete, search
StackPush, pop, peek
QueueEnqueue, dequeue
DequeInsert/delete at both ends
SetAdd, remove, membership
Map/DictionaryInsert, lookup, delete

7. Linear vs. Non-Linear Data Structures

7.1 Linear Data Structure

Definition

A linear data structure organizes elements in a sequential order, where each element generally has a predecessor and/or successor.

Conceptually:

[10] → [20] → [30] → [40] → [50]

The elements form a linear sequence.

Examples

  • Array
  • Linked list
  • Stack
  • Queue
  • Deque

Array

Index:  0    1    2    3
        ↓    ↓    ↓    ↓
       [10] [20] [30] [40]

Linked list

┌────┐    ┌────┐    ┌────┐
│ 10 │───►│ 20 │───►│ 30 │───► NULL
└────┘    └────┘    └────┘

Stack

       TOP
        ↓
      ┌────┐
      │ 30 │
      ├────┤
      │ 20 │
      ├────┤
      │ 10 │
      └────┘

Queue

FRONT                              REAR
  ↓                                  ↓
[10] → [20] → [30] → [40]

Characteristics

Linear structures generally:

  • arrange elements sequentially
  • have a logical first-to-last order
  • are relatively straightforward to traverse
  • are useful when data has a sequential relationship

7.2 Non-Linear Data Structure

Definition

A non-linear data structure organizes data in a way where elements are not arranged in one simple sequential sequence.

Instead, one element may connect to multiple other elements.

Examples include:

  • Trees
  • Graphs
  • Heaps
  • Tries

Tree

                 A
              /     \
             B       C
           /   \       \
          D     E       F

Notice that A connects to both B and C.

Graph

       A ───── B
       │ \     │
       │  \    │
       │   \   │
       C ───── D

A graph can contain many relationships between nodes.

Linear vs non-linear

LINEAR

A → B → C → D → E


NON-LINEAR

        A
       / \
      B   C
     / \   \
    D   E   F

Comparison

LinearNon-linear
Sequential organizationHierarchical/network organization
One logical sequenceMultiple relationships
Usually one-to-one progressionOften one-to-many or many-to-many
Array, list, stack, queueTree, graph, heap
Traversal is generally sequentialMultiple traversal strategies may exist

8. Static vs. Dynamic Data Structures

This classification concerns whether the size/storage organization can change during program execution.

8.1 Static Data Structure

Definition

A static data structure has a size that is fixed when it is created and generally cannot automatically grow or shrink during execution.

The classic example is a fixed-size array.

int numbers[5];

The array has five positions:

[  ][  ][  ][  ][  ]

You cannot simply make it:

[  ][  ][  ][  ][  ][  ][  ]

without creating another structure or using a different mechanism.

Diagram

Creation
   │
   ▼
┌────┬────┬────┬────┬────┐
│    │    │    │    │    │
└────┴────┴────┴────┴────┘
        fixed size

Advantages

  • Simple
  • Fast indexing
  • Predictable memory requirements
  • Low management overhead

Disadvantages

  • Fixed capacity
  • May waste memory
  • Difficult to accommodate unknown amounts of data

8.2 Dynamic Data Structure

Definition

A dynamic data structure can change its size or memory allocation during program execution.

A linked list is a common example.

Initially:

[10] → [20] → NULL


After inserting 30:

[10] → [20] → [30] → NULL

The structure grows as required.

Diagram

          Dynamic growth

[10] → [20]
          │
          ▼
[10] → [20] → [30]
                  │
                  ▼
[10] → [20] → [30] → [40]

C++ example

#include <iostream>
using namespace std;

int main() {
    int* p = new int[5];

    p[0] = 10;
    p[1] = 20;
    p[2] = 30;

    delete[] p;

    return 0;
}

Modern C++ generally favors containers such as vector for dynamically sized arrays:

#include <vector>
using namespace std;

vector<int> numbers;

numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);

The vector can grow as elements are added.

Comparison

StaticDynamic
Fixed sizeCan change size
Allocation generally determined beforehandAllocation can occur during execution
Simple memory managementMore flexible memory management
Example: fixed arrayExample: linked list, dynamic array/vector
May waste allocated spaceCan adapt to changing data

Important: “Static” and “dynamic” can refer to different aspects of implementation. For example, a dynamic array has a changing logical size, while its underlying allocation may be resized in chunks.

9. Homogeneous vs. Non-Homogeneous Data Structures

9.1 Homogeneous Data Structure

Definition

A homogeneous data structure stores elements that are all of the same data type.

Example:

10   20   30   40   50

All elements are integers.

Array example

int marks[5] = {
    75,
    82,
    91,
    68,
    88
};

Every element is an int.

Diagram:

              int
               │
      ┌────────┼────────┐
      ↓        ↓        ↓
     [75]     [82]     [91] ...

Other examples

float temperatures[4];

char letters[5];

double prices[10];

Each collection contains elements of one type.

Advantages

  • Simple organization
  • Efficient memory representation
  • Easy processing
  • Convenient iteration
  • Often supports efficient indexed access

9.2 Non-Homogeneous Data Structure

Definition

A non-homogeneous data structure can contain different types of related data within one logical structure.

A student record is a good example.

A student may have:

ID       → integer
Name     → string
Age      → integer
GPA      → floating-point number
Passed   → boolean

These are different data types.

C++ example using struct

#include <string>
using namespace std;

struct Student {
    int id;
    string name;
    int age;
    double gpa;
    bool passed;
};

We can create:

Student s1 = {
    101,
    "Moses",
    21,
    3.75,
    true
};

Conceptually:

Student
│
├── id      → 101
├── name    → "Moses"
├── age     → 21
├── gpa     → 3.75
└── passed  → true

Homogeneous vs non-homogeneous

HOMOGENEOUS

Array
│
├── int
├── int
├── int
├── int
└── int


NON-HOMOGENEOUS

Student
│
├── int
├── string
├── int
├── double
└── bool

Comparison

HomogeneousNon-homogeneous
Same data typeDifferent data types
Example: int arr[5]Example: struct Student
Good for collections of similar valuesGood for records containing different attributes
Elements generally have the same representationFields can have different representations

10. Putting All the Classifications Together

These classifications describe different properties of data structures. They should not be treated as mutually exclusive categories.

For example, an array can be:

Array
│
├── Linear
├── Homogeneous
└── Static or dynamic depending on implementation

A linked list can be:

Linked List
│
├── Linear
├── Usually dynamic
└── Can be homogeneous

A tree can be:

Tree
│
├── Non-linear
└── Can be static or dynamic depending on implementation

A struct can be:

Student record
│
└── Non-homogeneous

Big-picture classification

                    DATA STRUCTURES
                          │
          ┌───────────────┼────────────────┐
          │               │                │
          ▼               ▼                ▼
       LINEAR         NON-LINEAR       CLASSIFICATION
          │               │                │
    ┌─────┼─────┐      ┌──┴──┐       ┌────┴────┐
    │     │     │      │     │       │         │
  Array  List  Stack   Tree  Graph  Static   Dynamic
    │
    ▼
Queue

Another useful view:

                 DATA STRUCTURE
                       │
       ┌───────────────┼────────────────┐
       │               │                │
       ▼               ▼                ▼
    Structure       Storage          Element type
       │               │                │
       ▼               ▼                ▼
 Linear/            Static/       Homogeneous/
 Non-linear         Dynamic        Non-homogeneous

These dimensions answer different questions:

QuestionClassification
How are elements connected?Linear / Non-linear
Can the structure change size?Static / Dynamic
Are elements the same type?Homogeneous / Non-homogeneous
What operations are exposed?ADT
How do we solve a problem using it?Algorithm

11. Data Structure + Algorithm = Efficient Problem Solving

A fundamental DSA principle is:

The choice of data structure affects the efficiency of algorithms.

Suppose we need to search for a value.

With an unsorted array:

[15] [42] [7] [91] [23]
 ↓
Search sequentially

We may need to examine many elements.

With a suitable search structure:

          50
        /    \
      25      75
     /  \    /  \
   10   30  60   90

The search strategy can exploit the structure.

Therefore:

             PROBLEM
                │
                ▼
       Choose Data Structure
                │
                ▼
       Design Algorithm
                │
                ▼
       Analyze Complexity
          ┌─────┴─────┐
          ▼           ▼
       Time          Space
      O(... )        O(...)
          │           │
          └─────┬─────┘
                ▼
        Efficient Solution

12. Essential Concepts to Remember

Data structure

A method of organizing and storing data so that it can be accessed and manipulated efficiently.

Algorithm

A finite sequence of precise steps for solving a problem or performing a computation.

Good algorithm

Should have:

Input
Output
Definiteness
Finiteness
Effectiveness
Correctness
Efficiency
Generality

Algorithm vs program

Algorithm = solution procedure
Program   = coded implementation

Data type vs data structure

Data type      → what kind of value?
Data structure → how is data organized?

ADT

ADT = WHAT operations are available
Implementation = HOW operations are performed

Linear

A → B → C → D

Sequential organization.

Non-linear

      A
     / \
    B   C

Hierarchical or network organization.

Static

Fixed capacity

Dynamic

Can change during execution

Homogeneous

int → int → int → int

Same type.

Non-homogeneous

int → string → double → bool

Different types.

The DSA Mental Model

The most important relationship to internalize is:

                     COMPUTER PROBLEM
                           │
                           ▼
                    ┌──────────────┐
                    │     DATA     │
                    └──────┬───────┘
                           │
                           ▼
                  Choose a suitable
                  DATA STRUCTURE
                           │
                           ▼
                    Design an
                    ALGORITHM
                           │
                           ▼
                  IMPLEMENT in
               Python / C++ / Java
                           │
                           ▼
                 ANALYZE COMPLEXITY
                    ┌──────┴──────┐
                    ▼             ▼
                 TIME           SPACE
                O(... )         O(...)
                    │             │
                    └──────┬──────┘
                           ▼
                  EFFICIENT PROGRAM

The key distinction is: a data structure organizes information, while an algorithm processes that information.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *