We're going to start out with a very simple problem.
我们从一个非常简单的问题开始
It's one of the oldest problems that has been studied in algorithms,is the problem of sorting.
这是一个在算法学习中最古老的问题,就是排序问题
We're going to actually study this for several lectures
我们将用几个课时来介绍它
because sorting contains many algorithmic techniques.
排序包含了很多基本的算法
The sorting problem is the following
举一个排序的例子
We have a sequence a_a, a_2 up to a_n of numbers as input
我们输入一组序列a1, a2, 直到 an
And our output is a permutation os those numbers.
按照需求重新排序后作为输出
A permutation is a rearrangement of the numbers.
排序是指对一组数字的重新排序
Every numbers appears exactly once in the rearrangement such that,
排序后每个数字出现且仅出现一次,使得...
I sometimes use a dollar sign to mean "such that,"
我有时会用 & 符号表示“使得”
such that a_1 is less than or equal to a_2 prime.
使得 a1' <= a2' <= ...
Such that they are monotonically increasing in size.
使得它们的大小单调递增
Take a bunch of numbers, put them in order,
输入一组数字,将它们按一定顺序排列
Here's an algorithm to do it. It's called insertion sort.
这里有一种算法来完成这项工作,称为插入排序
And we will write this algorithm in what we call pseudocode.
我们将用位代码来描述此算法
It's sort of programming language, except it's got English in there often.
伪代码与编程语言相似,只是经常会包含一些英语在里面
And it's just a shorthand for writing for being precise.
伪代码能够让我们更容易理解算法所要表达的意思
So this sorts A from 1 to n.
从第一项到第 n 项排序
And here is the code for it.
下面是它的代码
This is we call pseudocode.
这就是我们所说的伪代码
And if you don't understand the pseudocode.
如果你不理解伪代码
then you should ask questions about any of the notations.
你可以就不理解的符号进行提问
You will start to get used to it as wo go on.
今后你们将习惯使用伪代码
One thing is that in the pseudocode we use indentation,
值得一提的是我们在伪代码中使用缩进
where in most languages they have some king of begin-end delimiters
相当于在大多数语言中标注开始和结束的分隔符
like curly braces or something in JAVA or C, for example
好比JAVA和C中使用大括号
We just use indentation .
伪代码中只使用缩进
The whole idea of the pseudocode is to try to get the algorithms as short as possible
伪代码的目的是让算法的表达尽量简洁
while still understanding what the individual steps are .
同时表达第一步的含义
In practice, there actually have been languages
实际上,还真过一些编程语言
that sue indentation as a means of showing the nesting of things
使用缩进来表示嵌套的意义
(比如 Python, MIT600 课程使用的就是 Python语言)
It's generally a bad idea, because if things go over one page to another,
这不是一个好注意,比如当代码换页时
for example, you cannot fell what level of nesting it is.
你很难知道自己在哪一个嵌套的层级
Whereas, with explicit braces it's much easier to tell
而使用大括号会清晰了很多
So, there are reasons
所以,这就是为什么说
why this is a bad notation if you were doing software engineering.
在软件工程中用缩进表示嵌套并非好事的原因
But it’s a good one for us
但对于研究算法
because it just keeps thing short and makes fewer thing to write down.
它是不错的选择,因为这样我们的代码简短、容易书写
So, this is insertion sort.
这就是插入排序
Let's try to figure out a little bit what this does.
处我们来一步一步的看看它是如何工作的
It basically takes an array A and at any point the thing to understand is,
首先构造数组A,需要理解的是
we're setting basically, we're running the outer loop from j is 2 to n
设置外部循环设置把, j 从2 到 n 递增
and the inner loop that starts at j minus 1
而内部循环设置开始于 j - 1
and then goes down until it's zero
并递减到0
Basically, if we look at any point int the algorithm,
基本算法中的任意一步是这样的
we essentially are looking at some element here j
我们找到 j
A of j, the jth elements.
数组A的第j个位置
And what we do essentially is we pull a value out here that we call the key.
我们把这个位置的值提取出来,在这里称之为键值(key)
And at this point the important thing to understand,
在这里有一点很重要
and we'll talk more about this in recitation on Friday,
我们也会在周五的助教课中再次谈到
is that there is an invariant
即存在一个常量
that is being maintained by this loop each time through.
每次循环后这个常量保持不变
And the invariant is that this part of the array is sorted.
常量就是数组已经被排序的这部分
And the goal each time through the loop is to increase,
每次循环的目的是完成增量
is to add one to the length of the things that are sorted
就是使已排序部分的长度增加1
And the way we do that is we pull out the key
实现方法是提取当前循环中位置为 j 的数字键
and we just copy values up like this.
然后我们就一会这样复制值
And keep copying up until we find the place where this key goes,
一步步把前面的值抄到下一位上,直到找到此键合适的位置
and then we inset it in that place.
然后我们插入键值
And that's why it's called insertion sorts.
这就是称之为插入排序的原因
We just fort of move the things
我们在数组中移动元素的位置
copy the things up until we find where it goes,
持续复制这些元素直到我们找到它们的适当位置
and then we put it into place.
并将它们放置到这些位置中去
And now we have it from A from one to j is sorted,
至此,从数组A中的数字已经从 1 到 j 的位置排列好
and now we can work on j plus one
现在我们可以来执行 j + 1
Let's give an example of that.
这里有一个具体的实例
Imagine we are doing 8,2,4,9,3,6.
给定数组8,2,4,9,3,6