Explain in five minutes
Dutch flag 10 minutes here defines two regions is greater than the region area is smaller than
30 minutes
The following figure is given a number to the last digit in general we as num (this will be followed by a few other random number and rows of fast random exchange so called fast random row)
This number is less than equal to put the left to the right place to put greater than the intermediate
Add this line to represent a random row fast swap represents a random location
Fast random row is the most common sort
55 minutes
Note Recursive behavior is not allowed on any project recursive loop behavior can become our own pressure that do not break the system to help us push
How into non-recursive?
1:16 begin to pile row
Heap structure is a complete binary tree complete binary tree is either a full binary tree (any non-leaf nodes of the child must be a full binary tree that is full of the bottom layer are leaf nodes)
From left to right or complete binary tree is a tree filled
In practice heap array can be used to implement an array can be understood as a complete binary tree binary tree is actually fill out our brain
By numerical relationship between the index can be calculated and the relationship between the left and right parent
Two kinds of heap large and small root root heap heap
Large stacks in the star complete binary tree root in any subtree of a maximum value of the head is small stack of any one root subtree is the minimum value for any head is such a subtree
How to convert an array of piles ?? 1:27
The establishment of large root heap
Code 1:35
heapInsert is inserted during the upward than the parent node if large exchanged
heapInsert is the process of establishing large root heap
1:38 When creating large root heap of complexity
When a number of points along the way add to the mix and the number of other related and not related
So a number of highly relevant and would add to the mix
A binary tree nodes is N, then so her height logN, so the establishment of this highly relevant log1 large root heap of complexity and node + log2 + .. logN-1 = NN nodes joined to become the complexity of large root heap is
O (N)
Heapify action is to pop up when the top of the stack and the last top of the stack arr [0] and then exchange the sinking operation heapify
heapify array which is when a value becomes small example here is smaller sank how to reconvert into large root heap explain when 1:45
这里由6->1
1这个值 找到他的左右两个孩子 找到左右两个孩子中最大的那个交换 同理交换后再次找到 左右两个孩子
代码讲解 1:50时
heapSize代表 我们可能是0~i范围上 进行限定范围 就是标记是否越界 进行heapify
0->heapSize-1这个范围上已经形成了堆 再往右就越界
我们index位置上面的值发生了变化 变小了 导致index应该下沉
总结 2:00时
我们可以认为堆是可以在数组上面伸缩的 注意我们这里还是没有讲堆排序 只是讲了堆这个结构
2:02时
有一个流 突出数 我们想找到流中随时突出的中位数 吐出的数是无序的
要求任何时候求出吐出所有数的中位数
我们需要使用堆结构
笨办法是 创建一个大的数组 然后就往数组里面存 算出中位数 收集的代价是O(1) 我们需要O(n*logn)的排序代价 而且是每次插入数都需要排序 浪费时间
解决办法:2:04时 准备两个堆大根堆和小根堆 具体实现看视频
贪心结构都是利用堆的 优先级队列就是堆而不是双向链表
2:20 这里可以先不用理解 因为还没有说比较器
这个就是整个的代码实现
堆排序就是利用堆结构完成的排序 2:20时
1)让数组变成大根堆
2)堆顶位置和最后一个进行交换 让堆的大小减1
6就相当于永远不动了 heapSize--
3)然后3做heapify的调整 然后依次作此操作
整个数组就从小到大就排完序了
代码2:25