DEV Community

Nemwel Boniface
Nemwel Boniface

Posted on

Data structures and algorithms

Image of computer screen with code

Introduction to Data structures and algorithms

Data structures and algorithms. Sounds like an enigma, right? Luckily for you, my reader, today, I aim to debunk what these two mean by giving you a beginner-friendly overview of each and end it all with a simple example.

So what is a data structure?

Image showing data structure

according to Wikipedia,

a data structure is a data organization, management, and storage format that enables efficient access and modification.

Oh! still complex? Okay, imagine you have a warehouse and you have over 100 different products to store them. You would like to store them in an organized manner such that when a client wants a specific product, you know exactly where to retrieve it from. Remember you must never keep your client waiting :).

We have various types of data structures available to use for use. I will mention but a few of the most common data structures:

  1. Arrays - an array is a data structure consisting of a collection of elements. We can perform several operations on Arrays such as:

    • traversing - going though the elements in the array and doing something with them
    • Searching - You can search for an element in an array
    • Updating - to update a value of an existing array element
  2. Linked Lists - a linked list is a sequential structure that consists of a sequence of items in linear order which are linked to each other. In this data structure you have to access data sequentially and random access is not possible. Linked lists have the following operations:

    • Searching - Find the first element that matches a condition in the given linked list by a simple linear search.
    • Inserting - Insert a key to the linked list
    • Deleting - Removes an element from a given linked list.
  3. Stacks - A stack is a LIFO (Last In First Out the element placed at last can be accessed at first) structure it is named as “stack” because it resembles a real-world stack a stack of plates. Operations that can be operated on a stack include :

    • Push - Insert an element on to the top of the stack.
    • Pop - remove the topmost element and return it.
  4. Queues - a queue is a FIFO (First In First Out) structure resembles a real-world queue. The element that came in first will be accessed first. Operations that can be done on a queue include:

    • Enqueue - Insert an element to the end of the queue.
    • Dequeue - Delete the element from the beginning of the queue.

We also have other data structures such as Hash tables, trees, Heaps, and graphs.

Remember, when you see data structures you have to remember the concept of storing maintaining and retrieving that stored data.

Okay, what about an algorithm?

Image showing algorithms

An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task.

Simple right?

Because we are computer wizards, let us write a simple algorithm for a program that adds two numbers:

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2
Step 5: Display sum
Step 6: Stop

We have two types of algorithms :

  1. Search algorithms - When wanting to get the client's order from the warehouse you'd want tohave a way to search through all the goods in the warehouse. Search algorithms are categorized into two: Linear and Binary searches.

  2. Sort algorithms - searching for an element might not be enough. You may want a way to sort through the data and be able to find that specific good that was ordered.

An algorithm like everything else, has several rules that are checked to determine its effectiveness

  1. Time - your algorithms should not take a lot of time to execute

  2. Memory - your algorithm should use the least amount of memory as possible

  3. Accuracy - your algorithm should be as accurate as possible even when given multiple scenarios

That is it for today's class. I hope this was useful information for you. See you in my next article.

Top comments (3)

Collapse
 
janicera2880 profile image
Janice Alecha

Currently, learning the topic as we are almost closing to phase 1 of my SE bootcamp. Thank you for this!

Collapse
 
nemwelboniface profile image
Nemwel Boniface

Hello Janice! I am glad my article was of help to you. I will write a second article this week diving deeper on DSA's. Stay tuned.

Collapse
 
janicera2880 profile image
Janice Alecha

Will be looking forward...