由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 为什么quicksort会比heapsort快?
相关主题
A家,link all node in the same levG题,把binary tree里面的sibling节点连接起来
今天面试惨败,分享面经G家实习电面总结
一道面试题狗电面
这个check whether a binary tree is a BST 问题T第二轮面经
一个老题binary tree找 lowest common ancestor 的code (请教狗狗家onsite面经
讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径感觉Binary Tree Postorder Traversal的iterative是三种traversal中最难的
c语言实现TreeFeeG的一道考题
生成树其实我很想知道, 多少软工能25分钟内把heapsort写下
相关话题的讨论汇总
话题: int话题: heapsort话题: heaparray话题: rightchild
进入JobHunting版参与讨论
1 (共1页)
m********g
发帖数: 272
1
虽然worst-case quicksort是O(n^2), 但是on avarage, quicksort 为什么会比
heapsort 快呢?
c*********7
发帖数: 19373
2
移位少吧。
P****a
发帖数: 864
3
实战中和cache结合得好

【在 m********g 的大作中提到】
: 虽然worst-case quicksort是O(n^2), 但是on avarage, quicksort 为什么会比
: heapsort 快呢?

h**********d
发帖数: 4313
4
因为inner loop效率高
m********g
发帖数: 272
5
指的是哪一个inner loop?

【在 h**********d 的大作中提到】
: 因为inner loop效率高
h***o
发帖数: 30
6
Expected complexity是O(nlogn), partition很精简常数小, in-place, 每次逐个扫
数据
(cache miss少)

【在 m********g 的大作中提到】
: 虽然worst-case quicksort是O(n^2), 但是on avarage, quicksort 为什么会比
: heapsort 快呢?

s********x
发帖数: 914
7
// MaxHeap: heapArray starts from index 0
public static void heapSort(int[] a){
// Trickling Down in Place: start at node n/2-1, the rightmost node
with children
// O(n/2*log(n))
for (int i=(a.length/2-1) ; i>=0 ; i--)
trickleDown(a,i, a.length);
// remove max from heap and insert it at the end
// O(n*log(n))
for (int i= a.length - 1; i> 0; i--){
int max = a[0];
a[0] = a[i];
trickleDown(a,0, i);
a[i] = max;
}

}
// the number of levels L in a binary tree equals log2(n+1), where n is the
number of nodes.
// trickleUp() takes time proportional to log2(n), and trickleDown()
takes
// somewhat more it needs two comparisons
private static void trickleUp(int[] heapArray, int i){
if (i>=heapArray.length)
System.err.println("index exceeding the size of the heap");
int parent = (i - 1)/2;
while( i > 0 && heapArray[parent]< heapArray[i]) {
swap(heapArray, parent, i);
i = parent;
parent = (parent - 1)/2;
}
}

private static void trickleDown(int[] a, int i, int size){
// size = a.length
// the last element is a[size-1] and its parent is a[size/2-1]
while( i < size/2 ){
int leftChild = 2*i + 1;
int rightChild = leftChild + 1;
int largerChild;
if (rightChild < size // rightChild exists?
&& a[leftChild] largerChild = rightChild;
else
largerChild = leftChild;
if (a[i]>=a[largerChild])
break;
swap(a, i, largerChild);
i = largerChild;
}
}

【在 m********g 的大作中提到】
: 指的是哪一个inner loop?
s********x
发帖数: 914
8
heapSort is n/2*log(1.5n)+n*log(1.5n)
quickSort is nlog(n)

node

【在 s********x 的大作中提到】
: // MaxHeap: heapArray starts from index 0
: public static void heapSort(int[] a){
: // Trickling Down in Place: start at node n/2-1, the rightmost node
: with children
: // O(n/2*log(n))
: for (int i=(a.length/2-1) ; i>=0 ; i--)
: trickleDown(a,i, a.length);
: // remove max from heap and insert it at the end
: // O(n*log(n))
: for (int i= a.length - 1; i> 0; i--){

1 (共1页)
进入JobHunting版参与讨论
相关主题
其实我很想知道, 多少软工能25分钟内把heapsort写下一个老题binary tree找 lowest common ancestor 的code (请教
谁知道STL sort用的啥算法?讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径
junior level的工作对算法要求有多高?c语言实现TreeFee
问一道programming peals上的题生成树
A家,link all node in the same levG题,把binary tree里面的sibling节点连接起来
今天面试惨败,分享面经G家实习电面总结
一道面试题狗电面
这个check whether a binary tree is a BST 问题T第二轮面经
相关话题的讨论汇总
话题: int话题: heapsort话题: heaparray话题: rightchild