DEV Community

Cover image for Introduction to Data Structures and Algorithms
Millicent Kinoti
Millicent Kinoti

Posted on

Introduction to Data Structures and Algorithms

Data Structure

A data structure can be defined as a specialized method of organizing and storing data so that execution of operations on data can be more efficient. Data can be organized in many ways such as lists or dictionaries in Python.

Data structures can be of two types: primitive and non primitive

Primitive data structures
Primitive data structures can simply be defined as data types. Data types are elementary. They cannot be further subdivided. They include int, char, double.

Non Primitive data structures
Non primitive data structure is a data structure which can be used to store data of more than one type.
Non primitive data structure can be subdivided into two categories:

  1. Linear data structure.
  2. Non-linear data structure

1. Linear data structure
The elements of a linear data structure are stored in a sequential manner. Types of linear data structure include array, list, stack and queues.

Array
An array is a data structure that stores elements of the same data type in contiguous and adjacent memory locations. An item stored in an array is called an element and an index indicates the position of the element within in an array. The index of an array starts from 0, therefore if an array has 10 elements, the index will start from 0 to 9.

List
A list is a data structure which stores elements in an ordered manner. It allows repetition and therefore a single piece of data can occur more than once in a list.

Stack
A stack is data structure that holds an ordered, linear sequence of items. It uses the Last In, First Out(LIFO) structure to access the elements in the stack. A real life example is that of a stack of plates. You can only access the plates from the top of the stack and can only add a plate to the top of the stack.

Queue
A queue is similar to a stack but it is open at both its ends. It uses the First In, First Out technique to access the elements. Data insertion occurs at the rear end or the tail of the queue whole deletion at the front end or the head of the queue.

2. Non Linear Data Structure
The elements are not arranged sequentially rather in a hierarchical manner. Elements cannot be traversed in a single run. Examples include trees and graphs

Trees
A tree is a hierarchical data structure that consists of nodes connected by edges. A node is a structure which contains data and pointers to its child nodes.An edge is used to connect two nodes. Every node except the root node is connected by exactly one incoming edge from another node.

Graphs
A graph is a data structure made up of nodes or vertices and edges

Algorithms

An algorithm is a set of well-defined instructions to solve a particular problem. For example, an algorithm to add two numbers can be as follows:

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

Characteristics of an algorithm

  1. Finiteness - Must terminate after a finite number of steps.
  2. Unambiguous - Should be clear and unambiguous.
  3. Independent - Should be independent of any programming code
  4. Input - It should have 0 or more well defined inputs.
  5. Output - It should have 1 or more well-defined outputs.
  6. Effectiveness - It must be possible to perform each step of the algorithm correctly and in a finite amount of time. Effectiveness is determined by:
  • Space complexity :The memory space needed by a specific algorithm to be executed.
  • Time complexity: The time required by a program to be completed.

Thanks for reading!

Top comments (1)

Collapse
 
i_am_okoyo profile image
tommyokoyo

Great content, especially for us who forgot about those. Looking forward to more amazing posts.