Sorting Secret - Computerphile

Computerphile
18 Nov 201609:45

Summary

TLDRIn this talk, two popular sorting algorithms, Selection Sort and Insertion Sort, are explained through a visual method that reveals their similarity. The presenter uses simple diagrams to show that while these algorithms are often thought of as distinct, they are actually the same when viewed from different perspectives. By breaking down sorting into easy-to-understand steps, the presenter demonstrates how numbers are ordered through basic comparisons, making the complexities of sorting algorithms more accessible. This discovery highlights the importance of viewing problems from new angles to uncover hidden connections.

Takeaways

  • 🔄 The script discusses two fundamental sorting algorithms: Selection Sort and Insertion Sort, and reveals a surprising connection between them.
  • 🎨 The presenter uses a visual approach to explain sorting, emphasizing that no coding or computer science knowledge is required to understand the concepts.
  • 📊 The script introduces a 'sorting box' concept, a simple model for sorting two numbers at a time, which serves as a building block for the sorting algorithms.
  • 🔲 The sorting process is visualized as a grid of sorting boxes, where numbers are sorted by passing them through the grid column by column for Selection Sort.
  • 📏 The same grid can be viewed differently to represent Insertion Sort, where the sorting is done row by row, inserting each number into its correct position in a sorted sequence.
  • 🤔 The script challenges the conventional wisdom that Selection Sort and Insertion Sort are distinct, suggesting they are essentially the same algorithm viewed from different perspectives.
  • 👨‍🏫 The presenter, Graham, shares a personal anecdote about discovering this concept while teaching, highlighting the importance of different perspectives in understanding algorithms.
  • 🤝 The script includes a dialogue between Graham and Sean, where they discuss the broader implications of this observation and its potential for teaching sorting algorithms.
  • 🧠 The discussion touches on the idea of 'perspective' as a key to understanding complex computer science concepts, suggesting that simple visualizations can lead to profound insights.
  • 📚 The script mentions an attempt to apply this visual approach to other sorting algorithms like Quicksort and Merge Sort, indicating that while the method is insightful, it may not apply universally.

Q & A

  • What is the main focus of the transcript?

    -The main focus of the transcript is to explain two well-known sorting methods, Selection Sort and Insertion Sort, and demonstrate how they are fundamentally the same when viewed from different perspectives.

  • How does the speaker describe the process of Selection Sort?

    -The speaker describes Selection Sort as a process where each column of a sorting grid selects the smallest number from the remaining numbers and pushes it to the bottom, eventually resulting in a sorted list.

  • How is Insertion Sort different from Selection Sort in terms of visualization?

    -While Selection Sort focuses on columns selecting the smallest number, Insertion Sort focuses on rows inserting numbers into the correct position in an already sorted sequence. Both processes result in the same sorted output but are viewed from different perspectives.

  • What example does the speaker use to explain the sorting process?

    -The speaker uses the numbers 5, 2, 3, 1, and 4 in random order to demonstrate how both Selection Sort and Insertion Sort can sort these numbers into ascending order.

  • What is the basic building block used in the explanation?

    -The basic building block is a simple sorting box that takes two numbers, outputs the smallest number at the bottom, and the largest number at the right-hand side, regardless of the input order.

  • Why does the speaker believe that Selection Sort and Insertion Sort are the same?

    -The speaker believes that Selection Sort and Insertion Sort are the same because, when visualized, the sorting steps are identical whether viewed by columns or rows, suggesting a hidden structural similarity between the two methods.

  • What is the significance of visualizing sorting algorithms according to the speaker?

    -According to the speaker, visualizing sorting algorithms helps to uncover hidden connections and structural similarities that are not immediately obvious when simply writing or coding the algorithms.

  • How does the speaker demonstrate the progression of sorting in Selection Sort?

    -The speaker demonstrates the progression of Selection Sort by showing how the smallest number in each column moves to the bottom, gradually leading to a fully sorted list after processing each column.

  • What does the speaker say about their discovery regarding these sorting methods?

    -The speaker shares that they discovered the similarity between Selection Sort and Insertion Sort while trying to teach sorting algorithms. This observation came from visualizing the sorting process rather than relying on coding or complex computer science concepts.

  • Why does the speaker believe that others might not have noticed this similarity before?

    -The speaker believes others might not have noticed this similarity because the structure of these algorithms is often hidden in the code. By focusing on simple visual representations instead of programming, this hidden duality between the methods becomes apparent.

Outlines

00:00

🔢 Understanding Sorting Algorithms: Selection Sort vs Insertion Sort

In this introduction, the speaker sets the stage for discussing two popular sorting algorithms: Selection Sort and Insertion Sort. People often see these as distinct, but the speaker reveals that they are, in fact, the same when visualized in a specific way. Using illustrations, the speaker will demonstrate that both sorting methods share an underlying structure. The aim is to convey this through simple visual representations without requiring any programming knowledge.

05:02

📦 Visualizing Sorting with a Simple Box

The speaker explains sorting by visualizing a process using a 'sorting box' for two numbers. This box compares two inputs and outputs the smaller one at the bottom and the larger one on the right, regardless of input order. By connecting several of these boxes into a triangle-shaped network, the speaker demonstrates how numbers can be sorted. A specific example using five random numbers (5, 2, 3, 1, and 4) is processed to illustrate how the smallest number is selected by the network and 'ripples' down to the bottom.

🔁 Visualizing Selection Sort: Column by Column

In this section, the speaker describes how the triangle of boxes visually represents Selection Sort. By treating the columns one at a time, each column selects the smallest number and moves it to the bottom. The remaining numbers are progressively sorted in the same manner, with the smallest numbers moving down each column. This visual approach highlights how the Selection Sort algorithm works by selecting and positioning the smallest numbers until the list is fully sorted.

🖼️ Reinterpreting the Same Diagram: Insertion Sort

Here, the speaker reinterprets the same diagram used for Selection Sort, this time focusing on rows instead of columns. In this new perspective, the diagram shows how Insertion Sort works. Each row inserts a number into its correct position in a partially sorted sequence. The process continues row by row until all numbers are in the correct order. This row-based visualization demonstrates that Insertion Sort and Selection Sort are identical when viewed from different perspectives.

🔄 Comparing Selection and Insertion Sort: Two Sides of the Same Coin

The speaker concludes by emphasizing the dual nature of these sorting algorithms. By viewing the process either through columns (Selection Sort) or rows (Insertion Sort), the same sorting behavior is observed. This revelation, based on perspective, showcases the deep connection between the two methods. The speaker reflects on the simplicity and elegance of this discovery, suggesting that certain complexities in computer science can be unraveled by shifting perspectives.

🔍 Exploring Further: Other Algorithms and Dualities

In a closing conversation, the speaker shares how this insight sparked curiosity about other algorithms like Quicksort and Merge Sort. Despite some exploration, the same duality found between Selection Sort and Insertion Sort did not emerge in those cases. The speaker reflects on this journey of discovery and encourages further thinking about visualizing algorithms in new and intuitive ways.

Mindmap

Keywords

💡Sorting

Sorting refers to the process of arranging items in a specific order, typically numerical or alphabetical. In the context of the video, sorting is the main theme, focusing on algorithms that organize numbers into ascending or descending order. The script discusses two sorting methods, Selection Sort and Insertion Sort, and reveals a surprising connection between them.

💡Selection Sort

Selection Sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the order desired) element from the unsorted portion of the list and moving it to the beginning of the sorted section. The video script uses a visual approach to demonstrate how Selection Sort operates, showing that each column in a sorting grid selects the smallest number from the remaining unsorted elements.

💡Insertion Sort

Insertion Sort is another simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. The video script reveals that Insertion Sort can be seen as the same process as Selection Sort, but viewed from a different perspective, focusing on rows instead of columns.

💡Perspective

Perspective in this context refers to the way one views or interprets something. The video script emphasizes that the difference between Selection Sort and Insertion Sort is merely a matter of perspective. By changing the way the sorting process is visualized, the same underlying mechanism can be seen as two different algorithms.

💡Algorithm

An algorithm is a set of rules or steps used to solve a problem or perform a computation. In the video, algorithms are the methods used to sort numbers. The script discusses how different algorithms can be visualized and compared, highlighting the simplicity and elegance of Selection Sort and Insertion Sort.

💡Ascending Order

Ascending order is a sequence where each element is greater than or equal to the one before it. The video script mentions ascending order as the desired outcome of the sorting process, where numbers are arranged from smallest to largest.

💡Descending Order

Descending order is the reverse of ascending order, where each element is less than or equal to the one before it. While the video script focuses on ascending order, the concept of descending order is implicitly understood as the opposite sorting arrangement.

💡Computer Science

Computer science is the study of computers and computing, including their theory, design, development, and application. The video script suggests that understanding sorting algorithms, like those discussed, does not require extensive knowledge of computer science, emphasizing the accessibility of these concepts.

💡Visualization

Visualization in the context of the video refers to the use of diagrams or drawings to represent abstract concepts or processes. The script uses visualization as a tool to demonstrate the equivalence between Selection Sort and Insertion Sort, making the sorting process more intuitive and easier to understand.

💡Duality

Duality in this context means the existence of two forms or aspects that are fundamentally the same but appear different. The video script explores the duality between Selection Sort and Insertion Sort, showing that they are essentially the same algorithm viewed from different angles.

💡Magic

In the video script, 'magic' is used metaphorically to describe the surprising and enlightening realization that two seemingly different sorting algorithms are actually the same when viewed from different perspectives. It conveys the sense of wonder and discovery in understanding the underlying simplicity of these algorithms.

Highlights

The lecture discusses the surprising equivalence between Selection Sort and Insertion Sort.

Sorting methods are often perceived as distinct, but the lecture reveals a hidden connection.

Visual illustrations, rather than coding, are used to demonstrate the sorting methods.

The concept of a 'sorting box' for two numbers is introduced as a basic building block.

Sorting is visualized as a network of these 'sorting boxes' arranged in a triangle.

The process of sorting is shown by pushing numbers through the network column by column.

Selection Sort is explained as a method where each column selects the smallest number.

The same sorting network is then viewed row by row to illustrate Insertion Sort.

Insertion Sort is described as inserting each number into the correct position in a sorted sequence.

The lecture emphasizes the importance of perspective in understanding sorting algorithms.

The speaker shares a personal anecdote about the 'Eureka' moment of discovering the connection.

The simplicity of the observation is highlighted, contrasting with complex computer science concepts.

The lecture concludes with a discussion on the potential for similar insights in other sorting algorithms.

The speaker's colleague from Oxford is mentioned as a collaborator in exploring this concept.

Attempts to apply this perspective to Quicksort and Merge Sort are mentioned, with mixed results.

The lecture concludes by emphasizing the value of simple observations in complex fields.

Transcripts

play00:00

Today we're going to talk about sorting. And in particular, we're going to talk

play00:04

about two very well-known sorting methods. One is called Selection Sort; the

play00:09

other one is called Insertion Sort. And when people learn about sorting methods

play00:14

they usually learn about these as being two completely different ideas. You have

play00:19

selection sort on the one hand and insertion sort on the other hand. And what I'm going

play00:22

to show you today is that these are actually the same; you can regard them as

play00:26

being equal. The trick is that you just draw pictures of them. You don't need any

play00:34

coding to see this; you don't need to know any fancy stuff. In fact, you don't

play00:37

need to know anything about computer science at all. If you just draw a

play00:40

picture of selection sort in the right way, and a picture of insertion sort in

play00:44

the right way, you see that they're actually the same thing. And this is kind

play00:48

of a secret about sorting methods that even people like me, who have been doing

play00:51

computer science for many years - most people - or hardly anybody - actually

play00:55

knows this little trick. We'll start off by kind of refreshing ourselves with

play00:59

pictures about what sorting actually is. So, let me draw a box. What we're going to

play01:04

do is put some numbers into this box. It doesn't matter how many numbers - everything

play01:08

which I'm going to show you is completely general - but we'll do it with five.

play01:10

So suppose we do a 5 and a 2 and a 3 and a 1 and a 4. These are the first five numbers, in

play01:18

some kind of random order here. But the key thing is they're not sorted, they're not in

play01:21

ascending order. What we'd like our sorting box to do is

play01:25

give us the same numbers out, but in sorted order. So we'd like to have 1,

play01:30

2, 3, 4 and 5 coming out. Let's start to think about how we might

play01:35

construct a little program which implemented this kind of sorting

play01:39

procedure here. The most basic building block which you can think of is just a

play01:44

little box with four sides. What this box is going to do is on the left-hand side

play01:48

and on the top side you're going to have two numbers come in. And then, at the

play01:53

bottom, the smallest number is going to pop out. And on the right-hand side the

play01:58

largest number is going to pop out. So you can think of this as being like

play02:01

little sorting box for only two numbers. So, for example, if we put the number 1

play02:06

in on the left-hand side and the number 2 in on the top, then what our little

play02:11

sorting box would do is give us the smallest one out the bottom

play02:15

and it will give us the biggest one out the right-hand side. So, this is a sorting

play02:19

box for two numbers. And it doesn't matter the order in which the two numbers come in.

play02:23

So if I swap this around and if I had the 2 coming in here and a 1 coming in

play02:27

here, it doesn't make any difference. The

play02:29

smallest one is going to pop out the bottom, and the biggest one is going to

play02:33

pop out the right-hand side. So the game here is we've got this basic building block

play02:37

and we want to kind of plug these together, just with pictures, and see

play02:41

how you can build a little sorting program

play02:43

The trick is that you just build a little triangle of these boxes. So let's

play02:47

put a bunch of these things together. There's a little triangle of these boxes

play02:50

and we're going to just wire them together in a very straightforward way

play02:54

We'll just draw the obvious little links between them. And each of these boxes

play02:58

just sorts two numbers, like we saw a few moments ago. So let's take our little

play03:03

example and just push it through this little sorting network and actually

play03:06

see what happens. So I think the numbers we had were 5, 2, 3, 1

play03:12

and 4. So, out the bottom, we hope to get 12345. So let's see what happens.

play03:18

So we're going to treat the first column first of all and we'll see what happens.

play03:22

So it's really simple. So, you've got the 2 and the 5 coming into the first box.

play03:26

So the smallest number pops out the bottom. So that's the 2, and the biggest

play03:31

number comes out the right-hand side - so that's the 5. And then we do the same with

play03:35

the second box in the column. So we've got 2 coming in, and a 3 coming in, so

play03:38

the smallest number pops out the bottom. And the biggest number comes out

play03:42

the right-hand side. And we just do the same again. Then we got a 1 and a 2 - so the 1

play03:46

comes out the bottom, and the 2 comes out the right-hand side. And then we've got 4

play03:49

and a 1 - the 1 pops out the bottom and the 4 comes out the right-hand side.

play03:54

So what you see is that the smallest number, which in this case is 1, has

play03:59

kind of rippled its way down to the bottom. So what's happened is this first

play04:03

column has selected the smallest number. And it's quite easy to see that that's

play04:08

the case, because the top box selects the smallest from the two numbers it's given,

play04:12

and then the second box selects the smallest numbers from the two numbers

play04:14

it's given, and so on and so on. So kind of - the smallest number is going to

play04:18

ripple its way down to the bottom - so selecting the smallest one. Then we do

play04:22

the same with the remaining columns, and

play04:24

we won't be surprised with what happens. So we've got a 3 coming in and a 5

play04:28

coming in. So the 3 will pop out and the 5 will pop out here. Then we got a 2

play04:33

and the 3, so the 2 is smaller so it pops out. And the 3 comes out here. And then we got 2 and

play04:38

a 4, and the 2 is the smallest. And then the 4 pops out the other side. You see

play04:42

what's happened again: from the remaining numbers 5, 3, 2 and 4, the second column

play04:47

has selected the smallest one. And then, if we just complete this, it's obvious what's

play04:51

going to happen. We get a 3 here and a 5 here. 3,4,4,5. So what you see is that

play04:57

our little sorting grid has taken five numbers, in a mixed-up order, and just by

play05:02

pushing them through, one column at a time, we've ended up with the numbers in

play05:06

the correct order. And this is known as Selection Sort, because each column just

play05:11

selects the smallest number from what is left.

play05:15

OK? So nice and easy. You don't need to know anything about computer science, anything

play05:18

about algorithms, anyone can understand what's going on with selection sort here.

play05:21

But, you can actually view this picture in another way. So let me re-draw the same

play05:27

picture. So I just wire it up, in exactly the same way as before. And we'll push

play05:32

exactly the same five numbers through. So we started off with a 5,2,3,1 and a 4.

play05:39

So, what we did last time is we treated it in terms of the columns. But actually you

play05:44

can do exactly the same thing in terms of the rows. So we'll do one row at a

play05:47

time, and actually see what happens. So, if we consider the first row, it's the

play05:51

same as before. We get the 2 and the 5 coming in. And the 2 pops out here,

play05:55

and 5 pops out on the right-hand side, because the 2 is the smallest one.

play05:59

So we'll do the second row. So what happens? So, we've got a 2 and a 3 coming in.

play06:03

So the 2 is the smallest; it'll come out the bottom. And 3 is the biggest so it comes

play06:07

out on the right-hand side. And then we go over to this box in the row. We've got

play06:11

a 3 and a 5. So, the 3 comes out the bottom, and the 5 comes out the

play06:16

right-hand side. So what's actually happened is the

play06:18

second row has taken the 2 and the 5, which are already in the right

play06:22

order, because the first box did that for us. And it's taken the 3 and it's

play06:26

put 3 into the correct place. So maybe if I use a different colour here,

play06:29

you can see this. So I've got a 2 and a 5 here. And then

play06:33

I've got a 3 coming in on the left-hand side. And what the second row

play06:37

does, is it puts the 3 into the right place. So, out the bottom, you get 2, 3 and 5.

play06:43

So what the second row has done is inserted this number into the right

play06:47

place. And let's see what happens with the next row. So then we've got a 1 and a 2

play06:50

coming in. So the 1 comes out the bottom, because it's the smallest. And then the

play06:54

2 comes out here. And we've got a 2 and a 3 Se we get the 2 and the 3. And then we've got

play06:59

a 3 and a 5. And the 3 is the smallest, so it comes out here. What you see is exactly

play07:03

the same thing has happened again. We had a 1 here and then we had 2, 3

play07:07

and 5, which has already being sorted by the grid above us. And then this row

play07:13

here is just going to put the 1 into the right place.

play07:15

What's popped out the bottom of this grid is 1, 2, 3 and 5. So we just complete

play07:20

the picture. We'll get the expected results. So we've got a 1 and a 4. So, the 1 is the

play07:24

smallest so it pops out the bottom. And the 4 goes around here. And we've got a 2 and

play07:28

a 4. So the 2 pops out and the 4. And the 3 and the 4. So the 3 is smallest and then

play07:33

we get the 4 and the 5. This sorting method, when you think about the

play07:37

rows rather than the columns, is called Insertion Sort. And it's called

play07:42

insertion sort because each of these rows just inserts a number into the

play07:47

correct position in a sorted sequence. So, for example, if we look at the bottom row

play07:51

here, the input on the top is a sorted sequence - 1, 2, 3 and 5 -

play07:56

those are in the right order. And all the bottom row is doing, is putting this 4

play08:00

into the correct position so that we get 1, 2, 3, 4 and 5.

play08:04

Previously, we had exactly the same picture, and we said that's selection sort,

play08:09

if you view it in terms of the columns. And if you take the same picture now, and

play08:15

view it in terms of the rows, this way around, then we get insertion sort. So, for

play08:20

me this is a bit of magic. I've been in computer science for a long, long time.

play08:24

I was thinking about how to teach sorting algorithms to my students a few years ago,

play08:27

and came up with this pictorial idea, and I didn't realize until then that

play08:33

insertion sort and selection sort are exactly the same thing. But you only see

play08:37

this if you look at it in the right way using the pictures.

play08:40

>> Sean: Basically it all comes down to perspective, right? >> Graham: Yes, exactly, it's ...

play08:44

if you look at stuff in the right way then you can see things that you

play08:47

couldn't see before. So, if you write programs to do

play08:50

insertion sort or selection sort, the kind of structure here, which is the same,

play08:53

is being kind of hidden from you. But if you just draw some simple

play08:56

pictures and forget all your fancy computing, then you end up with an

play09:00

observation which is quite interesting. >> Sean: Have you come across anything else that

play09:04

that is kind of similar? Have you gone through other sort of algorithms and

play09:07

thought about them this way? >> Graham: Yes a colleague of mine from Oxford a

play09:11

few years ago, I was showing him this to him. And he said: "Oh! we can write a paper

play09:15

about this!" So we tried to look at, like, Quicksort and Merge Sort, and see if they

play09:19

had the same kind of duality. But it didn't quite work out at the time. And we

play09:23

kind of gave up writing the paper about it. But, I think this is just a simple

play09:27

observation in its own right here.

Rate This

5.0 / 5 (0 votes)

関連タグ
Sorting AlgorithmsComputer ScienceEducational InsightAlgorithm VisualizationPerspective ShiftSorting TechniquesInsertion SortSelection SortCoding ConceptsVisual Learning
英語で要約が必要ですか?