00:00

Data Structures and Algorithms (DSA) Introduction

Last updated:

What is Data Structure?

A data structure is a particular way of arranging data in memory together with the operations you can perform on it, such as adding, finding, removing or ordering items. The arrangement you pick decides how quickly each of those operations runs. For example, looking up the 1,000th item is instant in an array but needs a walk through 999 nodes in a linked list. Choosing a suitable data structure is therefore the first step in writing a fast program.

Classification of Data Structure

Data structures come in many forms, and each one is good at a different set of operations.

They are usually grouped into two broad categories:

  1. Linear Data Structure
  2. Non-Linear Data Structure
Classification of data structures into linear (static and dynamic) and non-linear (tree and graph)

What is Linear data structure?

In a linear data structure the elements form a single sequence: every element except the first has one element before it, and every element except the last has one element after it. You can visit all of them in one pass from start to end.

Arrays, linked lists, stacks and queues are the common linear data structures.

Linear data structures can be further described as static or dynamic, depending on whether their size can change:

  • Static Data Structure: its size is fixed when it is created and cannot change afterwards. A Java array is the classic example: new int[10] always holds exactly 10 elements. Because the elements sit next to each other in memory, any element can be read directly by its index.

  • Dynamic Data Structure: its size can grow and shrink while the program runs, so it uses only as much memory as the current data needs. A linked list is the classic example; Java's ArrayList also behaves dynamically by moving its elements to a larger array when it fills up. Stacks and queues are dynamic when they are built on a linked list or a growable array, and static when they are built on a fixed-size array.

What is Non-Linear Data Structure?

In a non-linear data structure an element can be connected to several other elements, so the data does not form a single line. The connections describe a hierarchy (one parent, many children) or a network (any element linked to any other).

This makes non-linear structures the natural choice for data such as folder hierarchies, organisation charts, road maps and social networks.

Trees and graphs are the two main non-linear data structures.

What is an Algorithm?

An algorithm is a finite, step-by-step procedure that takes some input and produces the required output. A recipe, the steps for long division and the method a map app uses to find the shortest route are all algorithms.

In programming, an algorithm is the logic you write before (or while) writing code. Two algorithms can solve the same problem with very different speed, which is why algorithms are studied together with data structures.

Specifications of Algorithms

A procedure counts as an algorithm when it has these five properties:

  1. Input - it accepts zero or more values to work on.
  2. Output - it produces at least one result.
  3. Definiteness - each step is precise and can be understood in only one way.
  4. Finiteness - it always stops after a limited number of steps, whatever the input.
  5. Effectiveness - each step is simple enough to be carried out exactly, for example by a person with pen and paper.

Example for an Algorithm

Here is a small problem and an algorithm that solves it.

Problem Statement : find the largest number in a list of numbers.

Input : a list L containing at least one integer (the numbers may be negative).

Output : the largest value in L, called max.

The idea is to remember the largest value seen so far and update it whenever a bigger value turns up.

Algorithm

  1. Step 1: Set max to the first number in L.
  2. Step 2: Take the next number x from L. If x is greater than max, set max to x.
  3. Step 3: Repeat Step 2 until every number in L has been checked.
  4. Step 4: Return max.

Starting with the first number (instead of 0) keeps the algorithm correct when every number is negative. It looks at each number once, so it takes O(n) time for a list of n numbers and O(1) extra space.