
Data Structures And Algorithms
Data Structures are structures programmed to store ordered data so that various operations can be performed on it easily. It represents the knowledge of data to be organized in memory. It should be designed and implemented in such a way that it reduces the complexity and increases the efficiency.
Types of Data Structures
Data structures are grouped into two; Primitive and abstract data structures. Primitive Data Structures which are built in data structures are used to store small amounts of data, these include String, Integer, Float, Boolean, Char etc. Then we also have some complex Data Structures, which are used to store large and connected data. Some examples of Abstract Data Structures are :
- Linked List
- Tree
- Graph
- Stack, Queue etc.
- Arrays
All these data structures allow us to perform different operations on data. We select these data structures based on which type of operation is required.
Arrays
An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.
Arrays are used in the following ways
- For online quiz games — generating random numbers.
- It is also used in lottery
- You use arrays all of the time in programming. Whenever you need to keep track of an ordered list of items, you’ll use an array: a list of songs, a list of each keystroke a user clicks. Even in the JSON data format, you’ll often use an array to hold a list of objects.
Stacks
We are all familiar with the famous Undo option, which is present in almost every application. Ever wondered how it works? The idea: you store the previous states of your work (which are limited to a specific number) in the memory in such an order that the last one appears first. That is where the Stack comes in handy.
A real-life example of Stack could be a pile of books placed in a vertical order. In order to get the book that’s somewhere in the middle, you will need to remove all the books placed on top of it. This is how the LIFO (Last In First Out) method works.
Here’s an image of a stack containing three data elements (1, 2 and 3), where 3 is at the top and will be removed first:
Queues
The significant difference between Stack and Queue is that instead of using the LIFO method, Queue implements the FIFO method, which is short for First in First Out.
A perfect real-life example of Queue: a line of people waiting at the bank. If a new person comes, they will join the line from the end, not from the start — and the person standing at the front will be the first to be served and hence leave the line.
Here’s an image of Queue containing four data elements (1, 2, 3 and 4), where 1 is at the top and will be removed first:
Linked List
A linked list is another important linear data structure which might look similar to arrays at first but differs in memory allocation, internal structure and how basic operations of insertion and deletion are carried out.
A linked list is like a chain of nodes, where each node contains information like data and a pointer to the succeeding node in the chain. There’s a head pointer, which points to the first element of the linked list, and if the list is empty then it simply points to null or nothing.
Linked lists are used to implement file systems, hash tables, and adjacency lists.
Here’s a visual representation of the internal structure of a linked list:
Following are the types of linked lists:
- Singly Linked List (Unidirectional)
- Doubly Linked List (Bi-directional)
Basic operations of Linked List:
- InsertAtEnd — Inserts given element at the end of the linked list
- InsertAtHead — Inserts given element at the start/head of the linked list
- Delete — Deletes given element from the linked list
- DeleteAtHead — Deletes first element of the linked list
- Search — Returns the given element from a linked list
- isEmpty — Returns true if the linked list is empty
Algorithm
An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task. The algorithm is not the complete code or program, it is just the core logic(solution) of a problem, which can be expressed either as an informal high-level description as pseudo code or using a flowchart.
Every Algorithm must satisfy the following properties:
- Input- There should be 0 or more inputs supplied externally to the algorithm.
- Output- There should be at least 1 output obtained.
- Definiteness- Every step of the algorithm should be clear and well defined.
- Finiteness- The algorithm should have a finite number of steps.
- Correctness- Every step of the algorithm must generate a correct output.
An algorithm is said to be efficient and fast if it takes less time to execute and consumes less memory space. The performance of an algorithm is measured on the basis of the following properties :
- Time Complexity
- Space Complexity
Algorithms are widely used throughout all areas of IT (information technology). A search engine algorithm, for example, takes search strings of keywords and operators as input, searches its associated database for relevant web pages, and returns results.
An encryption algorithm transforms data according to specified actions to protect it. A secret key algorithm uses the same key to encrypt and decrypt data. As long as the algorithm is sufficiently sophisticated, no one lacking the key can decrypt the data.
