E LEMENTARY S ORTING A LGORITHMS Acknowledgement: The course slides - - PowerPoint PPT Presentation

e lementary
SMART_READER_LITE
LIVE PREVIEW

E LEMENTARY S ORTING A LGORITHMS Acknowledgement: The course slides - - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING E LEMENTARY S ORTING A LGORITHMS Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. E LEMENTARY S ORTING


slide-1
SLIDE 1

BBM 202 - ALGORITHMS

ELEMENTARY
 SORTING ALGORITHMS

  • DEPT. OF COMPUTER ENGINEERING

Acknowledgement: The course slides are adapted from the slides prepared by 


  • R. Sedgewick and K. Wayne of Princeton University.
slide-2
SLIDE 2

ELEMENTARY SORTING ALGORITHMS

  • Sorting review
  • Rules of the game
  • Selection sort
  • Insertion sort
  • Shellsort
slide-3
SLIDE 3

ELEMENTARY SORTING ALGORITHMS

  • Sorting review
  • Rules of the game
  • Selection sort
  • Insertion sort
  • Shellsort
slide-4
SLIDE 4
  • Ex. Student records in a university.
  • Sort. Rearrange array of N items into ascending order.

4

Sorting problem

item key Chen 3 A

991-878-4944 308 Blair

Rohde 2 A

232-343-5555 343 Forbes

Gazsi 4 B

766-093-9873 101 Brown

Furia 1 A

766-093-9873 101 Brown

Kanaga 3 B

898-122-9643 22 Brown

Andrews 3 A

664-480-0023 097 Little

Battle 4 C

874-088-1212 121 Whitman

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

slide-5
SLIDE 5
  • Goal. Sort any type of data.

Ex 1. Sort random real numbers in ascending order.

5

Sample sort client

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

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]); } }

seems artificial, but stay tuned for an application

slide-6
SLIDE 6
  • Goal. Sort any type of data.

Ex 2. Sort strings from file in alphabetical order.

6

Sample sort client

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

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]); } }

slide-7
SLIDE 7
  • Goal. Sort any type of data.

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

7

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

Sample sort client

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()); } }

slide-8
SLIDE 8

8

Callbacks

  • Goal. Sort any type of data.
  • Q. How can sort() know how to compare data of type Double, String, and

java.io.File 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 back 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.
slide-9
SLIDE 9

Callbacks: roadmap

9

sort implementation client

  • bject 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()); } } key point: no dependence


  • n File data type

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; } public class File implements Comparable<File> { ... public int compareTo(File b) { ... return -1; ... return +1; ... return 0; } } Comparable interface (built in to Java) public interface Comparable<Item> { public int compareTo(Item that); }

slide-10
SLIDE 10

A total order is a binary relation ≤ that satisfies

  • Antisymmetry: if v ≤ w and w ≤ v, then v = w.
  • Transitivity: if 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.
  • Alphabetical order for strings.
  • Chronological order for dates.
  • ...

10

Total order

an intransitive relation

slide-11
SLIDE 11

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

  • Is 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).

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

11

Comparable API

less than (return -1) equal to (return 0) greater than (return +1) v w v w v w

slide-12
SLIDE 12

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

public class Date implements Comparable<Date>
 { private final int month, day, year; public Date(int m, int d, int y)
 { month = m; day = d; year = y; } public int compareTo(Date that)
 { if (this.year < that.year ) return -1; if (this.year > that.year ) return +1; if (this.month < that.month) return -1; if (this.month > that.month) return +1; if (this.day < that.day ) return -1; if (this.day > that.day ) return +1; return 0; } }

12

Implementing the Comparable interface

  • nly compare dates


to other dates

slide-13
SLIDE 13

Helper functions. Refer to data through compares and exchanges.

  • Less. Is item v less than w ?
  • Exchange. Swap item in array a[] at index i with the one at index j.

13

Two useful sorting abstractions

private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } private static void exch(Comparable[] a, int i, int j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; }

slide-14
SLIDE 14

ELEMENTARY SORTING ALGORITHMS

  • Sorting review
  • Rules of the game
  • Selection sort
  • Insertion sort
  • Shellsort
slide-15
SLIDE 15
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

15

remaining entries i

slide-16
SLIDE 16
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

16

i min remaining entries

slide-17
SLIDE 17
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

17

i min remaining entries

slide-18
SLIDE 18
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

18

remaining entries in final order i

slide-19
SLIDE 19
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

19

remaining entries i min in final order

slide-20
SLIDE 20
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

20

in final order remaining entries i min

slide-21
SLIDE 21
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

21

remaining entries in final order i

slide-22
SLIDE 22
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

22

remaining entries i min in final order

slide-23
SLIDE 23
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

23

in final order remaining entries i min

slide-24
SLIDE 24
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

24

remaining entries in final order i

slide-25
SLIDE 25
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

25

remaining entries i min in final order

slide-26
SLIDE 26
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

26

in final order remaining entries i min

slide-27
SLIDE 27
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

27

in final order remaining entries i

slide-28
SLIDE 28
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

28

in final order remaining entries i min

slide-29
SLIDE 29
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

29

in final order remaining entries i min

slide-30
SLIDE 30
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

30

in final order remaining entries i

slide-31
SLIDE 31
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

31

in final order remaining entries i min

slide-32
SLIDE 32
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

32

in final order remaining entries i min

slide-33
SLIDE 33
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

33

in final order remaining entries i

slide-34
SLIDE 34
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

34

in final order remaining entries i min

slide-35
SLIDE 35
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

35

in final order remaining entries i min

slide-36
SLIDE 36
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

36

in final order remaining entries i

slide-37
SLIDE 37
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

37

in final order remaining entries i min

slide-38
SLIDE 38
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

38

in final order remaining entries i min

slide-39
SLIDE 39
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

39

in final order remaining entries i

slide-40
SLIDE 40
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

40

in final order remaining entries i min

slide-41
SLIDE 41
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

41

in final order remaining entries i min

slide-42
SLIDE 42
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

42

in final order

slide-43
SLIDE 43
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].

Selection sort

43

sorted

slide-44
SLIDE 44

44

Selection sort: Java implementation

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); } } private static boolean less(Comparable v, Comparable w) { /* as before */ } private static void exch(Comparable[] a, int i, int j) { /* as before */ } }

slide-45
SLIDE 45

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 array is sorted. Data movement is minimal. Linear number of exchanges.

45

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

a[] i min 0 1 2 3 4 5 6 7 8 9 10 S O R T E X A M P L E 0 6 S O R T E X A M P L E 1 4 A O R T E X S M P L E 2 10 A E R T O X S M P L E 3 9 A E E T O X S M P L R 4 7 A E E L O X S M P T R 5 7 A E E L M X S O P T R 6 8 A E E L M O S X P T R 7 10 A E E L M O P X S T R 8 8 A E E L M O P R S T X 9 9 A E E L M O P R S T X 10 10 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 are in final position entries in black are examined to find the minimum entries in red are a[min]

slide-46
SLIDE 46

Selection sort: animations

46

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

20 random items in final order not in final order algorithm position

slide-47
SLIDE 47

Selection sort: animations

47

in final order not in final order algorithm position

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

20 partially-sorted items

slide-48
SLIDE 48

ELEMENTARY SORTING ALGORITHMS

  • Sorting review
  • Rules of the game
  • Selection sort
  • Insertion sort
  • Shellsort
slide-49
SLIDE 49
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

49

slide-50
SLIDE 50
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

50

i not yet seen

slide-51
SLIDE 51
  • In iteration i, swap a[i] with each larger entry to its left.

Selection sort

51

in ascending order not yet seen i j

slide-52
SLIDE 52
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

52

i not yet seen j

slide-53
SLIDE 53
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

53

not yet seen in ascending order i j

slide-54
SLIDE 54
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

54

i not yet seen j

slide-55
SLIDE 55
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

55

i not yet seen j

slide-56
SLIDE 56
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

56

i not yet seen j

slide-57
SLIDE 57
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

57

i not yet seen in ascending order

slide-58
SLIDE 58
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

58

i not yet seen j

slide-59
SLIDE 59
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

59

i not yet seen j

slide-60
SLIDE 60
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

60

i not yet seen j

slide-61
SLIDE 61
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

61

i not yet seen j

slide-62
SLIDE 62
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

62

i not yet seen in ascending order

slide-63
SLIDE 63
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

63

i not yet seen j

slide-64
SLIDE 64
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

64

i not yet seen j

slide-65
SLIDE 65
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

65

i not yet seen in ascending order

slide-66
SLIDE 66
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

66

i not yet seen j

slide-67
SLIDE 67
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

67

i not yet seen j

slide-68
SLIDE 68
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

68

i not yet seen j

slide-69
SLIDE 69
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

69

i not yet seen j

slide-70
SLIDE 70
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

70

i not yet seen j

slide-71
SLIDE 71
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

71

i not yet seen in ascending order

slide-72
SLIDE 72
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

72

i not yet seen j

slide-73
SLIDE 73
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

73

i not yet seen j

slide-74
SLIDE 74
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

74

i not yet seen j

slide-75
SLIDE 75
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

75

i not yet seen j

slide-76
SLIDE 76
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

76

i not yet seen j

slide-77
SLIDE 77
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

77

i not yet seen j

slide-78
SLIDE 78
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

78

i not yet seen j

slide-79
SLIDE 79
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

79

i not yet seen in ascending order

slide-80
SLIDE 80
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

80

i not yet seen j

slide-81
SLIDE 81
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

81

i not yet seen j

slide-82
SLIDE 82
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

82

i not yet seen in ascending order

slide-83
SLIDE 83
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

83

i j

slide-84
SLIDE 84
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

84

i j

slide-85
SLIDE 85
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

85

i j

slide-86
SLIDE 86
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

86

i j

slide-87
SLIDE 87
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

87

i j

slide-88
SLIDE 88
  • In iteration i, swap a[i] with each larger entry to its left.

Insertion sort

88

sorted

slide-89
SLIDE 89

Insertion sort: Java implementation

89

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; } private static boolean less(Comparable v, Comparable w) { /* as before */ } private static void exch(Comparable[] a, int i, int j) { /* as before */ } }

slide-90
SLIDE 90
  • 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.

Insertion sort: mathematical analysis

90

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

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 0 O S R T E X A M P L E 2 1 O R S T E X A M P L E 3 3 O R S T E X A M P L E 4 0 E O R S T X A M P L E 5 5 E O R S T X A M P L E 6 0 A E O R S T X M P L E 7 2 A E M O R S T X P L E 8 4 A E M O P R S T X L E 9 2 A E L M O P R S T X 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 black moved one position right for insertion entries in gray do not move entry in red is a[j]

slide-91
SLIDE 91

Insertion sort: animation

91

in order not yet seen algorithm position

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

40 random items

slide-92
SLIDE 92

Best case. If the array is in ascending order, insertion sort makes
 N - 1 compares and 0 exchanges. Worst case. If the array is in descending order (and no duplicates),
 insertion sort makes ~ ½ N 2 compares and ~ ½ N 2 exchanges.

Insertion sort: best and worst case

92

X T S R P O M L E E A A E E L M O P R S T X

slide-93
SLIDE 93

Insertion sort: animation

93

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

40 reverse-sorted items in order not yet seen algorithm position

slide-94
SLIDE 94
  • Def. An inversion is a pair of keys that are out of order.
  • Def. An array is partially sorted if the number of inversions is ≤ c N.
  • Ex 1. A subarray of size 10 appended to a sorted subarray of size N.
  • Ex 2. An array of size N with only 10 entries out of place.
  • Proposition. For partially-sorted arrays, insertion sort runs in linear time.
  • Pf. Number of exchanges equals the number of inversions.

Insertion sort: partially-sorted arrays

94

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) number of compares = exchanges + (N – 1)

slide-95
SLIDE 95

Insertion sort: animation

95

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

40 partially-sorted items in order not yet seen algorithm position

slide-96
SLIDE 96

ELEMENTARY SORTING ALGORITHMS

  • Sorting review
  • Rules of the game
  • Selection sort
  • Insertion sort
  • Shellsort
slide-97
SLIDE 97
  • Idea. Move entries more than one position at a time by h-sorting the array.
  • Shellsort. [Shell 1959] h-sort the array for decreasing seq. of values of h.

Shellsort overview

an h-sorted array is h interleaved sorted subsequences

97

L E E A M H L E P S O L T S X R L M P T E H S S E L O X A E L R

h = 4

P H E L L S O R T E X A M S L E A E E E H L L L M O P R S S T X L E E A M H L E P S O L T S X R S H E L L S O R T E X A M P L E

input 13-sort 4-sort 1-sort

slide-98
SLIDE 98

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

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

h-sorting

M O L E E X A S P R T E O L M E X A S P R T E E L M O X A S P R T E E L M O X A S P R T A E L E O X M S P R T A E L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T

3-sorting an array

98

slide-99
SLIDE 99

Shellsort example: increments 7, 3, 1

S O R T E X A M P L E

input

S O R T E X A M P L E M O R T E X A S P L E M O R T E X A S P L E M O L T E X A S P R E M O L E E X A S P R T

7-sort

M O L E E X A S P R T E O L M E X A S P R T E E L M O X A S P R T E E L M O X A S P R T A E L E O X M S P R T A E L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T

3-sort

A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T A E E L O P M S X R T A E E L O P M S X R T A E E L O P M S X R T A E E L M O P S X R T A E E L M O P S X R T A E E L M O P S X R T A E E L M O P R S X T A E E L M O P R S T X

1-sort

A E E L M O P R S T X

result

99

slide-100
SLIDE 100

100

Shellsort: intuition

  • Proposition. A g-sorted array remains g-sorted after h-sorting it.
  • Challenge. Prove this fact—it's more subtle than you'd think!

M O R T E X A S P L E M O R T E X A S P L E M O L T E X A S P R E M O L E E X A S P R T M O L E E X A S P R T

7-sort

M O L E E X A S P R T E O L M E X A S P R T E E L M O X A S P R T E E L M O X A S P R T A E L E O X M S P R T A E L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T

3-sort still 7-sorted

slide-101
SLIDE 101

Shellsort: which increment sequence to use?

Powers of two. 1, 2, 4, 8, 16, 32, ... No. Powers of two minus one. 1, 3, 7, 15, 31, 63, ... Maybe. 3x + 1. 1, 4, 13, 40, 121, 364, ...

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

= Interested in learning more?

  • See Section 6.8 of Algs, 3rd edition or

Volume 3 of Knuth for details.

  • Do a JP on the topic.

101

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

slide-102
SLIDE 102

public class Shell
 {
 public static void sort(Comparable[] a)
 {
 int N = a.length; 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);
 } h = h/3; }
 }
 private static boolean less(Comparable v, Comparable w)
 { /* as before */ } private static boolean void(Comparable[] a, int i, int j) { /* as before */ } }

Shellsort: Java implementation

102

insertion sort 3x+1 increment sequence move to next increment

slide-103
SLIDE 103

Shellsort: visual trace

103

input 40-sorted 13-sorted 4-sorted result

slide-104
SLIDE 104

Shellsort: animation

104

h-sorted current subsequence algorithm position 50 random items

  • ther elements

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

slide-105
SLIDE 105

Shellsort: animation

105

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

50 partially-sorted items h-sorted current subsequence algorithm position

  • ther elements
slide-106
SLIDE 106
  • Proposition. The worst-case number of compares used by shellsort with

the 3x+1 increments is O(N 3/2).

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

  • Remark. Accurate model has not yet been discovered (!)

106

Shellsort: analysis

measured in thousands N compares N1.289 2.5 N lg N 5.000 93 58 106 10.000 209 143 230 20.000 467 349 495 40.000 1022 855 1059 80.000 2266 2089 2257

slide-107
SLIDE 107

Why are we interested in shellsort?

Example of simple idea leading to substantial performance gains. Useful in practice.

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

Simple algorithm, nontrivial performance, interesting questions.

  • Asymptotic growth rate?
  • Best sequence of increments?
  • Average-case performance?
  • Lesson. Some good algorithms are still waiting discovery.

107

  • pen problem: find a better increment sequence