## DEV Community

steve kimoi

Posted on • Updated on

# 101: INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

We have all heard about data structures and algorithms, and how essential they are to any programmer or software engineer/developer. But do we really have a good understanding of what data structures and algorithms are?

Let’s dive deep into it:

DEFINITIONS:
What is a data structure: it is a named location that can be used to store and organize data.

What is an algorithm: they are steps that can be used to solve a particular problem.

Having a good understanding of the above allows you (a programmer) to write efficient and optimized computer programs. This leads us to our next question, what is a computer program?

A computer program is a sequence of instructions in a programming language that a computer can execute or interpret. A computer program may need to store data, retrieve data and perform computations on the data.

Now that we have a basic understanding of data structures and algorithms and how they’re related to a computer program, let’s see the various types of data structures:

TYPES OF DATA STRUCTURES:
There are several basic and advanced types of data structures, all designed to arrange data to suit a specific purpose. There are 8 commonly used data structures every programmer must know:

1. Arrays:

An array is a structure of fixed size that can hold items of the same data type e.g integers, strings, floating point numbers e.t.c. The type of elements that can be stored in the form of array is determined by the programming language. Random access of the data is possible since they are indexed.

A sequential structure that consists of a sequence of items in linear order that are linked to each other. Random access is not possible since you have to access the data sequentially. Elements in a linked lists are known as nodes. Each node contains a key and a pointer to its successor node.

3. Stacks:
A stack is a LIFO (Last in first out) structure which can be found in many programming languages. This structure is named as “stack” since it resembles a real-world stack – e.g a stack of plates. It is named this way since the last element to go in comes out first.

4. Queues:

This behaves in the opposite manner as compared to stack. It uses the FIFO (First in first out) principle, where the element to go in first comes out first. It is named “queue” since it represents a real world queue.

5. Hash tables:

A data structure that stores values which have keys associated with each one of them. It supports lookup efficiently if we know a key associated with the value.

6. Trees:
This is a hierarchical structure where the data is organized hierarchically and are linked together. It is a bit different from a linked list since a linked list has items linked together.

7. Heaps:

A special case of a binary tree where the parent nodes are compared to their children with their values and are arranged accordingly.

8. Graphs:
This is a fine set of vertices or nodes and a set of edges connecting these vertices. Order of the graph is the number of vertices in the graph and the size of the graph is the number of edges in the graph.

Let us now look on the importance of data structures.

WHY ARE DATA STRUCTURES IMPORTANT:
Data types such as integers and floating-point values that are available in most programming languages are not sufficient to capture the logical intent for data processing and use. Data structures bring together the data elements in a logical way and facilitate their effective use, persistent and sharing of data. They provide a formal model that describes the way the data elements are organized.

Basically, data structures are the building blocks for more sophisticated applications.

We now understand what are data structures and the various types, we can now have a look into algorithms.

ALGORITHIMS:
We started off by defining what algorithms are at the start of this article, now let’s have a look into how algorithms work, their characteristics, the various types and their importance in computer programming.

How do algorithms work:
As I had mentioned earlier, algorithms are basically steps that are used to solve a particular problem. They can be expressed as natural languages(e.g English), pseudocodes, programming languages, flowcharts and control tables. Programming languages are the ones commonly used for expressing algorithms used by a computer.

It usually takes an input, and passes it through a set of instructions. The input acts as the initial data. It then gives out an output, which is usually the last step.

What are the characteristics of algorithms:
In order for a program to become an algorithm, it usually has some type of characteristics:

1. Finiteness: it should have a finite number of steps, and should end after a finite time.
2. Well defined inputs: the inputs of an algorithm must be well defined.
3. Well defined outputs: the algorithm must clearly define what output will be yielded and it should be well defined as well.
4. Clear and unambiguous: each of the steps should be clear in all aspects and must lead to one meaning.
5. Language independent: it must be plain instructions that can be implemented in any language, and the output will be the same as expected.

Types of algorithms:
There are several types of algorithms available, some important ones are:

1. Brute force algorithm: straightforward methods of solving a problem that rely on sheer computer power.
2. Recursive algorithm: Based on recursion. A problem is broken into smaller sub-parts and called the same function over and over.
3. Backtracking algorithms: It builds the solution by searching among all possible solutions. In short it builds a solution incrementally, one piece at a time.
4. Searching algorithm: they are algorithms that are used to search for elements or groups of elements from a particular data structure.
5. Sorting algorithms: sorting is arranging a group of data in a particular manner according to the requirement. Algorithms that help performing this kind of task are called sorting algorithms.
6. Hashing algorithms: they work similarly to searching algorithms but the difference is, they contain index with a key ID. A key is assigned to a specific data.
7. Divide and conquer algorithm: involves breaking a problem into sub-problems, and merges the solutions together to get the final solution.
8. Greedy algorithm: the solution is built part by part. The solution of the next part is built based on the immediate benefit of the next part.
9. Dynamic programming algorithm: Uses the concept of using the already found solution to avoid repetitive calculation of the same part of the problem.
10. Randomized algorithms: we use a random number so it gives immediate benefit. The random number helps in deciding the expected outcome.

Importance of algorithms:
Algorithms are very key and important to computer programming. The best-chosen algorithm makes sure the computer will do the given task in the best possible manner.

Some of the importance of algorithms are:

1. To improve the efficiency of a computer program: the best algorithm enables the computer produce the very accurate results.
2. Proper utilization of resources: an algorithm determines the amount of memory used by the program and also amount of processing power determined by the program. A good algorithm will use less recourses.

References: