2.
1 E LEMENTARY S ORTS
‣ rules of the game
‣ selection sort
‣ insertion sort
‣ shuffling
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Sorting problem
Ex. Student records in a university.
Chen 3 A 991-878-4944 308 Blair
Rohde 2 A 232-343-5555 343 Forbes
Gazsi 4 B 766-093-9873 101 Brown
item Furia 1 A 766-093-9873 101 Brown
Kanaga 3 B 898-122-9643 22 Brown
Andrews 3 A 664-480-0023 097 Little
key Battle 4 C 874-088-1212 121 Whitman
Sort. Rearrange array of N items into ascending order.
Andrews 3 A 664-480-0023 097 Little
Battle 4 C 874-088-1212 121 Whitman
Chen 3 A 991-878-4944 308 Blair
Furia 1 A 766-093-9873 101 Brown
Gazsi 4 B 766-093-9873 101 Brown
Kanaga 3 B 898-122-9643 22 Brown
Rohde 2 A 232-343-5555 343 Forbes
3
Sorting applications
Library of Congress numbers
contacts
FedEx packages
playing cards Hogwarts houses
4
Sample sort client 1
Goal. Sort any type of data.
Ex 1. Sort random real numbers in ascending order.
public class Experiment
% java Experiment 10
{
0.08614716385210452
public static void main(String[] args)
0.09054270895414829
{
0.10708746304898642
int N = [Link](args[0]);
0.21166190071646818
Double[] a = new Double[N];
0.363292849257276
for (int i = 0; i < N; i++)
0.460954145685913
a[i] = [Link]();
0.5340026311350087
[Link](a);
for (int i = 0; i < N; i++) 0.7216129793703496
[Link](a[i]); 0.9003500354411443
} 0.9293994908845686
}
5
Sample sort client 2
Goal. Sort any type of data.
Ex 2. Sort strings in alphabetical order.
public class StringSorter
{
public static void main(String[] args)
{
String[] a = [Link]();
[Link](a);
for (int i = 0; i < [Link]; i++)
[Link](a[i]);
}
% more [Link]
}
bed bug dad yet zoo ... all bad yes
% java StringSorter < [Link]
all bad bed bug dad ... yes yet zoo
[suppressing newlines]
6
Sample sort client 3
Goal. Sort any type of data.
Ex 3. Sort the files in a given directory by filename.
import [Link];
% java FileSorter .
[Link]
public class FileSorter
[Link]
{
[Link]
public static void main(String[] args)
[Link]
{
[Link]
File directory = new File(args[0]);
[Link]
File[] files = [Link]();
[Link]
[Link](files);
[Link]
for (int i = 0; i < [Link]; i++)
[Link](files[i].getName()); [Link]
} [Link]
}
7
Total order
Goal. Sort any type of data (for which sorting is well defined).
A total order is a binary relation ≤ that satisfies:
・Antisymmetry: if both v ≤ w and w ≤ v, then v = w.
・Transitivity: if both v ≤ w and w ≤ x, then v ≤ x.
・Totality: either v ≤ w or w ≤ v or both.
Ex.
・Standard order for natural and real numbers.
・Chronological order for dates or times.
・Alphabetical order for strings. COS 423 COS 333
COS 226 COS 217
No transitivity. Rock-paper-scissors.
No totality. PU course prerequisites. COS 126
violates transitivity violates totality
8
Callbacks
Goal. Sort any type of data (for which sorting is well defined).
Q. How can sort() know how to compare data of type Double, String, and
[Link] without any information about the type of an item's key?
Callback = reference to executable code.
・Client passes array of objects to sort() function.
・The sort() function calls object's compareTo() method as needed.
Implementing callbacks.
・Java: interfaces.
・C: function pointers.
・C++: class-type functors.
・C#: delegates.
・Python, Perl, ML, Javascript: first-class functions.
9
Callbacks: roadmap
data-type implementation
client
public class String
public class StringSorter implements Comparable<String>
{
{
...
public static void main(String[] args)
public int compareTo(String b)
{ {
String[] a = [Link](); ...
[Link](a); return -1;
for (int i = 0; i < [Link]; i++) ...
[Link](a[i]); return +1;
...
}
return 0;
}
}
}
Comparable interface (built in to Java) sort implementation
public static void sort(Comparable[] a)
public interface Comparable<Item> {
{ int N = [Link];
public int compareTo(Item that); for (int i = 0; i < N; i++)
} for (int j = i; j > 0; j--)
if (a[j].compareTo(a[j-1]) < 0)
exch(a, j, j-1);
key point: no dependence else break;
}
on String data type
10
Comparable API
Implement compareTo() so that [Link](w)
・Defines a total order.
・Returns a negative integer, zero, or positive integer
if v is less than, equal to, or greater than w, respectively.
・Throws an exception if incompatible types (or either is null).
v w
v w
w v
less than (return -1) equal to (return 0) greater than (return +1)
Built-in comparable types. Integer, Double, String, Date, File, ...
User-defined comparable types. Implement the Comparable interface.
11
Implementing the Comparable interface
Date data type. Simplified version of [Link].
public class Date implements Comparable<Date>
{
private final int month, day, year;
public Date(int m, int d, int y)
only compare dates
{
to other dates
month = m;
day = d;
year = y;
}
public int compareTo(Date that)
{
if ([Link] < [Link] ) return -1;
if ([Link] > [Link] ) return +1;
if ([Link] < [Link]) return -1;
if ([Link] > [Link]) return +1;
if ([Link] < [Link] ) return -1;
if ([Link] > [Link] ) return +1;
return 0;
}
}
12
2.1 E LEMENTARY S ORTS
‣ rules of the game
‣ selection sort
‣ insertion sort
‣ shuffling
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Selection sort demo
・In iteration i, find index min of smallest remaining entry.
・Swap a[i] and a[min].
initial
16
Selection sort
Algorithm. scans from left to right.
Invariants.
・Entries the left of (including ) fixed and in ascending order.
・No entry to right of is smaller than any entry to the left of .
in final order
17
Selection sort: Java implementation
public class Selection {
public static void sort(Comparable[] a)
{
int N = [Link];
for (int i = 0; i < N; i++)
{
int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min]))
min = j;
exch(a, i, min);
}
}
private static boolean less(Comparable v, Comparable w)
{ return [Link](w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
}
20
Selection sort: animations
20 random items
algorithm position
in final order
not in final order
[Link]
21
Selection sort: animations
20 partially-sorted items
algorithm position
in final order
not in final order
[Link]
22
Selection sort: mathematical analysis
Proposition. Selection sort uses (N - 1) + (N - 2) + ... + 1 + 0 ~ N 2 / 2 compares
and N exchanges.
Running time insensitive to input. Quadratic time, even if input is sorted.
Data movement is minimal. Linear number of exchanges.
23
2.1 E LEMENTARY S ORTS
‣ rules of the game
‣ selection sort
‣ insertion sort
‣ shuffling
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Insertion sort demo
・In iteration i, swap a[i] with each larger entry to its left.
26
Insertion sort
Algorithm. scans from left to right.
Invariants.
・Entries to the left of (including ) are in ascending order.
・Entries to the right of have not yet been seen.
in order not yet seen
27
Insertion sort: Java implementation
public class Insertion
{
public static void sort(Comparable[] a)
{
int N = [Link];
for (int i = 0; i < N; i++)
for (int j = i; j > 0; j--)
if (less(a[j], a[j-1]))
exch(a, j, j-1);
else break;
}
private static boolean less(Comparable v, Comparable w)
{ /* as before */ }
private static void exch(Comparable[] a, int i, int j)
{ /* as before */ }
}
29
Insertion sort: animation
40 random items
algorithm position
in order
not yet seen
[Link]
30
Insertion sort: animation
40 reverse-sorted items
algorithm position
in order
not yet seen
[Link]
31
Insertion sort: animation
40 partially-sorted items
algorithm position
in order
not yet seen
[Link]
32
Insertion sort: mathematical analysis
Proposition. To sort a randomly-ordered array with distinct keys,
insertion sort uses ~ ¼ N 2 compares and ~ ¼ N 2 exchanges on average.
Pf. Expect each entry to move halfway back.
33
Insertion sort: analysis
Best case. If the array is in ascending order, insertion sort makes
N – 1 compares and 0 exchanges.
A E E L M O P R S T X
Worst case. If the array is in descending order (and no duplicates),
insertion sort makes ~ ½ N 2 compares and ~ ½ N 2 exchanges.
X T S R P O M L F E A
35
Insertion sort: practical improvements
Half exchanges. Shift items over (instead of exchanging).
・Eliminates unnecessary data movement.
・No longer uses only less() and exch() to access data.
A C H H I M N N P Q X Y K B I N A R Y
Binary insertion sort. Use binary search to find insertion point.
・Number of compares ~ N lg N .
・But still a quadratic number of array accesses.
A C H H I M N N P Q X Y K B I N A R Y
binary search for first key > K
37
2.1 E LEMENTARY S ORTS
‣ rules of the game
‣ selection sort
‣ insertion sort
‣ shuffling
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
How to shuffle an array
Goal. Rearrange array so that result is a uniformly random permutation.
all permutations
equally likely
55
How to shuffle an array
Goal. Rearrange array so that result is a uniformly random permutation.
all permutations
equally likely
56
Shuffle sort
・Generate a random real number for each array entry.
・Sort the array. useful for shuffling
columns in a spreadsheet
0.8003 0.9706 0.9157 0.9649 0.1576 0.4854 0.1419 0.4218 0.9572
57
Shuffle sort
・Generate a random real number for each array entry.
・Sort the array. useful for shuffling
columns in a spreadsheet
0.1419 0.1576 0.4218 0.4854 0.8003 0.9157 0.9572 0.9649 0.9706
58
Shuffle sort
・Generate a random real number for each array entry.
・Sort the array. useful for shuffling
columns in a spreadsheet
0.1419 0.1576 0.4218 0.4854 0.8003 0.9157 0.9572 0.9649 0.9706
Proposition. Shuffle sort produces a uniformly random permutation.
assuming real numbers
uniformly at random (and no ties)
59
Knuth shuffle demo
・In iteration i, pick integer r between 0 and i uniformly at random.
・Swap a[i] and a[r].
62
Knuth shuffle
・In iteration i, pick integer r between 0 and i uniformly at random.
・Swap a[i] and a[r].
Proposition. [Fisher-Yates 1938] Knuth shuffling algorithm produces a
uniformly random permutation of the input array in linear time.
assuming integers uniformly
at random 63
Knuth shuffle
・In iteration i, pick integer r between 0 and i uniformly at random.
・Swap a[i] and a[r].
public class StdRandom
{
...
public static void shuffle(Object[] a)
{
int N = [Link];
for (int i = 0; i < N; i++)
{
int r = [Link](i + 1);
exch(a, i, r);
}
}
}
64