Uploaded by Aakash Shah

DSA

advertisement
The Art of Loving Algorithms
Why Data Structures Are Your New Best Friend
Aakash Shah
What do we know so far?
• 😺
Guess a # 🤔
39
40
25
30
35
36
12
24
23
37
11
26
31
22
2
13
8
20
21
27
33
1
9
3
14
4
19
5
10
32
28
6
7
17
29
38
18
34
15
16
search(S, x) 🪄
#
Guess
Less?
1
20
Yes
1
2
10
No
1
3
15
Yes
11
4
12
No
11
5
14
Yes
12
x
🤫
20
10
x
x
15
12
13
40
14
20
20
x
15
15
13
linearSearch vs. binarySearch
Criteria
Linear Search
Binary Search
Maximum Tries
40
6
Average Tries
20.5
~5.32 (log2(40) ≈ 5.32)
Minimum Tries
1 (Best case)
1 (Best case)
Sorted Required?
No
Yes
Algorithm Complexity
O(n)
O(log n)
Initial Position
Start of list
Middle of list
Movement Strategy
One at a time
Halves the list each time
Comparison Operations
>= 1, <= 40
<= 6
Use Case
Small datasets, unsorted data
Large datasets, sorted data
⌈log2(1,000,000,000)⌉=30
What do we know so far?
• 😺
• Better algorithms are powerful.
Array
arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Binary Tree
P
L
R
Binary 🔎 Tree
P
L
R
Left child < Parent < Right child
Binary 🔎 Tree
class Tree:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
P
L
R
Left child < Parent < Right child
def search_bst(root: Tree, target):
if root is None:
return None # Value not found
if root.val == target:
return root # Value found
elif root.val < target:
return search_bst(root.right, target)
else:
return search_bst(root.left, target)
What do we know so far?
•
•
•
•
😺
Better algorithms are powerful.
Data Structures are a backbone of algorithms.
Algorithms can be implemented by multiple Data Structures.
Array Operations
arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
def insert_sorted_array(arr, target):
index = 0
while index < len(arr) and arr[index] < target:
index += 1
arr.insert(index, target)
•
•
•
•
•
Insertion - O(n)
Deletion - O(n)
Search - O(log(n))
Traversal - O(n)
Access - O(1)
https://www.bigocheatsheet.com/
BST Operations
P
L
R
Left child < Parent < Right child
def insert_bst(root, key):
if root is None:
return Node(key)
else:
if root.val < key:
root.right = insert_bst(root.right, key)
else:
root.left = insert_bst(root.left, key)
return root
•
•
•
•
•
Insertion - O(log(n))
Deletion - O(log(n))
Search - O(log(n))
Traversal - O(n)
Access - O(log(n))
https://www.bigocheatsheet.com/
What do we know so far?
•
•
•
•
•
•
😺
Better algorithms are powerful.
Data Structures are a backbone of algorithms.
Algorithms can be implemented by multiple Data Structures.
Select the Data Structure most relevant to the application.
Hash Map
Imagine you have a big drawer, and you want to store various
objects like keys, papers, and pens. You could randomly throw
everything in, but it would make nding anything later an ordeal.
Instead, you might use small labeled boxes within the drawer to
make retrieval easier.
fi
fi
fi
In computer science, this concept is similar to what's known as a
"HashMap." It's a way to store and quickly nd data by using a
unique identi er, much like the labels on our boxes
Hash Map - Collisions
But what happens when two objects need to be in the same
box? This is called a "collision," and it can slow down the
process of nding your stuff. Initially, Java used what you could
think of as a "mini-list" in each box to handle these collisions.
However, if too many items ended up in the same box, nding
something could become slow, like searching through a cluttered
box in your drawer.
fi
fi
fi
So you either store only one item in one drawer which is not
practical or nd a better way to store multiple values in the same
labeled box.
Hash Map - Collision Avoidance
fi
Java 8 introduced a clever solution. If a box gets too cluttered
(technically, if 8 or more items end up in the same box), it
replaces the "mini-list" with a "mini-tree." Trees are another way
to organize data that makes it easier to nd things, even when
there's a lot to sort through. Think of it as installing mini-shelves
inside the overcrowded box, making it easier to see and grab
what you need quickly.
Hash Map
What do we know so far?
•
•
•
•
•
•
•
😺
Better algorithms are powerful.
Data Structures are a backbone of algorithms.
Algorithms can be implemented by multiple Data Structures.
Select the Data Structure most relevant to the application.
You can use data structures within other data structures.
Try implementing data structures in different languages.
Recap
1. Challenge yourself
2. First principles thinking
3. Prefer Optimization
4. Know all data structures
5. Try all data structures
6. Make a list of: The _____ data structure is particularly useful for
algorithms that involve ____
7. Practice Interviews (https://www.pramp.com)
Thank you
Download