# sort3 - Robert Sedgewick and Kevin Wayne • Copyright ©...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 3.5 Applications 2 obvious applications problems become easy once items are in sorted order non-obvious applications Sorting Applications Applications. ! Sort a list of names. ! Organize an MP3 library. ! Display Google PageRank results. ! List RSS news items in reverse chronological order. ! Find the median. ! Find the closest pair. ! Binary search in a database. ! Identify statistical outliers. ! Find duplicates in a mailing list. ! Data compression. ! Computer graphics. ! Computational biology. ! Supply chain management. ! Book recommendations on Amazon. ! Load balancing on a parallel computer. . . . 3 Engineering a System Sort Bentley-McIlroy. [Engineeering a Sort Function] ! Original motivation: improve qsort() function in C. ! Basic algorithm = 3-way quicksort with cutoff to insertion sort.. ! Partition on Tukey's ninther: median-of-3 elements, each of which is a median-of-3 elements. R A M G X J K B E A M R X K G J E B K E M K ninther medians groups of 3 nine evenly spaced elements approximate median-of-9 4 Java System Sorts Java's system sort. ! Can sort array of type Comparable or any primitive type. ! Uses Bentley-McIlroy quicksort for primitive types. ! Uses mergesort for objects. Q. Why difference between objects and primitive types? import java.util.Arrays; public class IntegerSort { public static void main(String args) { int N = Integer.parseInt(args[0]); int a = new int[N]; for (int i = 0; i < N; i++) a[i] = StdIn.readInt(); Arrays . sort ( a ); for (int i = 0; i < N; i++) System.out.println(a[i]); } } 5 Breaking Java's System Sort Is it possible to make system sort go quadratic? ! No, for mergesort. ! Yes, for deterministic quicksort. McIlroy's devious idea. [A Killer Adversary for Quicksort] ! Construct malicious input while running system quicksort, in response to elements compared. ! If p is pivot, commit to (x < p) and (y < p) , but don't commit to (x < y) or (x > y) until x and y are compared. Consequences. ! Confirms theoretical possibility. ! Algorithmic complexity attack: you enter linear amount of data; server performs quadratic amount of work. so, why are most system implementations of quicksort deterministic? 6 Breaking Java's System Sort A killer input. Blows function call stack in Java and crashes program. % more 250000.txt 218750 222662 11 166672 247070 83339 156253 ... % java IntegerSort < 250000.txt Exception in thread "main" java.lang.StackOverflowError at java.util.Arrays.sort1(Arrays.java:562) at java.util.Arrays.sort1(Arrays.java:606) at java.util.Arrays.sort1(Arrays.java:608) at java.util.Arrays.sort1(Arrays.java:608) at java.util.Arrays.sort1(Arrays.java:608) . . ....
View Full Document

## This document was uploaded on 06/10/2011.

### Page1 / 6

sort3 - Robert Sedgewick and Kevin Wayne • Copyright ©...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online