Professional Documents
Culture Documents
source: geekrescue.com
Once you get the gist of these pillars of programming, you’ll start
seeing them everywhere. And the more algorithms and data structures
you learn, the more they’ll serve as jet fuel for your career as a software
engineer.
To get you started, let’s rst take a deep dive into Search and Sort, two
classes of algorithms you can’t live without. Then let’s do a quick survey
of the rest of the landscape that includes trees, graphs, dynamic
programming and tons more.
Searching
Roughly speaking, there are two categories of search algorithms you’ll
need to know right away: linear and binary. Depth First Search (DFS)
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 1/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
and Breadth First Search (BFS) are also super-important, but we’ll save
them for the graph traversal section below.
Linear search
The linear and binary algorithms are named as such to describe how
long (time complexity) a search is going to take based on the size of the
input that is being search.
For example, with linear search algorithms, if you have 100 items to
search then the worst case scenario would require that you look at
every item in the input before you came across your desired value. It is
called linear because the time is takes to search is exactly correlated
with the amount of items in the search (100 items/input =100
checks/complexity)
Binary search
Binary search gets its name because the word binary means “of or
relating to 2 things” and the algorithm works by splitting the input into
two parts until it nds the item that is being searched. One half
contains the search item and the other half doesn’t. The process
continues until the spot where the input is split becomes the item that is
being searched. Binary search is basically just a highly disciplined
approach to the process of elimination. It’s faster than linear search,
but it only works with ordered sequences.
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 2/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
Enter binary search. You select the person in the very middle of the
line, and measure their height. They’re 5'7'’. So right away you know
that this person, along with everyone to their right, is not Bin. Now that
you’ve cut your problem in half, you turn your attention to the
remainder of the line and pick the middle person again. They’re 5'4'’. So
you can rule out that person and anyone to their left, cutting the
problem in half again. And so on. After just ve or six of these splits,
you quickly home in on Bin in a fraction of the time it took you to nd
Lin. In doing so, you have followed the binary search algorithm.
Sorting
Ordering, otherwise know as sorting, lists of items is one of the most
common programming tasks you’ll do as a developer. Here we look at
two of the most useful sorting algorithms: MergeSort and QuickSort.
MergeSort
Let’s suppose that rather than coming across the ordered line of people
in the example above, you need to create an ordered line of people out
of an unordered group. You don’t have much time, so you come up with
a strategy to speed things up.
You rst have the group of people, which are all huddled together,
divide into two. Then you have each of the two groups divide into two
again, and so on, until you are dealing entirely with individuals. You
then begin to pair the individuals up, and have the taller of the two in
each pair stand to the right of the other one. Pretty soon everyone is
organized into these left-right ordered pairs.
Next, you start merging the ordered pairs into ordered groups of four;
then merging the ordered groups of four into ordered groups of eight;
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 3/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
QuickSort
QuickSort’s a bit too complex to easily picture with people, so let’s get a
little closer to the code. To start, let’s imagine you have an unordered
list, or array, of eight numbers that you want to order.
4 2 13 6 15 12 7 9
You could use MergeSort, but you hear that QuickSort is generally
faster, so you decide to try it. Your rst step is to choose a number in the
list called the pivot. Choosing the right pivot number will make all the
di erence in how fast your QuickSort performs, and there are ready-
made formulas to follow for choosing good pivots. But for now, let’s
keep it simple and just go with the last number in the array for our pivot
number: 9.
4 2 13 6 15 12 7 9
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 4/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
To make the next step easier to follow, we’ll create a separator at the
beginning of the array and represent it with the pound-sign.
# 4 2 13 6 15 12 7 9
Moving from left to right through our array, our goal is to put any
number smaller than the pivot (9) to the left of the separator, and any
number greater than (or equal to) the pivot to the right of the
separator. We start with the rst number in our array: 4. To move it to
the left of the separator, we just move the separator up one element:
4 # 2 13 6 15 12 7 9
4 2 # 13 6 15 12 7 9
But then we get to 13, which is larger than the pivot number 9 and
already to the right side of the separator. So we leave it alone. Next we
come to 6, which needs to go to the left of the separator. So rst, we
swap it with 13 to get it into position:
4 2 # 6 13 15 12 7 9
4 2 6 # 13 15 12 7 9
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 5/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
4 2 6 # 7 15 12 13 9
4 2 6 7 # 15 12 13 9
4 2 6 7 # 9 12 13 15
Since all the numbers to the left of 9 are now smaller than 9, and all the
numbers to the right of 9 are greater than 9, we’re done with our rst
cycle of QuickSort. Next, we’ll treat each set of four numbers on either
side of the separator as a new array to apply QuickSort to. We’ll spare
you the details, but the next round will give us four pairs of numbers to
easily apply our nal round of QuickSort to. The end result will be the
following ordered list that took us less time to generate than it would
have with the simpler MergeSort:
2 4 6 7 9 12 13 15
Heap sort: When you don’t need a stable sort and you care more about
worst case performance than average case performance. It’s
guaranteed to be O(N log N), and uses O(1) auxiliary space, meaning
that you won’t unexpectedly run out of heap or stack space on very
large inputs.
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 6/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
Bubble sort, selection sort: When you’re doing something quick and
dirty and for some reason you can’t just use the standard library’s
sorting algorithm. The only advantage these have over insertion sort is
being slightly easier to implement.
Quick sort: When you don’t need a stable sort and average case
performance matters more than worst case performance. A quick sort is
O(N log N) on average, O(N²) in the worst case. A good
implementation uses O(log N) auxiliary storage in the form of stack
space for recursion.
Merge sort: When you need a stable, O(N log N) sort, this is about
your only option. The only downsides to it are that it uses O(N)
auxiliary space and has a slightly larger constant than a quick sort.
There are some in-place merge sorts, but AFAIK they are all either not
stable or worse than O(N log N). Even the O(N log N) in place sorts
have so much larger a constant than the plain old merge sort that
they’re more theoretical curiosities than useful algorithms.
. . .
Counting sort: When you are sorting integers with a limited range.
Radix sort: When log(N) is signi cantly larger than K, where K is the
number of radix digits.
Bucket sort: When you can guarantee that your input is approximately
uniformly distributed.
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 7/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
Trees
You’re going to see a lot of trees in your time, as they’re one of the most
common data structures out there. They’re also one of the easiest data
structures to picture and make sense of. Almost all of the terminology
surrounding trees comes from the notion of a family tree. Depending
on the position of a node (i.e. a family member) relative to other nodes
in the tree, the node is called a parent, a child, a sibling, an ancestor, a
descendant, etc.
Graphs
If you want to get technical, a tree is ultimately just a special case of a
graph. So it’s no surprise that graphs are everywhere in daily life and
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 8/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
If you traverse the pyramid oor by oor, starting with the top of the
pyramid, you will be roughly performing a breadth- rst search for your
friend Fiz. Check out the visualization below to develop a better
intuition about this type search and how it behaves.
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 9/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
source: geeksforgeeks.com
Hashing
In recent years, there has been a sea change in how we nd data. While
the primary approach was once binary search, it’s now increasingly
becoming the hash lookup. Although we’ll spare you the time-
complexity comparison here, su ce it to say that when it comes to
searching through lists with millions of items, hashing is often far
faster.
Still more…
We’ve covered a lot of ground. But we’ve still barely hinted at the grand
scope of the world of algorithms and data structures. Here are a few
more to sink your teeth into:
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 10/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
Stacks
Queues
If you liked this post, I’ve got much more like it over at my personal blog
(jasonroell.com). Check it out and have a great day!
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 11/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 12/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 13/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 14/15
8/23/2018 Top Algorithms and Data Structures You Really Need To Know
https://towardsdatascience.com/top-algorithms-and-data-structures-you-really-need-to-know-ab9a2a91c7b5 15/15