notes_COSqueues

3 s o e e p a x e m a r p r a r x e e m o m l l e s o

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MEEX METX MS T X RSTX 11 2 11 1 heap-ordered 0 a[i] 456 TEX TLX 9 10 11 PLE PEE 10 9 8 7 1 1 1 1 6 1 O M E A L E P R S T 1 1 M L L E E E A A E M O O P P R R S S T T 1 1 E E A A E E L L M M O O P P R R S S T T X X A A E E E E L L M M O O P P R R S S T T ・Mergesort: no, linear extra space. ・Quicksort: no, quadratic time in worst case. ・Heapsort: yes! X X 3 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 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 3-way quick merge 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 x worst ½N x insertion stable? N exchanges 2.4 P RIORITY Q UEUES ‣ API and elementary implementations ‣ binary heaps improves quicksort in presence ½N2 N # 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. temperature, pressure, diffusion constant motion of individual atoms and molecules Significance. Relates macroscopic observables to microscopic...
View Full Document

This document was uploaded on 02/20/2014 for the course COS 226 at Princeton.

Ask a homework question - tutors are online