#1 Intro To DSA
#1 Intro To DSA
Data Structures
& Algorithms
Data Structures and Algorithms
Objectives:
Example
Some ways to find the pen:
• Ask each student one-by-one.
• Ask the first person if he has the pen. Then ask him
about the other 99 people if they have the pen and
so on.
• Divide the class into 2 groups. Ask them if they
have it. Take the group that answered and divide it
into 2 groups again, ask them again, and so on.
Repeat until you find the pen.
Example
Some ways to find the pen:
• Ask each student one-by-one.
• This approach is called O (n)
• Do this if 1 student had the pen and
only he knew it.
Example
Some ways to find the pen:
• Ask the first person if he has the pen.
Then ask him about the other 99 people
if they have the pen and so on.
• This is known as O (n2)
• Do this if only 1 student knows on
which student the pen is hidden.
Example
Some ways to find the pen:
• Divide the class into 2 groups. Ask them if
they have it. Take the group that answered
and divide it into 2 groups again, ask them
again, and so on. Repeat until you find the
pen.
• This is known as O (log n)
• Do this if all students knew, but would
only tell if you guessed the right group.
Example
Time Complexities and the Big O Notation
Fastest
O (log n) Also known as Log Time
Example: Binary Search
O (n )
2 A slow sorting algorithm
Example: Selection Sort
Space complexity is a
parallel concept to
time complexity. For
example, if we need to
create an array of size
n, this will require O(n)
space.
Space Complexities