DSA - Lesson 1-1
DSA - Lesson 1-1
Data Structures
&
Algorithms
Introduction
Data Structure
• Data Structure is a way of collecting and organizing data in
such a way that we can perform operations on these data in
an effective way. Data Structures is about rendering data
elements in terms of some relationship, for better
organization and storage.
3
D ATA S T R U C T U R E S &
Data
Is a collection of numbers, symbol or characters.
4
D ATA S T R U C T U R E S &
Information
An organized collection of data, that is arranged meaningfully.
5
D ATA S T R U C T U R E S &
Data Structures
• Is a data organization, management and storage format
that enables efficient access and modification.
6
D ATA S T R U C T U R E S &
Why Do We Need Data Structures?
So that we can Store Data in a particular situation efficiently
and use our resources in the most productive way possible.
7
D ATA S T R U C T U R E S &
Real World Application
Supermarket
• Stored Categorically
• Efficient Searching
8
D ATA S T R U C T U R E S &
Real World Application
Library
• Stored Alphabetically
• Efficient Searching
9
D ATA S T R U C T U R E S &
Real World Application
Maps
• Stored as Coordinates
• Efficient Searching
10
D ATA S T R U C T U R E S &
Variables
A Temporary storage for data which is stored in our RAM.
11
D ATA S T R U C T U R E S &
Array / Collections
12
D ATA S T R U C T U R E S &
Programming Application
13
D ATA S T R U C T U R E S &
Programming Application
14
D ATA S T R U C T U R E S &
Programming Application
Found “RM-101”
Searching forafter 3 seconds.
“RM-101”
15
D ATA S T R U C T U R E S &
Programming Application
Let’s search
Found
for “RM-101”
“RM-101”by
after
iterating
1 second
the array.
16
D ATA S T R U C T U R E S &
Programming Application
2s 1s
Found “RM-103”
Let’s search for “RM-103” for after
by iterating
Searching 2 second
the
“RM-103” array from the end.
17
D ATA S T R U C T U R E S &
Conclusion
18
D ATA S T R U C T U R E S &
Need for Data Structure
As applications are getting complex and data rich, there are three common problems that
applications face now-a-days.
• Data Search − Consider an inventory of 1 million (106) items of a store. If the application
is to search an item, it has to search an item in 1 million (106) items every time slowing
down the search. As data grows, search will become0 slower.
• Processor Speed − Processor speed although being very high, falls limited if the data
grows to billion records.
• Multiple Requests − as thousands of users can search data simultaneously on a web
server, even the fast server fails while searching the data.
To solve the above-mentioned problems, data structures come to rescue. Data can be organized
in a data structure in such a way that all items may not be required to be searched, and the
required data can be searched almost instantly.
19
D ATA S T R U C T U R E S &
What is an Algorithm?
• Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be implemented
in more than one programming language.
For example:
An algorithm to add two numbers:
• Take two number inputs
• Add numbers using the + operator
• Display the result
20
D ATA S T R U C T U R E S &
Let us consider the problem of preparing an omelette. To prepare an omelette, we follow
the steps given below:
1) Get the frying pan.
2) Get the oil.
a. Do we have oil?
• If yes, put it in the pan.
• If no, do we want to buy oil?
If yes, then go out and buy.
If no, we can terminate.
3)Turn on the stove, etc...
What we are doing is, for a given problem (preparing an omelette), we are providing a
step-by step procedure for solving it. The formal definition of an algorithm can be stated
as: An algorithm is the step-by-step unambiguous instructions to solve a given problem.
21
D ATA S T R U C T U R E S &
Qualities of a Good Algorithm
22
D ATA S T R U C T U R E S &
23
D ATA S T R U C T U R E S &
From the data structure point of view, following are some important categories of
algorithms −
24
D ATA S T R U C T U R E S &
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics −
25
D ATA S T R U C T U R E S &
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
26
D ATA S T R U C T U R E S &
Algorithm 2: Find the largest number among three
numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
27
D ATA S T R U C T U R E S &
A good algorithm maintains a level of correctness while being
efficient. Meaning, there is little error, and it doesn’t take much time
to complete. Another important component is comprehensibility.
We wouldn’t be able to use algorithms so frequently if they couldn’t
be understood. Algorithmic and computational thinking is so
pervasive that it governs the simplest things in our daily lives. Here
are some examples of algorithms you interact with everyday.
28
D ATA S T R U C T U R E S &
Recipes
Just like sorting papers and even tying your shoes, following a recipe is a type
of algorithm. The goal of course being to create a duplicated outcome. In
order to complete a recipe, you must follow a given set of steps. Say you are
making bread. You need flour, yeast and water. After you have your
ingredients, you need to combine them in a certain way that will create a
predictable outcome, in this case a loaf of bread.
29
D ATA S T R U C T U R E S &
Sorting Papers
A simple task and yet it uses algorithmic thinking. When you are sorting office
files or your personal documents you are implementing an algorithm. In its
most basic sense, you are following a set of tasks to achieve an outcome. The
reason why sorting papers is a great example, is because it shows the variety
of tasks and specifications algorithms can use. For instance, you can sort your
files alphabetically, by word count, by date, and countless others. The goal is
to simplify the organizational process by using small tasks.
30
D ATA S T R U C T U R E S &
ACTIVITY TIME!
31
D ATA S T R U C T U R E S &
Write down at least 3
algorithms you interact
with everyday and present
it in front of the class.
32
D ATA S T R U C T U R E S &
THANK
YOU