Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, …...

102
quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2

Transcript of Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, …...

Page 1: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

quick sort, lower bound on sorting, bucket sort,

radix sort, comparison of algorithms, code, …

Sorting: part 2

Page 2: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 3: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 4: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 5: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Demo with cards?

1. Divide S into 3 parts as follows• Select an element e• Create L with all element is S less than e• Create E with all element in S equal to e• Create G with all element in S greater than e

2. Recur by quick sorting L and quick sorting G3. Conquer by appending L to E to G

Divide & Conquer (with quick sort)

Page 6: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Demo with cards?

Page 7: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 8: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 9: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 10: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 11: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 12: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 13: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 14: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 15: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 16: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 17: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Stopping condition

Page 18: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Less than, Equal to, Greater than

Page 19: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

pivot

Page 20: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Get the data sets L, E, and G

Page 21: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Recurse & conquer

Page 22: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 23: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

.. and this happens if S is sorted!

Page 24: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Page 25: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingquick sort

Two enhancements• improve selection of pivot

• randomise• “in place” sorting

• uses no more space and is fast

Page 26: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

A lower bound on comparison based sorting

i.e. how good can it get?

Page 27: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingA lower bound on comparison based sorting

• Assume we are given a sequence S, of length n, to be sorted (no duplicates)• an algorithm compares pairs of elements with two outcomes

• x < y TRUE• x < y FALSE

• we can represent a comparison based sorting algorithm as a decision tree• i.e. represent its behaviour• internal node represents a decision• left is TRUE, right is FALSE

• there are n! possible orderings of S• there must be n! leaves in our decision tree

• one for each possible ordering of S• running time of algorithm must be at least the height of the decision tree• the height of the decision tree is at least log(n!) base 2• log(n!) is O(n.log(n))

Page 28: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

stable sorting

Page 29: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingstable sorting

A sorting algorithm is stable if for any two entries x and yof S such that

• x.getKey() == y.getKey() and • x precedes y in S before sorting• x precedes y after S is sorted

Page 30: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

bucket sortaka

pigeonhole sort

Page 31: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

bucket sortaka

pigeonhole sort

PIGEONHOLE SORT

Page 32: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingbucket sort (aka pigeonhole sort)

Use the sey of an object as an index into a bucket array

Assume keys are in range [0,N-1]

Assume we have n object to sort

insert object e into bucket[e.getKey()]

Complexity is O(n+N)

Page 33: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingbucket sort (aka pigeonhole sort)

Use the sey of an object as an index into a bucket array

Assume keys are in range [0,N-1]

Assume we have n object to sort

insert object e into bucket[e.getKey()]

Complexity is O(n+N)

How might we do this in java?What data structures would we use?

Is it stable?What kind of data sets would be suitable?

Page 34: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

radix sort

We will describe by example

Page 35: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on least significant digit

Page 36: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on least significant digit

Page 37: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

Sort on least significant digit

Page 38: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

Sort on least significant digit

Page 39: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

28

Sort on least significant digit

Page 40: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

28

Sort on least significant digit

Page 41: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

28

81

Sort on least significant digit

Page 42: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89

28

81

Sort on least significant digit

Page 43: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81

Sort on least significant digit

Page 44: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81

Sort on least significant digit

Page 45: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81

14

Sort on least significant digit

Page 46: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81

14

Sort on least significant digit

Page 47: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81, 31

14

Sort on least significant digit

Page 48: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69

28

81, 31

14

Sort on least significant digit

Page 49: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29

28

81, 31

14

Sort on least significant digit

Page 50: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29

28

81, 31

14

Sort on least significant digit

Page 51: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29

28, 18

81, 31

14

Sort on least significant digit

Page 52: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29

28, 18

81, 31

14

Sort on least significant digit

Page 53: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29,39

28, 18

81, 31

14

Sort on least significant digit

Page 54: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29,39

28, 18

81, 31

14

Sort on least significant digit

Page 55: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29,39

28, 18

81, 31

14

17

Sort on least significant digit

Page 56: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

89, 69, 29,39

28, 18

81, 31

14

17

Sorted on least significant digit!

Page 57: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

Page 58: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

Page 59: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

Page 60: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

Page 61: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

Page 62: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

Page 63: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14

Page 64: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14

Page 65: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14, 17

Page 66: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14, 17

28

Page 67: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14, 17

28

Page 68: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14, 17, 18

28

Page 69: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81

31

14, 17, 18

28

Page 70: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28

Page 71: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28

Page 72: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28

69

Page 73: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28

69

Page 74: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28, 29

69

Page 75: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31

14, 17, 18

28, 29

69

Page 76: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sort on next (second) significant digit

81, 89

31, 39

14, 17, 18

28, 29

69

Page 77: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

5

6

7

8

9

0

1

2

3

4

Sorted on next (second) significant digit!

81, 89

31, 39

14, 17, 18

28, 29

69

Page 78: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingradix sort

• We can extend this to numbers of any length• We can do this on strings

Considering numbers to base b• require b buckets• require d = log(m) scans (d is number of digits)

• log is to base b• m is largest number to be sorted

• each scan is of order n• therefore O(d.n)

Page 79: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

comparison of algorithms

Page 80: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 81: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 82: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 83: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 84: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 85: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 86: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 87: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 88: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 89: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sortingcomparison of algorithms

Bubble sort

Insertion sort

Selection sort

Merge sort

Quick sort

Heap sort

Bucket sort

Radix sort

Page 90: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 91: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 92: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 93: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 94: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

sorting

Yes, but how did you sort out your CD’s?

Page 95: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

not covered sorting

Sift sortShell sortBead sort

Page 96: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

not covered sorting

Sift sortShell sortBead sort

Page 97: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

not covered sorting

Sift sortShell sortBead sort

Page 98: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

not covered sorting

Page 99: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 100: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 101: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Page 102: Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.