Sorting Secret - Computerphile

Computerphile · Intermediate ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

Professor Graham Hutton explains how selection sort and insertion sort are essentially the same algorithm when visualized as a series of sorting networks, and demonstrates this using a basic building block of a box with four sides that takes two numbers in and outputs the smallest and largest numbers.

Full Transcript

Today we're going to talk about sorting and in particular we're going to talk about two very well-known sorting methods. One is called selection sort. The other one is called insertion sort. And when people learn about sorting methods, they usually learn about these as being two completely different ideas. You have selection sort on the one hand and insertion sort on the other hand. And what I'm going to show you today is that these are actually the same. You can regard them as being equal. And the trick is that you just draw pictures on them. You don't need any coding to see this. You don't need to know any fancy stuff. In fact, you don't need to know anything about computer science at all. If you just draw a picture of selection sort in the right way and a picture of insertion sort in the right way, you see that they're actually the same thing. And this is kind of a secret about sorting methods that even people like me who've been doing computer science for many years, most people or hardly anybody actually knows this little trick. We'll start off by kind of refreshing ourselves with pictures about what sorting actually is. So let me draw a box. What we're going to do is put some numbers into this box. Doesn't matter how many numbers. Everything which I'm going to show you is completely general, but we'll do it with five. So suppose we do a five and a two and a three and a one and a four. These are the first five numbers in in some kind of random order here. But the key thing is they're not sorted. They're not in ascending order. And what we'd like our sorting box to do is give us the same numbers out but in sorted order. So we'd like to have 1 2 3 4 and five coming out. Let's start to think about how we might construct a little program which implemented this kind of sorting procedure here. The most basic building block which you can think of is just a little box with four sides. What this box is going to do is on the left hand side and on the top side you're going to have two numbers come in and then at the bottom the smallest number is going to pop out and on the right hand side the largest number is going to pop out. So you can think of this as being like a little sorting box for only two numbers. So for example, if we put the number one in on the left hand side and the number two in on the top, then what our little sorting box would do is give us the smallest one out the bottom and it would give us the biggest one out the right hand side. So this is a sorting box for two numbers. And it doesn't matter the order in which the two numbers come in. So if I swap this around and if I had a two coming in here and a one coming in here, it doesn't make any difference. The smallest one is going to pop out the bottom and the biggest one is going to pop out the right hand side. So the game here is we've got this basic building block and we want to kind of plug these together in just with pictures and see how you could build a little sorting program. The trick is that you just build a little triangle of these boxes. So let's put a bunch of these things together. There's a little triangle of these boxes and we're going to just wire them together in a very straightforward way. We'll just draw the obvious little links between them. And each of these boxes just sorts two numbers like we saw a few moments ago. So let's take our little example and just push it through this little uh sorting network and actually see what happens. So I think the numbers we had were five 2 3 1 and four. So out the bottom we hope to get 1 2 3 4 5. So let's see what happens. So we're going to treat the first column first of all and we'll see what happens. So it's really simple. So you've got the two and the five coming into the first box. So the smallest number pops out the bottom. So that's the two. And the biggest number comes out the right hand side. So that's the five. And then we do the same with the second box in the column. So we've got a two coming in and a three coming in. So the smallest number pops out the bottom. And the biggest number comes out the right hand side. And we just do the same again. Then we've got a one and a two. So the one comes out the bottom and the two comes out the right hand side. And then we've got a four and a one. So the one pops out the bottom and the four comes out the right hand side. So what you see is that the smallest number which in this case has one has kind of rippled its way down to the bottom. So what's happened is this first column has selected the smallest number. Okay? And it's quite easy to see that that's the case because the top box selects the smallest from the two numbers it's given and then the second box selects the smallest numbers from the two numbers it's given and so on and so on. So kind of the the smallest number is going to ripple its way down to the bottom. So it's selecting the smallest one. Then we do the same with the with the remaining columns. And we won't be surprised with what happens. So we've got a three coming in and a five coming in. So the three will pop out and the five will pop out here. Then we've got a two and a three. So the two is smaller. So it pops out and the three comes out here. And then we've got a two and a four and the two is the smallest. And then the four pops out the other side. And you see what's happened again from the remaining numbers five 3 2 and four. The second column has selected the smallest one. And then if I just complete this, it's obvious what's going to happen. We get a three here and a five here. Three, four, four, five. So what you see is that our little sorting grid has taken five numbers in a mixed up order and just by pushing them through one column at a time, we've ended up with the numbers in the correct order. And this is known as selection sort because each column just selects the smallest number from what is left. Okay? So nice and easy. You don't need to know anything about computer science, anything about algorithms. Anyone can understand what's going on with selection sort here. But you can actually view this picture in another way. So let me redraw the same picture. Okay. So I just wire it up in exactly the same way as before and we'll push exactly the same five numbers through. So we started off with a five, two, three, one, and a four. So what we did last time is we treated it in terms of the columns. But actually you can do exactly the same thing in terms of the rows. So we'll do one row at a time and actually see what happens. So if we consider the first row, it's the same as before. We get the two and the five coming in and the two pops out here and the five pops out on the right hand side because the two is the smallest one. So we'll do the second row. So what happens? So we've got a two and a three coming in. So the two is the smallest. It will come out at the bottom and the three is the biggest. So it comes out on the right hand side. And then we go over to this box in the row. We've got a three and a five. So the three comes out the bottom and the five comes out the right hand side. So what's actually happened is the second row has taken the two and the five which are already in the right order because the first box did that for us and it's taken the three and it's put the three into the correct place. So maybe if I use a different color here. You can see this. So I've got a two and a five here. And then I've got a three coming in on the left hand side. And what the second row does is it puts the three into the right place. So at the bottom you get two, three, and five. So what the second row has done is inserted this number into the right place. And let's see what happens with the next row. So then we've got a one and a two coming in. So the one comes out the bottom cuz it's the smallest. And then the two comes out here. And we've got a two and a three. So we get the two and the three. And then we've got a three and a five. And the three is the smallest. So it comes out here. What you see is exactly the same thing has happened again. We had a one here. And then we had two, three, and five, which has already been sorted by the grid above us. And then this row here is just going to put the one into the right place. What's popped out the bottom of this grid is 1 2 3 and five. So if we just complete the picture, we'll get the expected result. So we've got a one and a four. So the one is the smallest, so it pops out at the bottom. And the four goes around here. Then we've got a two and a four. So the two pops out and the four and the three and the four. So three is smallest. And then we get the four and the five. This sorting method when you think about the rows rather than the columns is called insertion sort. And it's called insertion sort because each of these rows just inserts a number into the correct position in a sorted sequence. So for example, if we look at the bottom row here, the input on the top is a sorted sequence. 1 2 3 and five. Those are in the right order. And all the bottom row is doing is putting this four into the correct position so that we get one two three four and five. Previously we had exactly the same picture and we said that's selection sort. If you view it in terms of the columns and if you take the same picture now and view it in terms of the rows this way around then we get insertion sort. So for me this is a bit of magic. Uh I've been doing computer science for a long long time. uh I was thinking about how to teach sorting algorithms to my students a few years ago and I came up with this pictorial uh idea and I didn't realize until then that insertion sort and selection sort are exactly the same thing but you only see this if you look at it in the right way using the pictures basically it's all comes all down to perspective right yes exactly it's it's this if you look at stuff in the right way then you can see things that you couldn't see before so if you write programs to do insertion sort or selection sort the kind of structure here which is the same is being is being kind of hidden from you. But if you just draw some simple pictures and forget all your fancy computing, then you end up with an observation which is quite interesting. Have you come across anything else that that is kind of a similar? Have you have you gone through other sort of algorithms and thought thought about them this way? Yeah. So a colleague of mine from Oxford a few years ago, we I was showing him this to him and and he said, "Oh, we we can write a paper about this." So we we tried to look at like quick sort and merge sort and see if they were had the same kind of duality, but it didn't quite work out at the time. and and we kind of gave up writing the paper about it. But um I think this is just a simple observation in its own right here. It's very difficult for a firewall or something to notice this because these are valid HTTP requests. They're just super slow. And um you know, maybe I've just got a really bad internet connection. Maybe.

Original Description

Two different sorting algorithms are actually the same. Professor Graham Hutton explains. Note from Professor Hutton: It's great to see all the discussions here! To clarify a point raised in some of the comments, the video shows that if you consider the essential underlying idea of selection sort and insertion sort, in terms of how they decompose the problem of sorting, then they perform the same operations but in different orders. Using the picture in the video, selection sort proceeds column at a time and insertion sort proceeds row at a time, but they both perform the same 'triangle' of operations in the end. A particular implementation of these sorting methods may optimise the process in some way, e.g. in a sequential implementation each row can stop comparing numbers once the point of insertion is found. But this is an implementation detail that is dependent on the computational model used, such as sequential versus parallel, or imperative versus functional, rather than part of the essential means by which the sorting methods work. Slow Loris Attack: https://youtu.be/XiFkyR35v2Y Cracking Windows by Atom Bombing: https://youtu.be/rRxuh9fp7QI Quantum Computing: https://youtu.be/BYx04e35Xso Babbage's Analytical Engine: https://youtu.be/5rtKoKFGFSM Quicksort: https://youtu.be/XE4VP_8Y0BU Getting Sorted: https://youtu.be/kgBjXUE_Nwc http://www.facebook.com/computerphile https://twitter.com/computer_phile This video was filmed and edited by Sean Riley. Computer Science at the University of Nottingham: http://bit.ly/nottscomputer Computerphile is a sister project to Brady Haran's Numberphile. More at http://www.bradyharan.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Computerphile · Computerphile · 0 of 60

← Previous Next →
1 Follow the Cookie Trail - Computerphile
Follow the Cookie Trail - Computerphile
Computerphile
2 EXTRA BITS - Follow the Cookie Trail - Computerphile
EXTRA BITS - Follow the Cookie Trail - Computerphile
Computerphile
3 Musical Floppy Drives - Computerphile
Musical Floppy Drives - Computerphile
Computerphile
4 The Hair Algorithm - Computerphile
The Hair Algorithm - Computerphile
Computerphile
5 Getting Sorted & Big O Notation - Computerphile
Getting Sorted & Big O Notation - Computerphile
Computerphile
6 Quick Sort - Computerphile
Quick Sort - Computerphile
Computerphile
7 Hyper History and Cyber War - Computerphile
Hyper History and Cyber War - Computerphile
Computerphile
8 Entropy in Compression - Computerphile
Entropy in Compression - Computerphile
Computerphile
9 Original Elite on the BBC B - Computerphile
Original Elite on the BBC B - Computerphile
Computerphile
10 IP Addresses and the Internet - Computerphile
IP Addresses and the Internet - Computerphile
Computerphile
11 A Career in Video Games - Computerphile
A Career in Video Games - Computerphile
Computerphile
12 Error Detection and Flipping the Bits - Computerphile
Error Detection and Flipping the Bits - Computerphile
Computerphile
13 Programming BASIC and Sorting - Computerphile
Programming BASIC and Sorting - Computerphile
Computerphile
14 Birthplace of the World Wide Web - Computerphile
Birthplace of the World Wide Web - Computerphile
Computerphile
15 Punch Card Programming - Computerphile
Punch Card Programming - Computerphile
Computerphile
16 Programming Paradigms - Computerphile
Programming Paradigms - Computerphile
Computerphile
17 CERN Computing Centre (and mouse farm) - Computerphile
CERN Computing Centre (and mouse farm) - Computerphile
Computerphile
18 Error Correction - Computerphile
Error Correction - Computerphile
Computerphile
19 Home-Made Code - Computerphile
Home-Made Code - Computerphile
Computerphile
20 Security of Data on Disk - Computerphile
Security of Data on Disk - Computerphile
Computerphile
21 Gesture Controls - Computerphile
Gesture Controls - Computerphile
Computerphile
22 How Intelligent is Artificial Intelligence? - Computerphile
How Intelligent is Artificial Intelligence? - Computerphile
Computerphile
23 Encryption and Security Agencies - Computerphile
Encryption and Security Agencies - Computerphile
Computerphile
24 Virtual Machines Power the Cloud - Computerphile
Virtual Machines Power the Cloud - Computerphile
Computerphile
25 Hacking Websites with SQL Injection - Computerphile
Hacking Websites with SQL Injection - Computerphile
Computerphile
26 How Huffman Trees Work - Computerphile
How Huffman Trees Work - Computerphile
Computerphile
27 Cracking Websites with Cross Site Scripting - Computerphile
Cracking Websites with Cross Site Scripting - Computerphile
Computerphile
28 Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Computerphile
29 Texting Cabbage with a Recorder - Computerphile
Texting Cabbage with a Recorder - Computerphile
Computerphile
30 Hashing Algorithms and Security - Computerphile
Hashing Algorithms and Security - Computerphile
Computerphile
31 How YouTube Works - Computerphile
How YouTube Works - Computerphile
Computerphile
32 How NOT to Store Passwords! - Computerphile
How NOT to Store Passwords! - Computerphile
Computerphile
33 A New Golden Age of Video Games - Computerphile
A New Golden Age of Video Games - Computerphile
Computerphile
34 A Universe of Triangles - Computerphile
A Universe of Triangles - Computerphile
Computerphile
35 Cross Site Request Forgery - Computerphile
Cross Site Request Forgery - Computerphile
Computerphile
36 The True Power of the Matrix (Transformations in Graphics) - Computerphile
The True Power of the Matrix (Transformations in Graphics) - Computerphile
Computerphile
37 The Great 202 Jailbreak - Computerphile
The Great 202 Jailbreak - Computerphile
Computerphile
38 EXTRA BITS - Printing and Typesetting History - Computerphile
EXTRA BITS - Printing and Typesetting History - Computerphile
Computerphile
39 Triangles to Pixels - Computerphile
Triangles to Pixels - Computerphile
Computerphile
40 The Problem with Time & Timezones - Computerphile
The Problem with Time & Timezones - Computerphile
Computerphile
41 The Visibility Problem - Computerphile
The Visibility Problem - Computerphile
Computerphile
42 Lights and Shadows in Graphics - Computerphile
Lights and Shadows in Graphics - Computerphile
Computerphile
43 The Penguin Barcode - Computerphile
The Penguin Barcode - Computerphile
Computerphile
44 Typesetters in the '80s - Computerphile
Typesetters in the '80s - Computerphile
Computerphile
45 The Font Magicians - Computerphile
The Font Magicians - Computerphile
Computerphile
46 The Little Mac with the Big Bite - Computerphile
The Little Mac with the Big Bite - Computerphile
Computerphile
47 EXTRA BITS - More on the Original Mac at 30 - Computerphile
EXTRA BITS - More on the Original Mac at 30 - Computerphile
Computerphile
48 XP to Ubuntu with an 8yr old Hacktop - Computerphile
XP to Ubuntu with an 8yr old Hacktop - Computerphile
Computerphile
49 EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
Computerphile
50 EXTRA BITS - Making a Bootable USB in Linux - Computerphile
EXTRA BITS - Making a Bootable USB in Linux - Computerphile
Computerphile
51 EXTRA BITS - Installing Ubuntu Permanently - Computerphile
EXTRA BITS - Installing Ubuntu Permanently - Computerphile
Computerphile
52 The Dawn of Desktop Publishing - Computerphile
The Dawn of Desktop Publishing - Computerphile
Computerphile
53 What is Bootstrapping? - Computerphile
What is Bootstrapping? - Computerphile
Computerphile
54 Reverse Polish Notation and The Stack - Computerphile
Reverse Polish Notation and The Stack - Computerphile
Computerphile
55 Home-Made Z80 Retro Computer - Computerphile
Home-Made Z80 Retro Computer - Computerphile
Computerphile
56 Should Everybody Learn to Code? - Computerphile
Should Everybody Learn to Code? - Computerphile
Computerphile
57 Programming in PostScript - Computerphile
Programming in PostScript - Computerphile
Computerphile
58 Heartbleed, Running the Code - Computerphile
Heartbleed, Running the Code - Computerphile
Computerphile
59 YouTube's Secret Algorithm - Computerphile
YouTube's Secret Algorithm - Computerphile
Computerphile
60 YouTube Search & Discovery - Computerphile
YouTube Search & Discovery - Computerphile
Computerphile

This video teaches how selection sort and insertion sort can be viewed as the same algorithm when considering their underlying sorting networks, and why this matters for understanding algorithmic complexity and trade-offs. By visualizing these algorithms as a series of sorting networks, viewers can gain a deeper understanding of how they work and how they can be optimized. The video also covers the basic building blocks of sorting and how they can be used to create more complex sorting algorithm

Key Takeaways
  1. Draw a box with four sides to represent a basic sorting block
  2. Create a sorting network by wiring together multiple instances of the basic sorting block in a triangular shape
  3. Push a set of numbers through the sorting network to see the sorted output
  4. Implement selection sort using a grid with numbers being pushed through one column or row at a time
  5. Implement insertion sort by repeatedly finding the smallest element from the unsorted part of the list and swapping it with the first unsorted element
💡 Selection sort and insertion sort are essentially the same algorithm when viewed from different perspectives, and understanding this can help with optimizing and analyzing algorithmic complexity

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →