Relationship between Algorithms and Data Structures in Programming

Mary Babirye
3 min readSep 28, 2018

--

Algorithms and Data Structures

We are often given procedures on how to do different things for example chefs use a recipe with procedures to follow while preparing a meal and ingredients to use in the preparation. In computer programming, Computers use Algorithms just like chefs use recipes.

An Algorithm is a well-defined procedure that allows a computer to solve a problem.

The ingredients for the computer are called Inputs. A computer follows the given procedure while solving a problem until it comes up with the result which is also called the Output.

A computer Algorithm is clear and written in simple English or the language the programmer speaks, it is straight forward, its unambiguous and has a start and an end.

For starters, here is an example of a simple algorithm;

An algorithm to add two numbers and then display the result.

Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP

With the example above, we see that the algorithm has no data structures in it. An algorithm is a set of instructions to perform on some data. Together, an algorithm and data structure make up a program.

From the data structure point of view, the following are some important categories of algorithms −
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert an item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.

Data Structures are the programmatic way of storing data so that data can be used efficiently.

Algorithms in most cases need data structures internally to make them work as intended. I have used both data structures and algorithms while coding in Python.

An example of a program with both data structures and Algorithms:

A program that takes two lists and returns Fizz if the combined length of the lists is divisible by 3, Buzz if it is divisible by 5, Fizzbuzz if it is divisible by both 5 and 3 or the combined length of the list.

def fizzbuzz():

x=[1,2,4.5]

y=[6,7,0]

z=len(x)+len(y)

if z%3==0 and z%5==0:

print("FizzBuzz")

elif z%3==0:

print("Fizz")

elif z%5==0:

print("Buzz")

else:

print(z)

fizzbuzz()

Examples of Data Structures

An associative array (also called dictionary or map) is a more flexible variation on an array, in which name-value pairs can be added and deleted freely. A hash table is a common implementation of an associative array.

  • A record (also called tuple or struct) is an aggregate data structure. A record is a value that contains other values, typically in fixed number and sequence and typically indexed by names. The elements of records are usually called fields or members.
  • A union is a data structure that specifies which of a number of permitted primitive types may be stored in its instances, e.g. float or long integer. Contrast with a record, which could be defined to contain a float and an integer; whereas in a union, there is only one value at a time. Enough space is allocated to contain the widest member datatype.
  • A set is an abstract data structure that can store specific values, in no particular order and with no duplicate values.
  • A graph and a tree are linked abstract data structures composed of nodes. Each node contains a value and one or more pointers to other nodes arranged in a hierarchy. Graphs can be used to represent networks, while variants of trees can be used for sorting and searching, having their nodes arranged in some relative order based on their values.
  • A class is a data structure that contains data fields, like a record, as well as various methods which operate on the contents of the record. In the context of object-oriented programming, records are known as plain old data structures to distinguish them from classes.

Both Algorithms and Data Structures are fundamental in Computer Programming. Without one, the other could be useless.

--

--