DSA - Module 1 - Part 1
DSA - Module 1 - Part 1
Structure
Module 1 – Part 1
Gajendra Shrimal
Content
1. Introduction
2. Problem Solving
3. Data
6. Data structures
10. Algorithms
Introduction
In this part we will cover
Data structures, Advantages and disadvantages, and use cases of different
data structures.
Enhanced the knowledge about abstract data types (ADTs) and their
implementations.
Data structures are the way data is organized and stored in a computer's
memory.
Abstract Data Types (ADTs) define a set of operations on the data
structure without specifying its implementation.
Static implementations have fixed sizes, while dynamic implementations
can resize and adapt as needed.
Problem Solving
Correct
› always returns the desired output for all legal instances of
the problem.
Unambiguous
Precise
Efficient
› Can be measured in terms of
Time
Space
› Time tends to be more important
Importance of Data Structures and Algorithms:
Understanding data structures and algorithms is crucial for several
reasons:
Efficient Data Management: Proper data structures enable efficient data
storage, retrieval, and manipulation, leading to better performance of
programs and systems.
Optimized Operations: Efficient algorithms help in achieving faster and
more accurate results, reducing time and resource consumption.
Problem-Solving Skills: Learning algorithms improves problem-solving
skills, allowing developers to devise effective solutions to various real-
world challenges.
Code Optimization: Knowledge of data structures and algorithms
enables developers to write optimized and maintainable code.
Interview and Career Advancement: Data structure and algorithm
knowledge is often a key factor in technical interviews and career
growth in the field of software development.
Data:
Data refers to a collection of organized and interpretable information.
It can encompass facts, statistics, measurements, observations, or any
other form of knowledge that can be recorded or represented.
Data can exist in various formats such as text, numbers, images,
audio, or video.
The collection of data is frequently organized into hierarchy of
fields, records and files. The data serves as the raw material for
analysis, decision-making, and understanding patterns or trends.
While they are not themselves data structures, they are the basic
types that can be used to construct more complex data
structures. Here are some common built-in data types often used
in any programming language: Int, Float, Double, Long etc.
Static Implementation
In a static implementation, the data structure is allocated a fixed
amount of memory at compile-time or initialization.
The size of the data structure is determined in advance and
cannot be changed during runtime.
Static implementations are often used when the maximum size
of the data structure is known and remains constant.
Examples of static implementations include arrays, fixed-size
matrices, and statically allocated linked lists.
Dynamic Implementation
In a dynamic implementation, the data structure is allocated
memory at runtime as needed.
The size of the data structure can grow or shrink dynamically
as elements are added or removed.
Dynamic implementations are used when the size of the data
structure is not known in advance or can change during
runtime.
Examples of dynamic implementations include dynamically
allocated arrays, linked lists, trees, and hash tables.
Static vs Dynamic Implementation
Algorithms
Algorithms are used in various applications: Algorithms are a
fundamental concept in computer science and find applications in
sorting and searching data, pathfinding, optimization problems,
encryption and decryption, artificial intelligence, and more. They are
essential for solving complex computational problems and designing
efficient software solutions.
Time-space tradeoff in choosing algorithms: When selecting an
algorithm involving any data structure, the objective is to find a
balance between increasing the space for storing data and reducing
the time to process the data, or vice versa. This tradeoff between
space and time is crucial for optimizing the performance of algorithms
in various scenarios, as demonstrated in the example of searching
algorithms.
Example Problem: Searching a given number in the record of the file.
Statement - Given a series of integers and a target value, implement a
search algorithm to determine If the target value exists in the series or
not. If found return the index of the target value; otherwise, return -1.
Implementation of Algorithm
First, Start from the beginning of the list;
Compare 10 with target value (60)
If they matches; search is successful and position 1
(position of 10 i.e. 1) is returned; but here they are not
matching means go to step 4
Go to the Next element (i.e. 20)
Repeat steps 2 to 4 until the target find or end of the list is
reached.
(Check items 20, 30, 40, 50, at 60; here target is matched,
will move to successful case and result is position 6 will be
returned)
If the target value is not found after checking all elements,
the search is unsuccessful, and a special value (e.g., -1)
may be returned to indicate this.
Implementation of Algorithm
Linear search is straightforward to implement but may not be
efficient for large lists or arrays.
Its time complexity is O(n), where n is the number of
elements in the list.
In the worst case, the algorithm may need to check every
element in the list, resulting in a linear time complexity.
Here worst case leads us to 7 searches (means maximum no.
of loop executes.)
Summary…Topic Covered
1. Introduction
2. Data
5. Data structures
9. Algorithms