Data-structures and Algorithms using Python: Programming Series 101
Last Updated on January 6, 2023 by Editorial Team
Author(s): anuragbisht
Programming
For folks who have started learning to program or willing to learn and pursue their career in fields that require programming knowledge, this post will help you guys understand the fundamentals of data structures and algorithms.
Data-structures and algorithms are one of the most fundamental aspects of programming taught in any computer science lecture. You can learn any programming language, but to be a good programmer, you have to develop a grip over data-structures and algorithms. So what is a data structure, what is an algorithm, why should we learnΒ it?
A data structure, as the name suggests, is the building block of any programming /coding you do to design any approach to solve any problem using a programming language. That approach is also known as an algorithm in computer science terminology.
For e.g., If you are given a list of students in sorted order and asked to find whether a given student name is present in that class using programming within the least amount of time and computational resources.
To solve this programming problem, you will start thinking about the approach, which is referred to as an algorithm. Once you have an approach to solve the problem(whether or not in the least amount of time and computational resources), you will also start thinking about how to store the input in an optimal way, which is referred to as data structure.
To our problem, we can use a searching algorithm (say binary search as the input is sorted) and any suitable data structure like a list in python.
Different programming languages have different ways to implement data structures forΒ use.
The broad categories of data structures are asΒ follows:
Types of data-structure:
- PrimitiveΒ :
These are the most fundamental data-structures present at the machineΒ level.
E.g., integer, float, character, pointer,Β etc.
2. Non-primitive:
These are data-structures that are derived from primitive data-structures.
2.1 Array:
An array consists of a collection of homogenous elements in the continuous allocation ofΒ memory.
2.2. Files:
This is a collection of records primarily used to manage a large number ofΒ records.
2.3. Lists:
This data-structure consists of dynamic memory allocation of primitive data-structure.
2.3.1. LinearΒ lists:
The elements are stored in sequential order.
Stack: This data-structure stores and retrieves the elements in the last in first out( LIFO)Β format.
Queue: This data-structure stored and retrieves the elements in the first in, first out formatΒ (FIFO).
2.3.2. Non-linear lists:
The elements are not stored in sequential order.
Trees: Data-structure is stored in tree-based format with nodes and vertices connected in tree-based structure. It is a special case of aΒ graph.
Graph: Data-structure is stored with nodes and vertices where nodes with data are connected to vertices.
Stack:
To design a stack data-structure, we will use a fundamental python data-structure list.
The different functions in the stackΒ are:
- push(): To store elements in theΒ stack.
- pop(): To retrieve elements from theΒ stack.
- is_empty(): To check if the stack isΒ empty.
- size(): To get the size of theΒ stack.
- top (): To return the top element of theΒ stack.
Time and Space Complexity:
The time and space complexity of the algorithm is an important aspect to be calculated while coding any algorithm.
Big Oh notation (denoted by O): It measures the longest time/case taken by the algorithm to complete execution. While calculating the time/ space complexity of the algorithm, we look at the worst-case possible or the worst time/case required by the algorithm to complete any execution.
To learn more, you can visit thisΒ link.
While analyzing any algorithm, we try to calculate both time and space complexity in the worst possible scenarios.
Insertion: O(1)
Deletion/ Retrieval: O(1)
Search: O(n)
Space Complexity: O(n)
Pros andΒ Cons:
The LIFO pattern of storage of elements is useful in certain applicationsβEg. Calculators where operators take certain precedence.
Cons is that the data storage and access cannot be random, hence not suitable for the applications where memory management cannot be continuous.
I hope you enjoyed this post. Let me know your thoughts if you have any queries or suggestions, would love to hear more fromΒ you.
You can follow me for tutorials on AI/machine learning, programming, data analytics, and BI. You can connect with me on LinkedIn.
Data-structures and Algorithms using Python: Programming Series 101 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Published via Towards AI