SlideShare a Scribd company logo
1 of 19
Download to read offline
2.1 ELEMENTARY SORTS

‣
‣
‣
‣
‣
‣

Algorithms
F O U R T H

R O B E R T

S E D G E W I C K

Algorithms, 4th Edition

·

E D I T I O N

K E V I N

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

‣
‣
‣
‣
‣
‣

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

W A Y N E

Robert Sedgewick and Kevin Wayne

·

Copyright © 2002–2011

·

September 25, 2011 11:47:38 AM

2

Sorting problem

Sample sort client

Ex. Student records in a university.

Goal. Sort any type of data.
Ex 1. Sort random real numbers in ascending order.

Chen

3

A

991-878-4944

308 Blair

Rohde

2

A

232-343-5555

343 Forbes

Gazsi

4

B

766-093-9873

101 Brown

1

A

766-093-9873

101 Brown

3

B

898-122-9643

22 Brown

Andrews
key

Furia
Kanaga

item

3

A

664-480-0023

097 Little

Battle

4

C

874-088-1212

stay tuned for an application

121 Whitman

public class Experiment
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
Double[] a = new Double[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
Insertion.sort(a);
for (int i = 0; i < N; i++)
StdOut.println(a[i]);
}
}

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

% java Experiment 10
0.08614716385210452
0.09054270895414829
0.10708746304898642
0.21166190071646818
0.363292849257276
0.460954145685913
0.5340026311350087
0.7216129793703496
0.9003500354411443
0.9293994908845686

101 Brown

Gazsi

4

B

766-093-9873

Kanaga

3

B

898-122-9643

22 Brown

Rohde

2

A

232-343-5555

343 Forbes
3

4
Sample sort client

Sample sort client

Goal. Sort any type of data.

Goal. Sort any type of data.

Ex 2. Sort strings from standard input in alphabetical order.

Ex 3. Sort the files in a given directory by filename.

public class StringSorter
{
public static void main(String[] args)
{
String[] a = In.readStrings(args[0]);
Insertion.sort(a);
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
}

% java FileSorter .
Insertion.class
Insertion.java
InsertionX.class
InsertionX.java
Selection.class
Selection.java
Shell.class
Shell.java
ShellX.class
ShellX.java

import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}

% more words3.txt
bed bug dad yet zoo ... all bad yes
% java StringSorter words3.txt
all bad bed bug dad ... yes yet zoo
5

Callbacks

Callbacks: roadmap
client

Goal. Sort any type of data.

java.io.File without any information about the type of an item's key?

Callbacks = reference to executable code.
Client passes array of objects to sort() function.
The sort() function calls back object's compareTo() method as needed.

Implementing callbacks.

•
•
•
•
•

object implementation

import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}

Q. How can sort() know to compare data of type Double, String, and

•
•

6

Comparable interface (built in to Java)

Java: interfaces.
C: function pointers.

public interface Comparable<Item>
{
public int compareTo(Item that);
}

C++: class-type functors.
C#: delegates.
Python, Perl, ML, Javascript: first-class functions.

key point: no reference to File

7

public class File
implements Comparable<File>
{
...
public int compareTo(File b)
{
...
return -1;
...
return +1;
...
return 0;
}
}

sort implementation
public static void sort(Comparable[] a)
{
int N = a.length;
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);
else break;
}
8
Comparable API

Implementing the Comparable interface

Implement compareTo() so that v.compareTo(w)

Date data type. Simplified version of java.util.Date.

•

Returns a negative integer, zero, or positive integer

•

Throws an exception if incompatible types or either is null.

public class Date implements Comparable<Date>
{
private final int month, day, year;

if v is less than, equal to, or greater than w.

w
v
less than (return -1)

v

w

equal to (return 0)

public Date(int m, int d, int y)
{
month = m;
day
= d;
year = y;
}

v
w
greater than (return +1)

Required property. Must be a total order.

•
•
•

public int compareTo(Date that)
{
if (this.year < that.year )
if (this.year > that.year )
if (this.month < that.month)
if (this.month > that.month)
if (this.day
< that.day )
if (this.day
> that.day )
return 0;
}

Reflexive: v = v.
Antisymmetric: if v < w, then w > v; if v = w, then w = v.
Transitive: if v ≤ w and w ≤ x, then v ≤ x.

Built-in comparable types. String, Double, Integer, Date, File, ...
User-defined comparable types. Implement the Comparable interface.

only compare dates
to other dates

return
return
return
return
return
return

-1;
+1;
-1;
+1;
-1;
+1;

}
9

10

Two useful sorting abstractions

Testing

Helper functions. Refer to data through compares and exchanges.

Goal. Test if an array is sorted.

Less. Is item v less than w ?
private static boolean isSorted(Comparable[] a)
{
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}

private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }

Exchange. Swap item in array a[] at index i with the one at index j.

private static void exch(Comparable[] a, int i, int j)
{
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}

Q. If the sorting algorithm passes the test, did it correctly sort the array?
A.

11

12
Selection sort demo

‣
‣
‣
‣
‣
‣

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

13

14

Selection sort

Selection sort inner loop

Algorithm. ↑ scans from left to right.

To maintain algorithm invariants:

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 ↑.

Move the pointer to the right.
i++;
in final order

•

Identify index of minimum entry on right.

int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min]))
min = j;

•
in final order

↑

↑

in final order

↑

↑

↑

↑

Exchange into position.
exch(a, i, min);
in final order

15

16
Selection sort: Java implementation

Selection sort: mathematical analysis
Proposition. Selection sort uses (N – 1) + (N – 2) + ... + 1 + 0 ~ N 2 / 2 compares

public class Selection
{
public static void sort(Comparable[] a)
{
int N = a.length;
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);
}
}

and N exchanges.
a[]
i min

0

1

2

3

4

5

6

7

8

9 10

S

R

T

E

X

A

M

P

L

E

6
4

S
A

O
O

R
R

T
T

E
E

X
X

A
S

M
M

P
P

L
L

E
E

2
3
4
5
6

10
9
7
7
8

A
A
A
A
A

E
E
E
E
E

R
E
E
E
E

T
T
L
L
L

O
O
O
M
M

X
X
X
X
O

S
S
S
S
S

M
M
M
O
X

P
P
P
P
P

L
L
T
T
T

E
R
R
R
R

7
8
9

10
8
9

A
A
A

E
E
E

E
E
E

L
L
L

M
M
M

O
O
O

P
P
P

X
R
R

S
S
S

T
T
T

R
X
X

10

10

A

E

E

L

M

O

P

R

S

T

X

A

private static boolean less(Comparable v, Comparable w)
{ /* as before */ }

O

0
1

E

E

L

M

O

P

R

S

T

entries in black
are examined to find
the minimum

X

entries in red
are a[min]

entries in gray are
in final position

Trace of selection sort (array contents just after each exchange)

private static void exch(Comparable[] a, int i, int j)
{ /* as before */ }

Running time insensitive to input. Quadratic time, even if input array is sorted.

}

Data movement is minimal. Linear number of exchanges.
17

Selection sort: animations

18

Selection sort: animations

20 random items

20 partially-sorted items

algorithm position

algorithm position

in final order

in final order

not in final order

not in final order

http://www.sorting-algorithms.com/selection-sort

http://www.sorting-algorithms.com/selection-sort

19

20
Selection sort: Gypsy folk dance

‣
‣
‣
‣
‣
‣

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

21

Insertion sort demo

22

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

23

↑

not yet seen

24
Insertion sort inner loop

Insertion sort: Java implementation

To maintain algorithm invariants:

•

public class Insertion
{
public static void sort(Comparable[] a)
{
int N = a.length;
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;
}

Move the pointer to the right.
i++;
↑
in order

•

not yet seen

Moving from right to left, exchange
a[i] with each larger entry to its left.

private static boolean less(Comparable v, Comparable w)
{ /* as before */ }

for (int j = i; j > 0; j--)
if (less(a[j], a[j-1]))
exch(a, j, j-1);
else break;

private static void exch(Comparable[] a, int i, int j)
{ /* as before */ }
}
↑ ↑ ↑↑
in order

not yet seen
25

Insertion sort: mathematical analysis

26

Insertion sort: trace

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.
a[]
i

j

0

1

2

3

4

5

6

7

8

9 10

S

O

R

T

E

X

A

M

P

L

E

1
2

0
1

O
O

S
R

R
S

T
T

E
E

X
X

A
A

M
M

P
P

L
L

E
E

3
4
5
6
7

3
0
5
0
2

O
E
E
A
A

R
O
O
E
E

S
R
R
O
M

T
S
S
R
O

E
T
T
S
R

X
X
X
T
S

A
A
A
X
T

M
M
M
M
X

P
P
P
P
P

L
L
L
L
L

E
E
E
E
E

8
9

4
2

A
A

E
E

M
L

O
M

P
O

R
P

S
R

T
S

X
T

L
X

E
E

10

2

A

E

E

L

M

O

P

R

S

T

X

A

E

E

L

M

O

P

R

S

T

X

entries in gray
do not move

entry in red
is a[j]

entries in black
moved one position
right for insertion

Trace of insertion sort (array contents just after each insertion)

27

28
Insertion sort: animation

Insertion sort: best and worst case

40 random items

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 E E A

algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort

29

Insertion sort: animation

30

Insertion sort: partially-sorted arrays

40 reverse-sorted items

Def. An inversion is a pair of keys that are out of order.
A E E L M O T R X P S
T-R T-P T-S R-P X-P X-S
(6 inversions)

Def. An array is partially sorted if the number of inversions is O(N).

•
•

Ex 1. A small array appended to a large sorted array.
Ex 2. An array with only a few entries out of place.

Proposition. For partially-sorted arrays, insertion sort runs in linear time.

algorithm position

Pf. Number of exchanges equals the number of inversions.

in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort

number of compares = exchanges + (N-1)

31

32
Insertion sort: animation

Insertion sort: Romanian folk dance

40 partially-sorted items

algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort

34

33

Shellsort overview
Idea. Move entries more than one position at a time by h-sorting the array.
an h-sorted array is h interleaved sorted subsequences
h=4

L
L

‣
‣
‣
‣
‣
‣

E

E

A

M
M

E

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

H

L

E

P
P

H
E

S

O

L

T
T

S
L

A

S

X

R

S
O

E

X
L

R

h = 13

P

H

E

L

L

S

O

R

T

E

X

A

M

S

L

E

Shellsort. [Shell 1959] h-sort the array for decreasing sequence of values of h.
P
S
H
input

S

H

L
E

E

E

L L
L

S

O

R

T

E

X

A

M

P

L

L

L
L

S

O

R

T

E

X

A

M

S

L

E

A

M

H

L

E

P

S

O

L

T

S

X

R

S

T

X

13-sort P

H

E

4-sort

L

E

E

1-sort

A

E

E

(8 additional files of size 1)

An h-sorted file is h interleaved sorted files

E

H

L

L

L

M

O

P

R

S

E

Shellsort trace (array contents after each pass)
35

36
h-sorting

Shellsort example: increments 7, 3, 1

How to h-sort an array? Insertion sort, with stride length h.

input

S

3-sorting an array

M
E
E
E
A
A
A
A
A
A

O
O
E
E
E
E
E
E
E
E

L
L
L
L
L
L
L
L
L
L

E
M
M
M
E
E
E
E
E
E

E
E
O
O
O
O
O
O
O
O

X
X
X
X
X
X
P
P
P
P

A
A
A
A
M
M
M
M
M
M

S
S
S
S
S
S
S
S
S
S

P
P
P
P
P
P
X
X
X
X

R
R
R
R
R
R
R
R
R
R

T
T
T
T
T
T
T
T
T
T

R

T

E

X

A

M

P

L

R
R
R
L
L

T
T
T
T
E

E
E
E
E
E

X
X
X
X
X

A
A
A
A
A

M
S
S
S
S

P
P
P
P
P

L
L
L
R
R

E
E
E
E
T

L
L
L
L
L
L
L
L
L

E
M
M
M
E
E
E
E
E

E
E
O
O
O
O
O
O
O

X
X
X
X
X
X
P
P
P

A
A
A
A
M
M
M
M
M

S
S
S
S
S
S
S
S
S

P
P
P
P
P
P
X
X
X

R
R
R
R
R
R
R
R
R

A
A
A
A
A
A
A
A
A
A
A

E

T
T
T
T
T
T
T
T
T

7-sort

S
M
M
M
M

O
O
O
O
O

E
E
E
E
E
E
E
E
E
E
E

L
L
L
E
E
E
E
E
E
E
E

E
E
E
L
L
L
L
L
L
L
L

O
O
O
O
O
O
M
M
M
M
M

P
P
P
P
P
P
O
O
O
O
O

M
M
M
M
M
M
P
P
P
P
P

S
S
S
S
S
S
S
S
S
R
R

X
X
X
X
X
X
X
X
X
S
S

R
R
R
R
R
R
R
R
R
X
T

T
T
T
T
T
T
T
T
T
T
X

E

L

M

O

P

R

S

T

X

3-sort

M
E
E
E
A
A
A
A
A

Why insertion sort?

•
•

1-sort

O

Big increments ⇒ small subarray.
Small increments ⇒ nearly in order. [stay tuned]

O
O
E
E
E
E
E
E
E

result

A

E

37

38

Shellsort: intuition

Shellsort: which increment sequence to use?

Proposition. A g-sorted array remains g-sorted after h-sorting it.

Powers of two. 1, 2, 4, 8, 16, 32, ...
No.

7-sort

M
M
M
M
M

O
O
O
O
O

Powers of two minus one. 1, 3, 7, 15, 31, 63, ...

3-sort

R
R
L
L
L

T
T
T
E
E

E
E
E
E
E

X
X
X
X
X

A
A
A
A
A

S
S
S
S
S

P
P
P
P
P

L
L
R
R
R

E
E
E
T
T

M
E
E
E
A
A
A
A
A
A

O
O
E
E
E
E
E
E
E
E

L
L
L
L
L
L
L
L
L
L

E
M
M
M
E
E
E
E
E
E

E
E
O
O
O
O
O
O
O
O

X
X
X
X
X
X
P
P
P
P

A
A
A
A
M
M
M
M
M
M

S
S
S
S
S
S
S
S
S
S

P
P
P
P
P
P
X
X
X
X

R
R
R
R
R
R
R
R
R
R

Maybe.

T
T
T
T
T
T
T
T
T
T

3x + 1. 1, 4, 13, 40, 121, 364, ...
OK. Easy to compute.

merging of (9 ⨉ 4i) – (9 ⨉ 2i) + 1 and 4i – (3 ⨉ 2i) + 1

Sedgewick. 1, 5, 19, 41, 109, 209, 505, 929, 2161, 3905, ...
Good. Tough to beat in empirical studies.

still 7-sorted

Interested in learning more?

•
•

Challenge. Prove this fact—it's more subtle than you'd think!
39

See Section 6.8 of Algs, 3rd edition or Volume 3 of Knuth for details.
Do a JP on the topic.
40
Shellsort: Java implementation

Shellsort: visual trace

public class Shell
{
public static void sort(Comparable[] a)
{
int N = a.length;

input

3x+1 increment
sequence

int h = 1;
while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, 1093, ...
while (h >= 1)
{ // h-sort the array.
for (int i = h; i < N; i++)
{
for (int j = i; j >= h && less(a[j], a[j-h]); j -= h)
exch(a, j, j-h);
}

40-sorted

insertion sort

13-sorted

move to next
increment
4-sorted

h = h/3;
}
}
private
{ /* as
private
{ /* as

static
before
static
before

result

boolean less(Comparable v, Comparable w)
*/ }
boolean void(Comparable[] a, int i, int j)
*/ }

}
Visual trace of shellsort

41

Shellsort: animation

42

Shellsort: animation

50 random items

50 partially-sorted items

algorithm position

algorithm position

h-sorted

h-sorted

current subsequence
http://www.sorting-algorithms.com/shell-sort

current subsequence

other elements

http://www.sorting-algorithms.com/shell-sort

43

other elements

44
Shellsort: analysis

Why are we interested in shellsort?

Proposition. The worst-case number of compares used by shellsort with the
3x+1 increments is

Example of simple idea leading to substantial performance gains.

O(N 3/2).
Useful in practice.

•
•
•

Property. The number of compares used by shellsort with the 3x+1 increments
is at most by a small multiple of N times the # of increments used.
N

compares

N1.289

Fast unless array size is huge.
Tiny, fixed footprint for code (used in embedded systems).
Hardware sort prototype.

2.5 N lg N

5,000

93

58

209

143

230

20,000

467

349

495

40,000

1022

855

1059

80,000

2266

2089

Simple algorithm, nontrivial performance, interesting questions.

106

10,000

2257

•
•
•

Asymptotic growth rate?
Best sequence of increments?

open problem: find a better increment sequence

Average-case performance?

measured in thousands

Lesson. Some good algorithms are still waiting discovery.
Remark. Accurate model has not yet been discovered (!)
45

46

Shellsort: Hungarian folk dance

‣
‣
‣
‣
‣
‣

47

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

48
How to shuffle an array

How to shuffle an array

Shuffling. Rearrange an array so that result is a uniformly random permutation.

Shuffling. Rearrange an array so that result is a uniformly random permutation.

49

Knuth shuffle demo

50

Knuth shuffle
Knuth shuffle. [Fisher-Yates 1938]

•
•

In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].

r

i

shuffled

not yet seen

Proposition. Knuth shuffling algorithm produces a uniformly random
permutation of the input array in linear time.
51

assuming integers
uniformly at random
52
Knuth shuffle

Knuth shuffle

Knuth shuffle. [Fisher-Yates 1938]

Knuth shuffle. [Fisher-Yates 1938]

•
•

•
•

In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].

r

In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].

common bug: between 0 and N – 1
correct variant: between i and N – 1

i

shuffled

public class StdRandom
{
...
public static void shuffle(Object[] a)
{
int N = a.length;
for (int i = 0; i < N; i++) {
int r = StdRandom.uniform(i + 1);
exch(a, i, r);
}
}
}

not yet seen

Proposition. Knuth shuffling algorithm produces a uniformly random
permutation of the input array in linear time.

between 0 and i

assuming integers
uniformly at random
53

54

Shuffle sort

Shuffle sort

Shuffle sort.

Shuffle sort.

•
•

•
•

Generate a random real number for each array entry.
Sort the array.

0.8003

0.9706

useful for shuffling
columns in a spreadsheet

0.9157

0.9649

0.1576

0.4854

0.1419

0.4218

0.9572

Generate a random real number for each array entry.
Sort the array.

0.1419

0.1576

useful for shuffling
columns in a spreadsheet

0.4218

0.4854

0.8003

0.9157

0.9572

0.9649

0.9706

Proposition. Shuffle sort produces a uniformly random permutation

Proposition. Shuffle sort produces a uniformly random permutation

of the input array, provided no duplicate values.

of the input array, provided no duplicate values.

assuming real numbers
uniformly at random
55

assuming real numbers
uniformly at random
56
War story (Microsoft)

War story (Microsoft)

Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized

Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized

ballot screen for users to select browser in Windows 7.

ballot screen for users to select browser in Windows 7.
Solution? Implement shuffle sort by making comparator always return a
random answer.

http://www.browserchoice.eu

public int compareTo(Browser that)
Microsoft's implementation in Javascript
{
double r = Math.random();
function RandomSort (a,b)
if
{ (r < 0.5) return -1;
if return (0.5 - Math.random());
(r > 0.5) return +1;
return 0;
}
}

browser comparator
(should implement a total order)

appeared last
50% of the time

57

War story (online poker)

58

War story (online poker)

Texas hold'em poker. Software must shuffle electronic cards.

Shuffling algorithm in FAQ at www.planetpoker.com

for i := 1 to 52 do begin
r := random(51) + 1;
swap := card[r];
card[r] := card[i];
card[i] := swap;
end;

between 1 and 51

Bug 1. Random number r never 52 ⇒ 52nd card can't end up in 52nd place.
Bug 2. Shuffle not uniform (should be between i and 51).
Bug 3. random() uses 32-bit seed ⇒ 232 possible shuffles.
Bug 4. Seed = milliseconds since midnight ⇒ 86.4 million possible shuffles.

How We Learned to Cheat at Online Poker: A Study in Software Security
http://itmanagement.earthweb.com/entdev/article.php/616221

“ The generation of random numbers is too important to be left to chance. ”
Exploit. After seeing 5 cards and synchronizing with server clock,
— Robert all future
can determine R. Coveyou cards in real time.
59

60
War story (online poker)
Best practices for shuffling (if your business depends on it).

•
•
•

Use a hardware random-number generator that has passed

•

Use an unbiased shuffling algorithm.

FIPS 140-2 and the NIST statistical test suite.
Continuously monitor statistic properties because hardware
random-number generators are fragile and fail silently.

‣
‣
‣
‣
‣
‣

rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull

Bottom line. Shuffling a deck of cards is hard!
61

62

Convex hull

Convex hull

The convex hull of a set of N points is the smallest perimeter fence

The convex hull of a set of N points is the smallest perimeter fence

enclosing the points.

enclosing the points.

vertex

on convex hull boundary,
but not vertices

Equivalent definitions.

•
•
•

Smallest convex set containing all the points.

Convex hull output. Sequence of vertices in counterclockwise order.

Smallest area convex polygon enclosing the points.
Convex polygon enclosing the points, whose vertices are points in the set.
63

64
Convex hull: mechanical algorithm

Convex hull application: motion planning

Mechanical algorithm. Hammer nails perpendicular to plane; stretch elastic

Robot motion planning. Find shortest path in the plane from s to t

rubber band around points.

that avoids a polygonal obstacle.

obstacle

s

t

http://www.dfanning.com/math_tips/convexhull_1.gif

Fact. Shortest path is either straight line from s to t or it is one of two
polygonal chains of convex hull.
65

66

Convex hull application: farthest pair

Convex hull: geometric properties

Farthest pair problem. Given N points in the plane, find a pair of points with

Fact. Can traverse the convex hull by making only counterclockwise turns.

the largest Euclidean distance between them.
Fact. The vertices of convex hull appear in increasing order of polar angle
with respect to point p with lowest y-coordinate.
5

1
4

2

8
7

9

6
3

10

Fact. Farthest pair of points are extreme points on convex hull.

12

11

p
67

68
Convex hull: Graham scan

•
•
•

Graham scan demo

Choose point p with smallest y-coordinate.
Sort points by polar angle with p.
Consider points in order, and discard unless that would create a ccw turn.
5

1
4

2

8
7

9

6
3

10

12

11

0
69

70

Graham scan: implementation challenges

Implementing ccw

Q. How to find point p with smallest y-coordinate?

CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn?

A. Define a total order, comparing y-coordinate.
is c to the left of the ray a→b

Q. How to sort points by polar angle with respect to p ?
A. Define a total order for each point p. [next lecture]

c

b

b

b

a

c

a

a

b

a

yes
(∞-slope)

no
(collinear)

no
(collinear)

no
(collinear)

c

b

c

Q. How to determine whether p1 → p2 → p3 is a counterclockwise turn?

c

c

b

A. Computational geometry. [next two slides]
a

a

Q. How to sort efficiently?

yes

A. Mergesort sorts in N log N time. [next lecture]

no

Q. How to handle degeneracies (three or more points on a line)?
Lesson. Geometric primitives are tricky to implement.

A. Requires some care, but not hard.

•
•
71

Dealing with degenerate cases.
Coping with floating-point precision.
72
Implementing ccw

Immutable point data type

CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn?

•

Determinant (or cross product) gives twice signed area of planar triangle.

2 × Area(a, b, c) =

ax
bx
cx

ay 1
by 1
cy 1

public class Point2D
{
private final double x;
private final double y;

= (bx − a x )(c y − a y ) − (by − a y )(c x − a x )

public Point(double x, double y)
{ this.x = x; this.y = y; }

(b - a) × (c - a)

public double distanceTo(Point that)
{
double dx = this.x - that.x;
double dy = this.y - that.y;
return Math.sqrt(dx*dx + dy*dy);
}

€
• If signed area > 0, then a → b → c is counterclockwise.
• If signed area < 0, then a → b → c is clockwise.
• If signed area = 0, then a → b → c are collinear.
(bx, by)

(bx, by)

public static int ccw(Point a, Point b, Point c)
{
int area2 = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
if
(area2 < 0) return -1; // clockwise
else if (area2 > 0) return +1; // counter-clockwise
else
return 0; // collinear
}

(bx, by)

=0

>0

danger of floating-point
roundoff error

(cx, cy)

<0
}

(cx, cy)

(ax, ay)

counterclockwise

(ax, ay)

(cx, cy)
clockwise

(ax, ay)
collinear

73

74

More Related Content

What's hot

Aaa ped-18-Unsupervised Learning: Association Rule Learning
Aaa ped-18-Unsupervised Learning: Association Rule LearningAaa ped-18-Unsupervised Learning: Association Rule Learning
Aaa ped-18-Unsupervised Learning: Association Rule LearningAminaRepo
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & ScalaMartin Ockajak
 
Deriving Scalaz
Deriving ScalazDeriving Scalaz
Deriving Scalaznkpart
 
Refinement Types for Haskell
Refinement Types for HaskellRefinement Types for Haskell
Refinement Types for HaskellMartin Ockajak
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java DevelopersMartin Ockajak
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGDavid Galichet
 
Tupple ware in scala
Tupple ware in scalaTupple ware in scala
Tupple ware in scalaVulcanMinds
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FPLuka Jacobowitz
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressBrendan Eich
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Machine learning with R
Machine learning with RMachine learning with R
Machine learning with RMaarten Smeets
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016Penn State University
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalazoxbow_lakes
 

What's hot (20)

Aaa ped-18-Unsupervised Learning: Association Rule Learning
Aaa ped-18-Unsupervised Learning: Association Rule LearningAaa ped-18-Unsupervised Learning: Association Rule Learning
Aaa ped-18-Unsupervised Learning: Association Rule Learning
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & Scala
 
Deriving Scalaz
Deriving ScalazDeriving Scalaz
Deriving Scalaz
 
Refinement Types for Haskell
Refinement Types for HaskellRefinement Types for Haskell
Refinement Types for Haskell
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUG
 
Tupple ware in scala
Tupple ware in scalaTupple ware in scala
Tupple ware in scala
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Lec2
Lec2Lec2
Lec2
 
12. Stack
12. Stack12. Stack
12. Stack
 
Machine learning with R
Machine learning with RMachine learning with R
Machine learning with R
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalaz
 
Scalaz
ScalazScalaz
Scalaz
 
Templates
TemplatesTemplates
Templates
 

Viewers also liked

AwReporting tool introduction (russian)
AwReporting tool introduction (russian)AwReporting tool introduction (russian)
AwReporting tool introduction (russian)marcwan
 
Reporting tips & tricks (russian)
Reporting tips & tricks (russian)Reporting tips & tricks (russian)
Reporting tips & tricks (russian)marcwan
 
AdWords Scripts (russian)
AdWords Scripts (russian)AdWords Scripts (russian)
AdWords Scripts (russian)marcwan
 
Production Log
Production LogProduction Log
Production LogHarriet95
 
Feed services (russian)
Feed services (russian)Feed services (russian)
Feed services (russian)marcwan
 
El Plagio entre el Alumnado de ESO en Andalucía
El Plagio entre el Alumnado de ESO en AndalucíaEl Plagio entre el Alumnado de ESO en Andalucía
El Plagio entre el Alumnado de ESO en AndalucíaUniversidad de Cádiz
 
Mcc scripts deck (日本語)
Mcc scripts deck (日本語)Mcc scripts deck (日本語)
Mcc scripts deck (日本語)marcwan
 

Viewers also liked (7)

AwReporting tool introduction (russian)
AwReporting tool introduction (russian)AwReporting tool introduction (russian)
AwReporting tool introduction (russian)
 
Reporting tips & tricks (russian)
Reporting tips & tricks (russian)Reporting tips & tricks (russian)
Reporting tips & tricks (russian)
 
AdWords Scripts (russian)
AdWords Scripts (russian)AdWords Scripts (russian)
AdWords Scripts (russian)
 
Production Log
Production LogProduction Log
Production Log
 
Feed services (russian)
Feed services (russian)Feed services (russian)
Feed services (russian)
 
El Plagio entre el Alumnado de ESO en Andalucía
El Plagio entre el Alumnado de ESO en AndalucíaEl Plagio entre el Alumnado de ESO en Andalucía
El Plagio entre el Alumnado de ESO en Andalucía
 
Mcc scripts deck (日本語)
Mcc scripts deck (日本語)Mcc scripts deck (日本語)
Mcc scripts deck (日本語)
 

Similar to Αλγόριθμοι

21 elementarysorts 2
21 elementarysorts 221 elementarysorts 2
21 elementarysorts 2Hoang Nguyen
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptBrendan Eich
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
ppt - Deep Learning From Scratch.pdf
ppt - Deep Learning From Scratch.pdfppt - Deep Learning From Scratch.pdf
ppt - Deep Learning From Scratch.pdfsurefooted
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lecturesMSohaib24
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Anwar Patel
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppthenokmetaferia1
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1Puja Koch
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ....NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...NETFest
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with ClojureCarlo Sciolla
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevJavaDayUA
 

Similar to Αλγόριθμοι (20)

21 elementarysorts 2
21 elementarysorts 221 elementarysorts 2
21 elementarysorts 2
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
ppt - Deep Learning From Scratch.pdf
ppt - Deep Learning From Scratch.pdfppt - Deep Learning From Scratch.pdf
ppt - Deep Learning From Scratch.pdf
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Java introduction
Java introductionJava introduction
Java introduction
 
Type Casting Operator
Type Casting OperatorType Casting Operator
Type Casting Operator
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
ppopoff
ppopoffppopoff
ppopoff
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ....NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with Clojure
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 

More from Ρεβέκα Θεοδωροπούλου

Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίαση
Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίασηΤα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίαση
Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίασηΡεβέκα Θεοδωροπούλου
 
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία;
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία; Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία;
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία; Ρεβέκα Θεοδωροπούλου
 
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...Ρεβέκα Θεοδωροπούλου
 
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014Ρεβέκα Θεοδωροπούλου
 
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητες
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητεςοδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητες
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητεςΡεβέκα Θεοδωροπούλου
 
προετοιμασια για τις πανελληνιες εξετασεις
προετοιμασια για τις πανελληνιες εξετασειςπροετοιμασια για τις πανελληνιες εξετασεις
προετοιμασια για τις πανελληνιες εξετασειςΡεβέκα Θεοδωροπούλου
 
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικά
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικάεκπαιδευτικό παιχνίδι διδάσκει μαθηματικά
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικάΡεβέκα Θεοδωροπούλου
 

More from Ρεβέκα Θεοδωροπούλου (20)

Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίαση
Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίασηΤα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίαση
Τα Μαθηματικά και η σχέση τους με την Αρχιτεκτονική σχεδίαση
 
H εξίσωση του suspense
H εξίσωση του suspenseH εξίσωση του suspense
H εξίσωση του suspense
 
κωνσταντίνος μ. κούμας
κωνσταντίνος μ. κούμαςκωνσταντίνος μ. κούμας
κωνσταντίνος μ. κούμας
 
αγορα ακινητων
αγορα ακινητωναγορα ακινητων
αγορα ακινητων
 
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία;
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία; Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία;
Ο δράκος των παιδικών μας χρόνων ή απλά ... Μαθηματικοφοβία;
 
θεωρια μαθηματικων κατευθυνσης
θεωρια μαθηματικων κατευθυνσηςθεωρια μαθηματικων κατευθυνσης
θεωρια μαθηματικων κατευθυνσης
 
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...
εταιρική εικόνα ως σύνθετη διαδικασία, απόδειξης και τοποθέτησης διαμέσου μία...
 
ασκήσεις γ' γυμνασίου
ασκήσεις γ' γυμνασίουασκήσεις γ' γυμνασίου
ασκήσεις γ' γυμνασίου
 
Η απόδειξη της αθωότητας
Η απόδειξη της αθωότηταςΗ απόδειξη της αθωότητας
Η απόδειξη της αθωότητας
 
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014
υλη & οδηγιες μαθηματικων γυμνασιου 2013 2014
 
αγάπη και μαθηματικά
αγάπη και μαθηματικάαγάπη και μαθηματικά
αγάπη και μαθηματικά
 
συμμετρία, γεωμετρία ... χορός;
συμμετρία, γεωμετρία ... χορός;συμμετρία, γεωμετρία ... χορός;
συμμετρία, γεωμετρία ... χορός;
 
μαθηματικά τετράδια 1ο τεύχος
μαθηματικά τετράδια 1ο τεύχοςμαθηματικά τετράδια 1ο τεύχος
μαθηματικά τετράδια 1ο τεύχος
 
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητες
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητεςοδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητες
οδηγος για μαθητες με ιδιαιτερες νοητικες ικανοτητες
 
μονόγραμμα ελύτης
μονόγραμμα ελύτηςμονόγραμμα ελύτης
μονόγραμμα ελύτης
 
προετοιμασια για τις πανελληνιες εξετασεις
προετοιμασια για τις πανελληνιες εξετασειςπροετοιμασια για τις πανελληνιες εξετασεις
προετοιμασια για τις πανελληνιες εξετασεις
 
αριθμός Erdős
αριθμός Erdősαριθμός Erdős
αριθμός Erdős
 
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικά
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικάεκπαιδευτικό παιχνίδι διδάσκει μαθηματικά
εκπαιδευτικό παιχνίδι διδάσκει μαθηματικά
 
προβλήματα όρασης
προβλήματα όρασηςπροβλήματα όρασης
προβλήματα όρασης
 
Ασκήσεις Στατιστικής Γ' Λυκείου ΕΠΑΛ
Ασκήσεις Στατιστικής Γ' Λυκείου ΕΠΑΛΑσκήσεις Στατιστικής Γ' Λυκείου ΕΠΑΛ
Ασκήσεις Στατιστικής Γ' Λυκείου ΕΠΑΛ
 

Recently uploaded

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Recently uploaded (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

Αλγόριθμοι

  • 1. 2.1 ELEMENTARY SORTS ‣ ‣ ‣ ‣ ‣ ‣ Algorithms F O U R T H R O B E R T S E D G E W I C K Algorithms, 4th Edition · E D I T I O N K E V I N rules of the game selection sort insertion sort shellsort shuffling convex hull ‣ ‣ ‣ ‣ ‣ ‣ rules of the game selection sort insertion sort shellsort shuffling convex hull W A Y N E Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 25, 2011 11:47:38 AM 2 Sorting problem Sample sort client Ex. Student records in a university. Goal. Sort any type of data. Ex 1. Sort random real numbers in ascending order. Chen 3 A 991-878-4944 308 Blair Rohde 2 A 232-343-5555 343 Forbes Gazsi 4 B 766-093-9873 101 Brown 1 A 766-093-9873 101 Brown 3 B 898-122-9643 22 Brown Andrews key Furia Kanaga item 3 A 664-480-0023 097 Little Battle 4 C 874-088-1212 stay tuned for an application 121 Whitman public class Experiment { public static void main(String[] args) { int N = Integer.parseInt(args[0]); Double[] a = new Double[N]; for (int i = 0; i < N; i++) a[i] = StdRandom.uniform(); Insertion.sort(a); for (int i = 0; i < N; i++) StdOut.println(a[i]); } } 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 % java Experiment 10 0.08614716385210452 0.09054270895414829 0.10708746304898642 0.21166190071646818 0.363292849257276 0.460954145685913 0.5340026311350087 0.7216129793703496 0.9003500354411443 0.9293994908845686 101 Brown Gazsi 4 B 766-093-9873 Kanaga 3 B 898-122-9643 22 Brown Rohde 2 A 232-343-5555 343 Forbes 3 4
  • 2. Sample sort client Sample sort client Goal. Sort any type of data. Goal. Sort any type of data. Ex 2. Sort strings from standard input in alphabetical order. Ex 3. Sort the files in a given directory by filename. public class StringSorter { public static void main(String[] args) { String[] a = In.readStrings(args[0]); Insertion.sort(a); for (int i = 0; i < a.length; i++) StdOut.println(a[i]); } } % java FileSorter . Insertion.class Insertion.java InsertionX.class InsertionX.java Selection.class Selection.java Shell.class Shell.java ShellX.class ShellX.java import java.io.File; public class FileSorter { public static void main(String[] args) { File directory = new File(args[0]); File[] files = directory.listFiles(); Insertion.sort(files); for (int i = 0; i < files.length; i++) StdOut.println(files[i].getName()); } } % more words3.txt bed bug dad yet zoo ... all bad yes % java StringSorter words3.txt all bad bed bug dad ... yes yet zoo 5 Callbacks Callbacks: roadmap client Goal. Sort any type of data. java.io.File without any information about the type of an item's key? Callbacks = reference to executable code. Client passes array of objects to sort() function. The sort() function calls back object's compareTo() method as needed. Implementing callbacks. • • • • • object implementation import java.io.File; public class FileSorter { public static void main(String[] args) { File directory = new File(args[0]); File[] files = directory.listFiles(); Insertion.sort(files); for (int i = 0; i < files.length; i++) StdOut.println(files[i].getName()); } } Q. How can sort() know to compare data of type Double, String, and • • 6 Comparable interface (built in to Java) Java: interfaces. C: function pointers. public interface Comparable<Item> { public int compareTo(Item that); } C++: class-type functors. C#: delegates. Python, Perl, ML, Javascript: first-class functions. key point: no reference to File 7 public class File implements Comparable<File> { ... public int compareTo(File b) { ... return -1; ... return +1; ... return 0; } } sort implementation public static void sort(Comparable[] a) { int N = a.length; 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); else break; } 8
  • 3. Comparable API Implementing the Comparable interface Implement compareTo() so that v.compareTo(w) Date data type. Simplified version of java.util.Date. • Returns a negative integer, zero, or positive integer • Throws an exception if incompatible types or either is null. public class Date implements Comparable<Date> { private final int month, day, year; if v is less than, equal to, or greater than w. w v less than (return -1) v w equal to (return 0) public Date(int m, int d, int y) { month = m; day = d; year = y; } v w greater than (return +1) Required property. Must be a total order. • • • public int compareTo(Date that) { if (this.year < that.year ) if (this.year > that.year ) if (this.month < that.month) if (this.month > that.month) if (this.day < that.day ) if (this.day > that.day ) return 0; } Reflexive: v = v. Antisymmetric: if v < w, then w > v; if v = w, then w = v. Transitive: if v ≤ w and w ≤ x, then v ≤ x. Built-in comparable types. String, Double, Integer, Date, File, ... User-defined comparable types. Implement the Comparable interface. only compare dates to other dates return return return return return return -1; +1; -1; +1; -1; +1; } 9 10 Two useful sorting abstractions Testing Helper functions. Refer to data through compares and exchanges. Goal. Test if an array is sorted. Less. Is item v less than w ? private static boolean isSorted(Comparable[] a) { for (int i = 1; i < a.length; i++) if (less(a[i], a[i-1])) return false; return true; } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } Exchange. Swap item in array a[] at index i with the one at index j. private static void exch(Comparable[] a, int i, int j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } Q. If the sorting algorithm passes the test, did it correctly sort the array? A. 11 12
  • 4. Selection sort demo ‣ ‣ ‣ ‣ ‣ ‣ rules of the game selection sort insertion sort shellsort shuffling convex hull 13 14 Selection sort Selection sort inner loop Algorithm. ↑ scans from left to right. To maintain algorithm invariants: 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 ↑. Move the pointer to the right. i++; in final order • Identify index of minimum entry on right. int min = i; for (int j = i+1; j < N; j++) if (less(a[j], a[min])) min = j; • in final order ↑ ↑ in final order ↑ ↑ ↑ ↑ Exchange into position. exch(a, i, min); in final order 15 16
  • 5. Selection sort: Java implementation Selection sort: mathematical analysis Proposition. Selection sort uses (N – 1) + (N – 2) + ... + 1 + 0 ~ N 2 / 2 compares public class Selection { public static void sort(Comparable[] a) { int N = a.length; 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); } } and N exchanges. a[] i min 0 1 2 3 4 5 6 7 8 9 10 S R T E X A M P L E 6 4 S A O O R R T T E E X X A S M M P P L L E E 2 3 4 5 6 10 9 7 7 8 A A A A A E E E E E R E E E E T T L L L O O O M M X X X X O S S S S S M M M O X P P P P P L L T T T E R R R R 7 8 9 10 8 9 A A A E E E E E E L L L M M M O O O P P P X R R S S S T T T R X X 10 10 A E E L M O P R S T X A private static boolean less(Comparable v, Comparable w) { /* as before */ } O 0 1 E E L M O P R S T entries in black are examined to find the minimum X entries in red are a[min] entries in gray are in final position Trace of selection sort (array contents just after each exchange) private static void exch(Comparable[] a, int i, int j) { /* as before */ } Running time insensitive to input. Quadratic time, even if input array is sorted. } Data movement is minimal. Linear number of exchanges. 17 Selection sort: animations 18 Selection sort: animations 20 random items 20 partially-sorted items algorithm position algorithm position in final order in final order not in final order not in final order http://www.sorting-algorithms.com/selection-sort http://www.sorting-algorithms.com/selection-sort 19 20
  • 6. Selection sort: Gypsy folk dance ‣ ‣ ‣ ‣ ‣ ‣ rules of the game selection sort insertion sort shellsort shuffling convex hull 21 Insertion sort demo 22 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 23 ↑ not yet seen 24
  • 7. Insertion sort inner loop Insertion sort: Java implementation To maintain algorithm invariants: • public class Insertion { public static void sort(Comparable[] a) { int N = a.length; 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; } Move the pointer to the right. i++; ↑ in order • not yet seen Moving from right to left, exchange a[i] with each larger entry to its left. private static boolean less(Comparable v, Comparable w) { /* as before */ } for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break; private static void exch(Comparable[] a, int i, int j) { /* as before */ } } ↑ ↑ ↑↑ in order not yet seen 25 Insertion sort: mathematical analysis 26 Insertion sort: trace 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. a[] i j 0 1 2 3 4 5 6 7 8 9 10 S O R T E X A M P L E 1 2 0 1 O O S R R S T T E E X X A A M M P P L L E E 3 4 5 6 7 3 0 5 0 2 O E E A A R O O E E S R R O M T S S R O E T T S R X X X T S A A A X T M M M M X P P P P P L L L L L E E E E E 8 9 4 2 A A E E M L O M P O R P S R T S X T L X E E 10 2 A E E L M O P R S T X A E E L M O P R S T X entries in gray do not move entry in red is a[j] entries in black moved one position right for insertion Trace of insertion sort (array contents just after each insertion) 27 28
  • 8. Insertion sort: animation Insertion sort: best and worst case 40 random items 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 E E A algorithm position in order not yet seen http://www.sorting-algorithms.com/insertion-sort 29 Insertion sort: animation 30 Insertion sort: partially-sorted arrays 40 reverse-sorted items Def. An inversion is a pair of keys that are out of order. A E E L M O T R X P S T-R T-P T-S R-P X-P X-S (6 inversions) Def. An array is partially sorted if the number of inversions is O(N). • • Ex 1. A small array appended to a large sorted array. Ex 2. An array with only a few entries out of place. Proposition. For partially-sorted arrays, insertion sort runs in linear time. algorithm position Pf. Number of exchanges equals the number of inversions. in order not yet seen http://www.sorting-algorithms.com/insertion-sort number of compares = exchanges + (N-1) 31 32
  • 9. Insertion sort: animation Insertion sort: Romanian folk dance 40 partially-sorted items algorithm position in order not yet seen http://www.sorting-algorithms.com/insertion-sort 34 33 Shellsort overview Idea. Move entries more than one position at a time by h-sorting the array. an h-sorted array is h interleaved sorted subsequences h=4 L L ‣ ‣ ‣ ‣ ‣ ‣ E E A M M E rules of the game selection sort insertion sort shellsort shuffling convex hull H L E P P H E S O L T T S L A S X R S O E X L R h = 13 P H E L L S O R T E X A M S L E Shellsort. [Shell 1959] h-sort the array for decreasing sequence of values of h. P S H input S H L E E E L L L S O R T E X A M P L L L L S O R T E X A M S L E A M H L E P S O L T S X R S T X 13-sort P H E 4-sort L E E 1-sort A E E (8 additional files of size 1) An h-sorted file is h interleaved sorted files E H L L L M O P R S E Shellsort trace (array contents after each pass) 35 36
  • 10. h-sorting Shellsort example: increments 7, 3, 1 How to h-sort an array? Insertion sort, with stride length h. input S 3-sorting an array M E E E A A A A A A O O E E E E E E E E L L L L L L L L L L E M M M E E E E E E E E O O O O O O O O X X X X X X P P P P A A A A M M M M M M S S S S S S S S S S P P P P P P X X X X R R R R R R R R R R T T T T T T T T T T R T E X A M P L R R R L L T T T T E E E E E E X X X X X A A A A A M S S S S P P P P P L L L R R E E E E T L L L L L L L L L E M M M E E E E E E E O O O O O O O X X X X X X P P P A A A A M M M M M S S S S S S S S S P P P P P P X X X R R R R R R R R R A A A A A A A A A A A E T T T T T T T T T 7-sort S M M M M O O O O O E E E E E E E E E E E L L L E E E E E E E E E E E L L L L L L L L O O O O O O M M M M M P P P P P P O O O O O M M M M M M P P P P P S S S S S S S S S R R X X X X X X X X X S S R R R R R R R R R X T T T T T T T T T T T X E L M O P R S T X 3-sort M E E E A A A A A Why insertion sort? • • 1-sort O Big increments ⇒ small subarray. Small increments ⇒ nearly in order. [stay tuned] O O E E E E E E E result A E 37 38 Shellsort: intuition Shellsort: which increment sequence to use? Proposition. A g-sorted array remains g-sorted after h-sorting it. Powers of two. 1, 2, 4, 8, 16, 32, ... No. 7-sort M M M M M O O O O O Powers of two minus one. 1, 3, 7, 15, 31, 63, ... 3-sort R R L L L T T T E E E E E E E X X X X X A A A A A S S S S S P P P P P L L R R R E E E T T M E E E A A A A A A O O E E E E E E E E L L L L L L L L L L E M M M E E E E E E E E O O O O O O O O X X X X X X P P P P A A A A M M M M M M S S S S S S S S S S P P P P P P X X X X R R R R R R R R R R Maybe. T T T T T T T T T T 3x + 1. 1, 4, 13, 40, 121, 364, ... OK. Easy to compute. merging of (9 ⨉ 4i) – (9 ⨉ 2i) + 1 and 4i – (3 ⨉ 2i) + 1 Sedgewick. 1, 5, 19, 41, 109, 209, 505, 929, 2161, 3905, ... Good. Tough to beat in empirical studies. still 7-sorted Interested in learning more? • • Challenge. Prove this fact—it's more subtle than you'd think! 39 See Section 6.8 of Algs, 3rd edition or Volume 3 of Knuth for details. Do a JP on the topic. 40
  • 11. Shellsort: Java implementation Shellsort: visual trace public class Shell { public static void sort(Comparable[] a) { int N = a.length; input 3x+1 increment sequence int h = 1; while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, 1093, ... while (h >= 1) { // h-sort the array. for (int i = h; i < N; i++) { for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) exch(a, j, j-h); } 40-sorted insertion sort 13-sorted move to next increment 4-sorted h = h/3; } } private { /* as private { /* as static before static before result boolean less(Comparable v, Comparable w) */ } boolean void(Comparable[] a, int i, int j) */ } } Visual trace of shellsort 41 Shellsort: animation 42 Shellsort: animation 50 random items 50 partially-sorted items algorithm position algorithm position h-sorted h-sorted current subsequence http://www.sorting-algorithms.com/shell-sort current subsequence other elements http://www.sorting-algorithms.com/shell-sort 43 other elements 44
  • 12. Shellsort: analysis Why are we interested in shellsort? Proposition. The worst-case number of compares used by shellsort with the 3x+1 increments is Example of simple idea leading to substantial performance gains. O(N 3/2). Useful in practice. • • • Property. The number of compares used by shellsort with the 3x+1 increments is at most by a small multiple of N times the # of increments used. N compares N1.289 Fast unless array size is huge. Tiny, fixed footprint for code (used in embedded systems). Hardware sort prototype. 2.5 N lg N 5,000 93 58 209 143 230 20,000 467 349 495 40,000 1022 855 1059 80,000 2266 2089 Simple algorithm, nontrivial performance, interesting questions. 106 10,000 2257 • • • Asymptotic growth rate? Best sequence of increments? open problem: find a better increment sequence Average-case performance? measured in thousands Lesson. Some good algorithms are still waiting discovery. Remark. Accurate model has not yet been discovered (!) 45 46 Shellsort: Hungarian folk dance ‣ ‣ ‣ ‣ ‣ ‣ 47 rules of the game selection sort insertion sort shellsort shuffling convex hull 48
  • 13. How to shuffle an array How to shuffle an array Shuffling. Rearrange an array so that result is a uniformly random permutation. Shuffling. Rearrange an array so that result is a uniformly random permutation. 49 Knuth shuffle demo 50 Knuth shuffle Knuth shuffle. [Fisher-Yates 1938] • • In iteration i, pick integer r between 0 and i uniformly at random. Swap a[i] and a[r]. r i shuffled not yet seen Proposition. Knuth shuffling algorithm produces a uniformly random permutation of the input array in linear time. 51 assuming integers uniformly at random 52
  • 14. Knuth shuffle Knuth shuffle Knuth shuffle. [Fisher-Yates 1938] Knuth shuffle. [Fisher-Yates 1938] • • • • In iteration i, pick integer r between 0 and i uniformly at random. Swap a[i] and a[r]. r In iteration i, pick integer r between 0 and i uniformly at random. Swap a[i] and a[r]. common bug: between 0 and N – 1 correct variant: between i and N – 1 i shuffled public class StdRandom { ... public static void shuffle(Object[] a) { int N = a.length; for (int i = 0; i < N; i++) { int r = StdRandom.uniform(i + 1); exch(a, i, r); } } } not yet seen Proposition. Knuth shuffling algorithm produces a uniformly random permutation of the input array in linear time. between 0 and i assuming integers uniformly at random 53 54 Shuffle sort Shuffle sort Shuffle sort. Shuffle sort. • • • • Generate a random real number for each array entry. Sort the array. 0.8003 0.9706 useful for shuffling columns in a spreadsheet 0.9157 0.9649 0.1576 0.4854 0.1419 0.4218 0.9572 Generate a random real number for each array entry. Sort the array. 0.1419 0.1576 useful for shuffling columns in a spreadsheet 0.4218 0.4854 0.8003 0.9157 0.9572 0.9649 0.9706 Proposition. Shuffle sort produces a uniformly random permutation Proposition. Shuffle sort produces a uniformly random permutation of the input array, provided no duplicate values. of the input array, provided no duplicate values. assuming real numbers uniformly at random 55 assuming real numbers uniformly at random 56
  • 15. War story (Microsoft) War story (Microsoft) Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized ballot screen for users to select browser in Windows 7. ballot screen for users to select browser in Windows 7. Solution? Implement shuffle sort by making comparator always return a random answer. http://www.browserchoice.eu public int compareTo(Browser that) Microsoft's implementation in Javascript { double r = Math.random(); function RandomSort (a,b) if { (r < 0.5) return -1; if return (0.5 - Math.random()); (r > 0.5) return +1; return 0; } } browser comparator (should implement a total order) appeared last 50% of the time 57 War story (online poker) 58 War story (online poker) Texas hold'em poker. Software must shuffle electronic cards. Shuffling algorithm in FAQ at www.planetpoker.com for i := 1 to 52 do begin r := random(51) + 1; swap := card[r]; card[r] := card[i]; card[i] := swap; end; between 1 and 51 Bug 1. Random number r never 52 ⇒ 52nd card can't end up in 52nd place. Bug 2. Shuffle not uniform (should be between i and 51). Bug 3. random() uses 32-bit seed ⇒ 232 possible shuffles. Bug 4. Seed = milliseconds since midnight ⇒ 86.4 million possible shuffles. How We Learned to Cheat at Online Poker: A Study in Software Security http://itmanagement.earthweb.com/entdev/article.php/616221 “ The generation of random numbers is too important to be left to chance. ” Exploit. After seeing 5 cards and synchronizing with server clock, — Robert all future can determine R. Coveyou cards in real time. 59 60
  • 16. War story (online poker) Best practices for shuffling (if your business depends on it). • • • Use a hardware random-number generator that has passed • Use an unbiased shuffling algorithm. FIPS 140-2 and the NIST statistical test suite. Continuously monitor statistic properties because hardware random-number generators are fragile and fail silently. ‣ ‣ ‣ ‣ ‣ ‣ rules of the game selection sort insertion sort shellsort shuffling convex hull Bottom line. Shuffling a deck of cards is hard! 61 62 Convex hull Convex hull The convex hull of a set of N points is the smallest perimeter fence The convex hull of a set of N points is the smallest perimeter fence enclosing the points. enclosing the points. vertex on convex hull boundary, but not vertices Equivalent definitions. • • • Smallest convex set containing all the points. Convex hull output. Sequence of vertices in counterclockwise order. Smallest area convex polygon enclosing the points. Convex polygon enclosing the points, whose vertices are points in the set. 63 64
  • 17. Convex hull: mechanical algorithm Convex hull application: motion planning Mechanical algorithm. Hammer nails perpendicular to plane; stretch elastic Robot motion planning. Find shortest path in the plane from s to t rubber band around points. that avoids a polygonal obstacle. obstacle s t http://www.dfanning.com/math_tips/convexhull_1.gif Fact. Shortest path is either straight line from s to t or it is one of two polygonal chains of convex hull. 65 66 Convex hull application: farthest pair Convex hull: geometric properties Farthest pair problem. Given N points in the plane, find a pair of points with Fact. Can traverse the convex hull by making only counterclockwise turns. the largest Euclidean distance between them. Fact. The vertices of convex hull appear in increasing order of polar angle with respect to point p with lowest y-coordinate. 5 1 4 2 8 7 9 6 3 10 Fact. Farthest pair of points are extreme points on convex hull. 12 11 p 67 68
  • 18. Convex hull: Graham scan • • • Graham scan demo Choose point p with smallest y-coordinate. Sort points by polar angle with p. Consider points in order, and discard unless that would create a ccw turn. 5 1 4 2 8 7 9 6 3 10 12 11 0 69 70 Graham scan: implementation challenges Implementing ccw Q. How to find point p with smallest y-coordinate? CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn? A. Define a total order, comparing y-coordinate. is c to the left of the ray a→b Q. How to sort points by polar angle with respect to p ? A. Define a total order for each point p. [next lecture] c b b b a c a a b a yes (∞-slope) no (collinear) no (collinear) no (collinear) c b c Q. How to determine whether p1 → p2 → p3 is a counterclockwise turn? c c b A. Computational geometry. [next two slides] a a Q. How to sort efficiently? yes A. Mergesort sorts in N log N time. [next lecture] no Q. How to handle degeneracies (three or more points on a line)? Lesson. Geometric primitives are tricky to implement. A. Requires some care, but not hard. • • 71 Dealing with degenerate cases. Coping with floating-point precision. 72
  • 19. Implementing ccw Immutable point data type CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn? • Determinant (or cross product) gives twice signed area of planar triangle. 2 × Area(a, b, c) = ax bx cx ay 1 by 1 cy 1 public class Point2D { private final double x; private final double y; = (bx − a x )(c y − a y ) − (by − a y )(c x − a x ) public Point(double x, double y) { this.x = x; this.y = y; } (b - a) × (c - a) public double distanceTo(Point that) { double dx = this.x - that.x; double dy = this.y - that.y; return Math.sqrt(dx*dx + dy*dy); } € • If signed area > 0, then a → b → c is counterclockwise. • If signed area < 0, then a → b → c is clockwise. • If signed area = 0, then a → b → c are collinear. (bx, by) (bx, by) public static int ccw(Point a, Point b, Point c) { int area2 = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x); if (area2 < 0) return -1; // clockwise else if (area2 > 0) return +1; // counter-clockwise else return 0; // collinear } (bx, by) =0 >0 danger of floating-point roundoff error (cx, cy) <0 } (cx, cy) (ax, ay) counterclockwise (ax, ay) (cx, cy) clockwise (ax, ay) collinear 73 74