Unformatted text preview: MEEX
MS T X
heap-ordered 0 a[i]
TLX 9 10 11
1 6 1 O M E A L E P R S T 1
T ・Mergesort: no, linear extra space.
・Quicksort: no, quadratic time in worst case.
・Heapsort: yes! X
2 Significance. In-place sorting algorithm with N log N worst-case. X
X in-place merge possible, not practical
N log N worst-case quicksort possible,
not practical X 5
4 algorithm be improved to ~ 1 N lg N 1
sorted result Bottom line. Heapsort is optimal for both time and space, but: ・Inner loop longer than quicksort’s.
・Makes poor use of cache memory.
・Not stable. Heapsort trace (array contents just after each sink) 35 36 Sorting algorithms: summary inplace?
selection x shell x quick
heap x average best ½N 2 ½N ½N 2 ¼N 2 N use for small N or partially ordered N tight code, subquadratic N lg N N log N probabilistic guarantee
fastest in practice ½N ?
2 2 N ln N 2 remarks 2 ? x
½N x insertion stable? N exchanges 2.4 P RIORITY Q UEUES
‣ API and elementary implementations
‣ binary heaps improves quicksort in presence
# key comparisons2 Nsort N distinct randomly-ordered keys
to ln N
of duplicate keys
x N lg N N lg N N log N guarantee, stable 2 N lg N x N lg N 2 N lg N N lg N Algorithms N log N guarantee, in-place ‣ heapsort
‣ event-driven simulation R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu ??? x x N lg N N lg N N lg N holy sorting grail 37 Molecular dynamics simulation of hard discs Molecular dynamics simulation of hard discs Goal. Simulate the motion of N moving particles that behave Goal. Simulate the motion of N moving particles that behave according to the laws of elastic collision. according to the laws of elastic collision.
Hard disc model. ・Moving particles interact via elastic collisions with each other and walls.
・Each particle is a disc with known position, velocity, mass, and radius.
・No other forces.
diffusion constant motion of individual
atoms and molecules Significance. Relates macroscopic observables to microscopic...
View Full Document
- Fall '08
- Binary heap, Priority queue, EVIN W AYNE