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
Let's compare how long Selection Sort, Quicksort, and Merge Sort take to run on different data sets.

0:00
I've removed the call to print that displays the sorted list at the end of our

0:04
selection_sort, quicksort, and merge_sort scripts.

0:07
That way, it'll still run the sort, but

0:09
the output won't get in the way of our comparing run times.

0:13
Let's try running each of these scripts, and see how long it takes.

0:17
time python selection_sort, we'll do that

0:22
one first, numbers/10,000.txt.

0:30
We combine the user and sys results, and that gives us about 6 seconds.

0:35
Now let's try quicksort, time python

0:40
quicksort.py numbers/10000.txt.

0:45
Much faster, less than a second.

0:48
And finally, time python merge_sort.py numbers/10000.txt.

0:58
A little longer, but far less than a second.

1:01
So even on a list with just 10,000 numbers,

1:04
selection_sort takes many times as long as quicksort and merge_sort.

1:09
And remember, I ran the selection_sort script on a file with a million numbers.

1:13
And it took so long that my workspace timed out before it completed.

1:18
It looks like selection_sort is out of the running as a viable sorting algorithm.

1:22
It may be easy to understand and implement, but

1:25
it's just too slow to handle the huge data sets that are out in the real world.

1:31
Now let's try quicksort and merge_sort on our file with a million numbers, and

1:35
see how they compare there.

1:36
time python quicksort.py

1:40
numbers/1000000.txt.

1:49
Looks like it took about 11 seconds of CPU time.

1:53
Now let's try merge_sort, time python

1:57
merge_sort.py numbers/1000000.txt.

2:05
That took about 15 seconds of CPU time.

2:08
It looks like quicksort is marginally faster than merge_sort on this

2:12
sample data.
You need to sign up for Treehouse in order to download course files.
Sign up