0% found this document useful (0 votes)
23 views43 pages

#1 Intro To DSA

Uploaded by

Gian Dizon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views43 pages

#1 Intro To DSA

Uploaded by

Gian Dizon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Introduction to

Data Structures
& Algorithms
Data Structures and Algorithms
Objectives:

• Describe data structures.


• Describe algorithms.
• Explain the different types of
data structures.
• Differentiate simple search
from binary search.
Data Structures
Data Structures

A data structure is a storage that is used


to store and organize data. It is a way of
arranging data on a computer so that it
can be accessed and updated
efficiently.
Data Structures

A data structure is also used managing


and manipulating data effectively,
enabling faster access, insertion, and
deletion operations.
Classification of Data Structures
Linear Data Structure

The elements are Examples:


arranged sequentially • Static Data Structure
or linearly. Each • Array
element is attached to • Dynamic Data Structure
its previous and next • Stack
adjacent elements. • Queue
• Linked List
Non-Linear Data Structure

The data elements are Examples:


not placed • Trees
sequentially or • Graphs
linearly. The elements
usually cannot be
traversed in a single
run.
Algorithms
Algorithms

An algorithm is a set of Algorithms are generally


instructions for independent of
accomplishing a task or underlying languages.
solving a problem. It is a They can be
implemented in more
step-by-step procedure than one programming
that must be executed in language.
a certain order to get the (Python will be used mostly for this
desired output. subject.)
Algorithms

There are many types • Searching algorithms


of algorithms, each • Sorting algorithms
with their own • Divide and Conquer algorithms
approach and • Greedy algorithms
efficiency. Some of
these types are… • Recursion
• Graph algorithms
Write a set of step-
by-step instructions
to buy a bottle of
water.
Practice Time
Data Structures
and Algorithms
Data Structures and Algorithms (DSA)

DSA refers to the study of methods for


organizing and storing data and the
design of procedures (algorithms) for
solving problems, which operate on data
structures.
Time and Space
Complexities
Time and Space Complexities

Generally, there is always more than one way


to solve a problem. Different algorithms can
solve the same problem but have different
ways to do it.
Time Complexities

Time complexity refers to how many


times a statement is executed. It does
not necessarily mean the
running/execution time of the code
(usually measured in milliseconds, ms)
Imagine a classroom of 100
students. You give your pencil
to 1 person. You need to find
that pen without knowing to
whom you gave it. How do
you find the pen?

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

• O (n) The O is called Big O


Notation (big-oh, or
order). It is a special
• O (n )
2
notation that tells how
fast an algorithm is. The n
• O (log n) refers to the number of
operations based on the
size of the list.
Time Complexities and the Big O Notation

Big O establishes The worst-case scenario


a worst-case run in the previous example
would be asking all 100
time. students. The best-case
scenario would be finding
the pen after asking only
1 student.
Big O Notation

In algorithm analysis, the Big O notation


provides a way to estimate how the runtime
of an algorithm changes, as the input data
size increases.
Big O Notation and Algorithm Performance
Time Complexities and the Big O Notation

By knowing the worst-case, you’re reassured


in knowing how long solving a problem will
require.

It’s also important to look at the average case.


(to be discussed at a different lesson)
Some common Big O run times

Fastest
O (log n) Also known as Log Time
Example: Binary Search

O (n) Also known as Linear Time


Example: Simple Search

O (n * log n) A fast sorting algorithm


Example: Quicksort

O (n )
2 A slow sorting algorithm
Example: Selection Sort

Slowest O (n!) A really slow algorithm


Example: Travelling salesman problem
When talking about
running time in Big O
notation, log always
means log2.
Space Complexities

Space complexity refers to the total space


taken by an algorithm with respect to the
input size.
Space complexity also includes auxiliary
space, which is the extra or temporary
space used by an algorithm.
Space Complexities

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

A sample array (list) created in Python:


team = [“Al”, “Ben”, “Carl”, “Dave”, “Eric”, “Fred”,
“Gel”, “Harold”, “Ian”, “John”]
Simple Search vs.
Binary Search
Simple Search vs. Binary Search

Suppose I picked a If your guess


number from 1 to 10. eliminates only one
You need to guess the number at a time,
number I picked. For what you’re doing is
each guess, I’ll say too
low, too high, or
simple search.
correct.
Simple Search vs. Binary Search

Suppose I picked a If your guess can


number from 1 to 10. eliminate half of
You need to guess the the possible
number I picked. For numbers every try,
each guess, I’ll say too you’re doing
low, too high, or
correct.
binary search.
Binary Search

Binary search involves Binary search only


repeatedly dividing a works when the range
given list in half and or list is in sorted order
selecting the midpoint
(i.e., in ascending
as the guess. Each guess
halves the remaining order, or alphabetical
possibilities. order).
Suppose I’m thinking of a
number from 1 to 10. How
many steps could simple
search take? How many
steps could binary search
take? Practice Time
Suppose I’m thinking of a
number from 1 to 100.
How many steps could
simple search take? How
many steps could binary
search take? Practice Time
Suppose I’m thinking of a
number from 1 to 1,000.
How many steps could
simple search take? How
many steps could binary
search take? Practice Time
Suppose I’m thinking of a
number from 1 to 10,000.
How many steps could
simple search take? How
many steps could binary
search take? Practice Time
Suppose I’m thinking of a
number from 1 to
100,000. How many steps
could simple search take?
How many steps could
binary search take? Practice Time
References

Bhargava, A. (2016). Grokking Algorithms – An illustrated guide for


programmers and other curious people.
GeeksforGeeks. (2024). Learn Data Structures and Algorithms DSA
Tutorial. https://www.geeksforgeeks.org/learn-data-structures-and-
algorithms-dsa-tutorial/#dsa-full-form
TutorialsPoint. (nd). Data Structures and Algorithms (DSA) Tutorial.
https://www.tutorialspoint.com/data_structures_algorithms/index.htm

You might also like