Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now let's look at an algorithm that speeds up the process further by further reducing the number of comparisons it makes: "Quicksort".
We'll have the complete code used in this video and the next in an upcoming instruction step!
You've seen "Bogosort", which doesn't make any progress toward sorting a list with each pass: either it's entirely sorted or it isn't. You've seen "selection sort", which moves one value over to a sorted list with each pass, so that it has fewer items to compare each time. Now let's look at an algorithm that speeds up the process further by further reducing the number of comparisons it makes: "Quicksort".
# Again, you can ignore these lines at the top. We're just using them to load
# a file full of numbers into a list.
import sys
from load import load_numbers
numbers = load_numbers(sys.argv[1])
# The Quicksort algorithm relies on recursion. To implement it, we'll write a
# recursive function. We'll accept the list of numbers to sort as a parameter.
def quicksort(values):
# Quicksort is recursive because it keeps calling itself with smaller and
# smaller subsets of the list you're trying to sort. We're going to need a
# base case where the recursion stops, so it doesn't enter an infinite loop.
# Lists that are empty don't need to be sorted. And lists with just one
# element don't need to be sorted, either. In both cases, there's nothing
# to flip around. So we'll make that our base case; if there are 0 or 1
# elements in the list passed to the "quicksort" function, we'll return
# the unaltered list to the caller.
if len(values) <= 1:
return values
# Lastly, we need to call our quicksort function with our list of numbers,
# and print the list it returns.
sorted_numbers = quicksort(numbers)
print(sorted_numbers)
 That takes care of our base case. Now, we need a recursive case.
 We're going to rely on a technique that's common in algorithm design, called "divide and conquer".
 Basically, we're going to take our problem and split it into smaller and smaller problems until they are easy to solve.
 In this case, that means taking our list and splitting it into smaller lists.
Suppose we load the numbers from our 8.txt
file into a list. How do we divide it? It would probably be smart to have our quicksort function divide the list in a way that brings it closer to being sorted.
quicksort([4, 6, 3, 2, 9, 7, 3, 5])
Let's pick an item from the list. We'll just pick the first item for now, "4". We'll call this value we've picked the pivot, like the center of a seesaw on a playground.
We'll break the list into two sublists. The first sublist will contain all the items in the original list that are smaller than the pivot. The second sublist will contain all the items in the original list that are greater than the pivot.
Less than pivot Pivot Greater than pivot
[3, 2, 3] [4] [6, 9, 7, 5]
The sublists of values less than and greater than the pivot aren't sorted. But what if they were?
Less than pivot Pivot Greater than pivot
[2, 3, 3] [4] [5, 6, 7, 9]
You could just join the sublists and the pivot all together into one list, and the whole thing would be sorted!
[2, 3, 3, 4, 5, 6, 7, 9]
So how do we sort the sublists? We call our quicksort function recursively on them! This may seem like magic, but it's not; it's the "divide and conquer" algorithm design technique at work. If our quicksort function works on the big list, then it will work on the smaller lists, too.
Less than pivot Pivot Greater than pivot
quicksort([3, 2, 3]) [4] [6, 9, 7, 5]
For our first sublist, we take the first item as the pivot again. That's 3. We break the sublist into two sublists, one with everything less than the pivot, and one with everything greater than the pivot.
Less than pivot Pivot Greater than pivot
[2, 3] [3] []
Notice that there's a value equal to the pivot that gets put into the "lessthan" sublist. Our finished quicksort function is actually going to put everything that's less than or equal to the pivot in the first sublist, but I don't want to say "less than or equal to" over and over, so I'm just referring to it as the "less than pivot" sublist. Also notice that there are no values greater than the pivot. That's okay; when we join the sublists back together, that just means nothing will be in the returned list after the pivot.
We still have one sublist that's more than one element long, so we call our quicksort function on that, too. You and I can see that it's already sorted, but the computer doesn't know that, so it will call it anyway just in case.
Less than pivot Pivot Greater than pivot
quicksort([2, 3]) [3] []
It picks the first element, 2, as a pivot. There are no elements less than the pivot, and only one element greater than the pivot.
Less than pivot Pivot Greater than pivot
[] [2] [3]
That's it for the recursive case; we've finally hit the base case for our quicksort function. It will be called on both the empty list of elements less than the pivot, and the oneitem list of elements greater than the pivot... ...but both of those lists will be returned as they are, because there's nothing to sort.
Less than pivot Pivot Greater than pivot
quicksort([]) quicksort([3])
So now, at the level of the call stack above this, the returned sorted lists are used in place of the unsorted sublist that's less than the pivot and the unsorted sublist that's greater than the pivot.
These are joined together into one, sorted list. (Remember that any empty lists get discarded.)
[2, 3]
Then, at the level of the call stack above that, the returned sorted lists are used in place of the unsorted sublists there. (Again, they were already sorted, but the quicksort method was called on them anyway just in case.)
Less than pivot Pivot Greater than pivot
[2, 3] [3] []
The sublists are joined together into one, sorted list.
[2, 3, 3]
At the level of the call stack above that, the returned sorted list is used in place of the unsorted sublist that's less than the pivot. So now, everything that's less than or equal to the pivot is sorted.
Less than pivot Pivot Greater than pivot
[2, 3, 3] [4] [6, 9, 7, 5]
Now, we call quicksort on the unsorted sublist that's greater than the pivot. And the process repeats for that sublist.
Less than pivot Pivot Greater than pivot
[2, 3, 3] [4] quicksort([6, 9, 7, 5])
We pick the first element, 6, as the pivot. We split the sublist into sublists of elements that are less than and greater than this pivot. And we recursively call the quicksort function until those sublists are sorted.
Less than pivot Pivot Greater than pivot
[5] [6] [9, 7]
Eventually, a sorted sublist is returned to our first quicksort function call.
Less than pivot Pivot Greater than pivot
[2, 3, 3] [4] [5, 6, 7, 9]
We combine the sublist that's less than (or equal to) the pivot, the pivot itself, and the sublist that's greater than the pivot into a single list. And because we recursively sorted the sublists, the whole list is sorted.
[2, 3, 3, 4, 5, 6, 7, 9]
So that's how the quicksort function is going to work. In the next video, we'll show you the actual code.

0:00
You have seen bogo_sort.

0:01
Which doesn't make any progress toward sorting a list with each pass,

0:05
either it's entirely sorted or it isn't.

0:07
You've seen selection_sort, which moves one value over to an a sorted

0:11
list with each pass so that it has fewer items to compare each time.

0:16
Now let's look at an algorithm that speeds up the process further by further reducing

0:20
the number of comparison it makes.

0:22
It's called quicksort.

0:24
Here's some Python code where we'll implement quicksort.

0:27
Again, you can ignore these lines at the top.

0:30
We're just using them to load a file full of numbers into a list.

0:34
The quicksort algorithm relies on recursion.

0:37
To implement, it we'll write a recursive function.

0:39
We'll accept the list of numbers to sort as a parameter.

0:43
quicksort is recursive because it keeps calling itself with smaller and

0:47
smaller subsets of the list you're trying to sort.

0:50
We're going to need a base case where the recursion stops, so

0:53
it doesn't end in an infinite loop.

0:55
Lists that are empty don't need to be sorted, and

0:58
lists with just one element don't need to be sorted either.

1:01
In both cases, there's nothing to flip around.

1:04
So we'll make that our base case.

1:06
If there are 0 or 1 elements in the list past to the quicksort function,

1:10
we'll return the unaltered list to the caller.

1:13
Lastly, we need to call our quicksort function with our list of numbers and

1:19
print the list it returns.

1:23
That takes care of our base case, now we need a recursive case.

1:27
We're going to rely on a technique that's common

1:30
in algorithm design called divide and conquer.

1:32
Basically, we're going to take our problem and split it into smaller and

1:36
smaller problems until they're easy to solve.

1:39
In this case, that means taking our list and splitting it into smaller lists.

1:44
Viewers, a suggestion, the process I'm about to describe is complex.

1:48
There's just no way around it.

1:50
If you're having trouble following along, remember the video playback controls.

1:54
Feel free to slow the playback down, rewind or pause the video as needed.

1:58
After you watch this the first time, you may also find it helpful to rewind and

2:03
make your own diagram of the process as we go.

2:05
Okay, ready, here it goes.

2:08
Suppose we load the numbers from our 8.txt file into a list.

2:13
How do we divide it?

2:14
It would probably be smart to have our quicksort function

2:17
divide the list in a way that brings it closer to being sorted.

2:21
Let us pick an item from the list.

2:23
We'll just pick the first item for now, 4.

2:26
We'll call this value we've picked the pivot,

2:29
wike the center of a seesaw on a playground.

2:31
We'll break the list into two sublists.

2:33
The first sublist will contain all the items in the original list that

2:36
are smaller than the pivot.

2:38
The second sublist will contain all the items in the original list

2:41
that are greater than the pivot.

2:43
The sublist of values less than and greater than the pivot aren't sorted.

2:48
But what if they were?

2:49
You could just join the sublists and the pivot all together into one list, and

2:53
the whole thing would be sorted.

2:56
So how do we sort the sublist?

2:57
We call our quicksort function recursively on them.

3:00
This may seem like magic, but it's not.

3:03
It's the divide and conquer algorithm technique at work.

3:07
If our quicksort function works on the big list,

3:09
then it will work on the smaller list, too.

3:12
For our first sublist, we take the first item, it's the pivot again.

3:16
That's 3.

3:18
We break the sublist into two sublists.

3:20
One with everything less than the pivot.

3:22
And one with everything greater than the pivot.

3:25
Notice that there's a value equal to the pivot that gets put in to

3:28
the less than sublist.

3:30
Our finished quicksort function is actually going to put everything that's

3:33
less than or equal to the pivot in the first sublist.

3:36
But I don't wanna say less than or equal to over and over, so

3:40
I'm just referring to it as the less than pivot sublist.

3:44
Also, notice that there are no values greater than the pivot.

3:47
That’s okay.

3:48
When we join the sublists back together,

3:49
that just means nothing will be in the returned list after the pivot.

3:53
We still have one sublist that’s more than one element long.

3:56
So we call our quicksort function on that too.

3:59
You and I can see that it’s already sorted.

4:01
But the computer doesn’t know that, so it’ll call it anyway, just in case.

4:06
It picks the first element, 2, as a pivot.

4:08
There are no elements less than the pivot and

4:10
only one element greater than the pivot.

4:13
That's it for the recursive case.

4:15
We've finally hit the base case for our quicksort function.

4:18
It'll be called on both the empty list of elements less than the pivot and

4:21
the one item list of elements greater than the pivot.

4:24
But both of these lists will be returned as they are,

4:27
because there's nothing to sort.

4:29
So now, at the level of the call stack above this,

4:31
the return sorted lists are used in place of the unsorted sublist that's less

4:36
than the pivot and the unsorted sublist that's greater than the pivot.

4:39
These are joined together into one sorted list.

4:42
Remember that any empty lists get discarded.

4:46
Then at the level of the call stack above that,

4:48
the returned sorted lists are used in place of the unsorted sublists there.

4:52
Again, they were already sorted, but

4:54
the quicksort method was called on them anyway, just in case.

4:57
The sublists are joined together into one sorted list.

5:01
At the level of the call stack above that, the return sorted list

5:04
is used in place of the unsorted sublist that's less that the pivot.

5:07
So now everything that's less than or equal to the pivot is sorted.

5:11
Now we call quicksort on the unsorted sublist that's greater than the pivot and

5:16
the process repeats for that sublist.

5:18
We pick the first element, 6, as the pivot.

5:21
We split the sublist into sublists of elements that are less than and

5:24
greater than this pivot.

5:25
And we recursively call the quicksort function until those sublists are sorted.

5:31
Eventually, a sorted sublists is returned to our first quicksort function call.

5:35
We combine the sublist that's lessthanorequalto the pivot,

5:39
the pivot itself, and the sublist that's greater than the pivot into a single list.

5:43
And because we recursively sorted the sublist, the whole list is sorted.

5:48
So that's how the quicksort function is going to work.

5:51
In the next video, we'll show you the actual code.
You need to sign up for Treehouse in order to download course files.
Sign up