sorting - eecs 2011€¦ · sorting eecs 2011 1/95

123
Sorting EECS 2011 www.eecs.yorku.ca/course_archive/2017-18/W/2011MF/ 1/95

Upload: others

Post on 15-Oct-2020

35 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

SortingEECS 2011

www.eecs.yorku.ca/course_archive/2017-18/W/2011MF/

1/95

Page 2: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

2/95

Page 3: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Sorting algorithms

Question

http://www.eecs.yorku.ca/course_archive/2017-18/W/

2011MF/sorting.html

3/95

Page 4: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Sorting algorithms

Answer

1 Merge sort

2 Heap sort

3 Selection sort

4 Quick sort

5 Bubble sort

6 Insertion sort

4/95

Page 5: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Big O, Big Omega and Big Theta

To capture the running time of algorithms, we use the followingnotation. Let f , g ∈ N→ N.

f ∈ O(g) if

∃k > 0 : ∃m ∈ N : ∀n ≥ m : f (n) ≤ k g(n).

f is bounded from above by g

f ∈ Ω(g) if

∃k > 0 : ∃m ∈ N : ∀n ≥ m : k g(n) ≤ f (n).

f is bounded from below by g

f ∈ Θ(g) if

∃k` > 0 : ∃ku > 0 : ∃m ∈ N : ∀n ≥ m : k` g(n) ≤ f (n) ≤ ku g(n).

f is bounded from above and below by g

5/95

Page 6: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Sorting algorithms

selection sort Θ(n2)insertion sort Θ(n2)bubble sort Θ(n2)merge sort Θ(n log n)quick sort Θ(n2)heap sort Θ(n log n)

6/95

Page 7: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

7/95

Page 8: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

How fast can we sort?

Question

How fast can we sort?

Answer

We can sort n elements in Θ(n log n).

Question

Can we sort any faster?

Question

Does there exist an algorithm that can sort n elements any faster?

8/95

Page 9: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

How fast can we sort?

Question

How fast can we sort?

Answer

We can sort n elements in Θ(n log n).

Question

Can we sort any faster?

Question

Does there exist an algorithm that can sort n elements any faster?

8/95

Page 10: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

How fast can we sort?

Question

How fast can we sort?

Answer

We can sort n elements in Θ(n log n).

Question

Can we sort any faster?

Question

Does there exist an algorithm that can sort n elements any faster?

8/95

Page 11: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

How fast can we sort?

Question

How fast can we sort?

Answer

We can sort n elements in Θ(n log n).

Question

Can we sort any faster?

Question

Does there exist an algorithm that can sort n elements any faster?

8/95

Page 12: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

Definition

A sorting algorithm is a comparison sort if the sorting is based oncomparisons between the elements (and not on the values of theelements).

9/95

Page 13: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

insertionSort(a)

for (i = 1; i < a.length; i = i + 1)

key = a[i];

j = i;

while (j > 0 && a[j - 1] > key)

a[j] = a[j - 1];

j = j - 1;

a[j] = key;

Question

Is insertion sort a comparison sort?

Question

Yes.

10/95

Page 14: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

insertionSort(a)

for (i = 1; i < a.length; i = i + 1)

key = a[i];

j = i;

while (j > 0 && a[j - 1] > key)

a[j] = a[j - 1];

j = j - 1;

a[j] = key;

Question

Is insertion sort a comparison sort?

Question

Yes.

10/95

Page 15: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

mergeSort(a, l, u)

if (l + 1 < u)

m = (l + u) / 2;

mergeSort(a, l, m);

mergeSort(a, m, u);

merge(a, l, m, u);

merge(a, l, m, u)

i = l; j = m;

for (k = l; k < u; k = k + 1)

if (i < m && (j >= u || a[i] <= a[j]))

b[k] = a[i]; i = i + 1;

else

b[k] = a[j]; j = j + 1;

for (k = l, k < u; k = k + 1)

a[k] = b[k];

11/95

Page 16: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

Question

Is merge sort a comparison sort?

Question

Yes.

We will see examples of sorting algorithms that are not comparisonsorts later in this lecture.

12/95

Page 17: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

Question

Is merge sort a comparison sort?

Question

Yes.

We will see examples of sorting algorithms that are not comparisonsorts later in this lecture.

12/95

Page 18: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparison sorts

Question

Is merge sort a comparison sort?

Question

Yes.

We will see examples of sorting algorithms that are not comparisonsorts later in this lecture.

12/95

Page 19: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Lower bound

Theorem

Any comparison sort must make Ω(n log n) comparisons in theworst case.

Corollary

The worst case running time of any comparison sort is Ω(n log n).

Corollary

Merge sort and heap sort are asymptotically optimal comparisonsorts.

13/95

Page 20: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Lower bound

Theorem

Any comparison sort must make Ω(n log n) comparisons in theworst case.

Corollary

The worst case running time of any comparison sort is Ω(n log n).

Corollary

Merge sort and heap sort are asymptotically optimal comparisonsorts.

13/95

Page 21: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Lower bound

Theorem

Any comparison sort must make Ω(n log n) comparisons in theworst case.

Corollary

The worst case running time of any comparison sort is Ω(n log n).

Corollary

Merge sort and heap sort are asymptotically optimal comparisonsorts.

13/95

Page 22: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Proof of theorem

Without loss of generality, we may assume that all elements to besorted are different.

Question

Why can we assume that?

Answer

Assume that some elements are the same.

1, 6, 1, 6, 6

Add fractions to make them all different (in linear time).

1, 6, 1 12 , 6 1

3 , 6 23

Sort them.1, 1 1

2 , 6, 6 13 , 6 2

3

Drop fractions (in linear time).

1, 1, 6, 6, 6

14/95

Page 23: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Proof of theorem

Without loss of generality, we may assume that all elements to besorted are different.

Question

Why can we assume that?

Answer

Assume that some elements are the same.

1, 6, 1, 6, 6

Add fractions to make them all different (in linear time).

1, 6, 1 12 , 6 1

3 , 6 23

Sort them.1, 1 1

2 , 6, 6 13 , 6 2

3

Drop fractions (in linear time).

1, 1, 6, 6, 614/95

Page 24: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparisons

Given two elements ai and aj , we can compare then using

ai < aj

ai ≤ aj

ai = aj

ai ≥ aj

ai > aj

15/95

Page 25: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparisons

Question

If all elements are different, then it suffices to compare elementsusing the comparator ≤. Why?

Answer

The other comparators <, ≥ and > can all be expressed in termsof ≤ since

ai < aj iff ai ≤ aj

iff aj ≥ ai

iff aj > ai

Corollary

If all elements are different, then each comparison sort algorithmcan be rewritten so it only uses ≤.

16/95

Page 26: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparisons

Question

If all elements are different, then it suffices to compare elementsusing the comparator ≤. Why?

Answer

The other comparators <, ≥ and > can all be expressed in termsof ≤ since

ai < aj iff ai ≤ aj

iff aj ≥ ai

iff aj > ai

Corollary

If all elements are different, then each comparison sort algorithmcan be rewritten so it only uses ≤.

16/95

Page 27: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Comparisons

Question

If all elements are different, then it suffices to compare elementsusing the comparator ≤. Why?

Answer

The other comparators <, ≥ and > can all be expressed in termsof ≤ since

ai < aj iff ai ≤ aj

iff aj ≥ ai

iff aj > ai

Corollary

If all elements are different, then each comparison sort algorithmcan be rewritten so it only uses ≤.

16/95

Page 28: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Definition

Given the number of elements to be sorted, the decision tree for asorting algorithm is a binary tree containing the comparisonsperformed by the sorting algorithm.

17/95

Page 29: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

insertionSort(a)

for (i = 1; i < a.length; i = i + 1)

key = a[i];

j = i;

while (j > 0 && key <= a[j - 1]) // use <=

a[j] = a[j - 1];

j = j - 1;

a[j] = key;

18/95

Page 30: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

The decision tree for insertion sort for three elements can bedepicted as follows.

a1 ≤ a0

a2 ≤ a0 a2 ≤ a1

〈1, 0, 2〉a2 ≤ a1 a2 ≤ a0 〈0, 1, 2〉

〈2, 1, 0〉 〈1, 2, 0〉 〈2, 0, 1〉 〈0, 2, 1〉

19/95

Page 31: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

selectionSort(a)

for (i = 0; i < a.length; i = i + 1)

min = i;

for (j = i + 1; j < a.length; j = j + 1)

if (a[j] <= a[min]) // use <=

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;

20/95

Page 32: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Question

Draw the decision tree for selection sort for three elements.

21/95

Page 33: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Answer

a1 ≤ a0

a2 ≤ a1 a2 ≤ a0

a0 ≤ a1 a2 ≤ a0 a1 ≤ a0 a1 ≤ a2

〈2, 1, 0〉 〈1, 2, 0〉 〈1, 0, 2〉 〈0, 2, 1〉〈0, 1, 2〉〈2, 0, 1〉

22/95

Page 34: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Height of decision tree

Observation

The worst case running time of a sorting algorithm of n elements

the maximal number of comparisons of a sorting algorithm of nelements

=

the height of the decision tree for the sorting algorithm of nelements.

23/95

Page 35: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Question

Given a decision tree for a sorting algorithm of n elements, whatare the leaves?

Answer

Permutations of 0, 1, . . . , n − 1.

Question

How many permutations of 0, 1, . . . , n − 1 are there?

Answer

n × (n − 1)× · · · 2× 1 = n!.

24/95

Page 36: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Question

Given a decision tree for a sorting algorithm of n elements, whatare the leaves?

Answer

Permutations of 0, 1, . . . , n − 1.

Question

How many permutations of 0, 1, . . . , n − 1 are there?

Answer

n × (n − 1)× · · · 2× 1 = n!.

24/95

Page 37: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Question

Given a decision tree for a sorting algorithm of n elements, whatare the leaves?

Answer

Permutations of 0, 1, . . . , n − 1.

Question

How many permutations of 0, 1, . . . , n − 1 are there?

Answer

n × (n − 1)× · · · 2× 1 = n!.

24/95

Page 38: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Decision tree

Question

Given a decision tree for a sorting algorithm of n elements, whatare the leaves?

Answer

Permutations of 0, 1, . . . , n − 1.

Question

How many permutations of 0, 1, . . . , n − 1 are there?

Answer

n × (n − 1)× · · · 2× 1 = n!.

24/95

Page 39: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Properties of decision trees

Property (Proposition 8.7 of the textbook)

A binary tree of height h has at most 2h leaves.

Property

A decision tree for a sorting algorithm of n elements has at least n!leaves.

Question

Why does a decision tree for a sorting algorithm of n elementshave at least n! leaves?

Answer

Because the n elements can be ordered in any order and, hence,any permutation is a possible outcome.

25/95

Page 40: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Properties of decision trees

Property (Proposition 8.7 of the textbook)

A binary tree of height h has at most 2h leaves.

Property

A decision tree for a sorting algorithm of n elements has at least n!leaves.

Question

Why does a decision tree for a sorting algorithm of n elementshave at least n! leaves?

Answer

Because the n elements can be ordered in any order and, hence,any permutation is a possible outcome.

25/95

Page 41: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Properties of decision trees

Property (Proposition 8.7 of the textbook)

A binary tree of height h has at most 2h leaves.

Property

A decision tree for a sorting algorithm of n elements has at least n!leaves.

Question

Why does a decision tree for a sorting algorithm of n elementshave at least n! leaves?

Answer

Because the n elements can be ordered in any order and, hence,any permutation is a possible outcome.

25/95

Page 42: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Properties of decision trees

Property (Proposition 8.7 of the textbook)

A binary tree of height h has at most 2h leaves.

Property

A decision tree for a sorting algorithm of n elements has at least n!leaves.

Question

Why does a decision tree for a sorting algorithm of n elementshave at least n! leaves?

Answer

Because the n elements can be ordered in any order and, hence,any permutation is a possible outcome.

25/95

Page 43: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Properties of decision trees

Property

A binary tree of height h has at most 2h leaves.

Property

A decision tree for a sorting algorithm of n elements has at least n!leaves.

Conclusion

2h ≥ n! and, hence,

h ≥ log(n!) ≥ log(

(n2 )n2

)= n

2 log(n2 ) ∈ Ω(n log n).

26/95

Page 44: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Lower bound

Observation

The worst case running time of a sorting algorithm of n elements

the maximal number of comparisons of a sorting algorithm of nelements

=

the height of the decision tree for the sorting algorithm of nelements

≥n2 log(n2 ) ∈ Ω(n log n).

27/95

Page 45: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Lower bound

Theorem

Any comparison sort must make Ω(n log n) comparisons in theworst case.

Corollary

The worst case running time of any comparison sort is Ω(n log n).

Corollary

Merge sort and heap sort are asymptotically optimal comparisonsorts.

28/95

Page 46: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

29/95

Page 47: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

But first...

... a break.

30/95

Page 48: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

Bucket sortCounting sortRadix sort

31/95

Page 49: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Linear time sorting

Theorem

The worst case running time of any comparison sort is Ω(n log n).

Question

Can we do better if use information other than comparison ofelements?

Answer

Yes.

32/95

Page 50: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Linear time sorting

Theorem

The worst case running time of any comparison sort is Ω(n log n).

Question

Can we do better if use information other than comparison ofelements?

Answer

Yes.

32/95

Page 51: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

Bucket sortCounting sortRadix sort

33/95

Page 52: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Assumption

All elements come from the interval [0,N − 1] for some N ≥ 2.

Main idea

1. Create N buckets.

2. Place each element in “its” bucket.

3. Concatenate the buckets.

34/95

Page 53: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

1. Create five buckets.

0 1 2 3 4

35/95

Page 54: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

2. Place each element in “its” bucket.

0 1 2 3 4

36/95

Page 55: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 4, 4, 1, 0, 2

2. Place each element in “its” bucket.

0 1 2 3 4

2

37/95

Page 56: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 4, 1, 0, 2

2. Place each element in “its” bucket.

0 1 2 3 4

2 4

38/95

Page 57: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 1, 0, 2

2. Place each element in “its” bucket.

0 1 2 3 4

2 4

4

39/95

Page 58: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 0, 2

2. Place each element in “its” bucket.

0 1 2 3 4

2 4

4

1

40/95

Page 59: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted: 2

2. Place each element in “its” bucket.

0 1 2 3 4

2 4

4

10

41/95

Page 60: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Elements to be sorted:

2. Place each element in “its” bucket.

0 1 2 3 4

2 4

4

10

2

42/95

Page 61: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

2 4

4

10

2

Sorted elements:

43/95

Page 62: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

2 4

4

1

2

Sorted elements: 0

44/95

Page 63: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

2 4

42

Sorted elements: 0, 1

45/95

Page 64: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

2 4

4

Sorted elements: 0, 1, 2

46/95

Page 65: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

4

4

Sorted elements: 0, 1, 2, 2

47/95

Page 66: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

4

Sorted elements: 0, 1, 2, 2, 4

48/95

Page 67: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

3. Concatenate the buckets.

0 1 2 3 4

Sorted elements: 0, 1, 2, 2, 4, 4

49/95

Page 68: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Question

How can we represent the buckets?

Answer

As an array of lists.

50/95

Page 69: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Question

How can we represent the buckets?

Answer

As an array of lists.

50/95

Page 70: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

bucketSort(a, N)

for (i = 0; i < N; i = i + 1)

b[i] = empty list;

for (i = 0; i < a.length; i = i + 1)

b[a[i]].add(a[i]);

j = 0;

for (i = 0; i < N; i = i + 1)

while (!b[i].isEmpty())

a[j] = b[i].remove();

j++;

51/95

Page 71: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

52/95

Page 72: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

52/95

Page 73: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Bucket sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

52/95

Page 74: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Overview

Review

Lower bound

Linear sorting

Bucket sortCounting sortRadix sort

53/95

Page 75: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Assumption

All elements come from the interval [0,N − 1] for some N ≥ 2.

Main idea

1. Create a frequency table with N entries.

2. Keep track of the frequency of each element.

3. Compute the number of elements smaller than or equal to agiven element.

4. Place each element in “right” place.

54/95

Page 76: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

1. Create a frequency table with five entries.

0 1 2 3 4

0 0 0 0 0

55/95

Page 77: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

2. Keep track of the frequency of each element.

0 1 2 3 4

0 0 0 0 0

56/95

Page 78: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 4, 4, 1, 0, 2

2. Keep track of the frequency of each element.

0 1 2 3 4

0 0 1 0 0

57/95

Page 79: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 4, 1, 0, 2

2. Keep track of the frequency of each element.

0 1 2 3 4

0 0 1 0 1

58/95

Page 80: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 1, 0, 2

2. Keep track of the frequency of each element.

0 1 2 3 4

0 0 1 0 2

59/95

Page 81: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 0, 2

2. Keep track of the frequency of each element.

0 1 2 3 4

0 1 1 0 2

60/95

Page 82: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2

2. Keep track of the frequency of each element.

0 1 2 3 4

1 1 1 0 2

61/95

Page 83: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted:

2. Keep track of the frequency of each element.

0 1 2 3 4

1 1 2 0 2

62/95

Page 84: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

0 0 0 0 0

63/95

Page 85: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

1 0 0 0 0

64/95

Page 86: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

1 2 0 0 0

65/95

Page 87: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

1 2 4 0 0

66/95

Page 88: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 0

67/95

Page 89: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

3. Compute the number of elements smaller than or equal to agiven element.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 6

68/95

Page 90: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 6

Sorted elements:

69/95

Page 91: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 6

Sorted elements:

70/95

Page 92: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 6

Sorted elements:

71/95

Page 93: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 4 4 6

Sorted elements:

2

72/95

Page 94: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 3 4 6

Sorted elements:

2

73/95

Page 95: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 3 4 6

Sorted elements:

2

74/95

Page 96: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 3 4 6

Sorted elements:

2

75/95

Page 97: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

1 2 3 4 6

Sorted elements:

0 2

76/95

Page 98: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 2 3 4 6

Sorted elements:

0 2

77/95

Page 99: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 2 3 4 6

Sorted elements:

0 2

78/95

Page 100: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 2 3 4 6

Sorted elements:

0 2

79/95

Page 101: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 2 3 4 6

Sorted elements:

0 1 2

80/95

Page 102: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 6

Sorted elements:

0 1 2

81/95

Page 103: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 6

Sorted elements:

0 1 2

82/95

Page 104: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 6

Sorted elements:

0 1 2

83/95

Page 105: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 6

Sorted elements:

0 1 2 4

84/95

Page 106: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 5

Sorted elements:

0 1 2 4

85/95

Page 107: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 5

Sorted elements:

0 1 2 4

86/95

Page 108: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 5

Sorted elements:

0 1 2 4

87/95

Page 109: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 5

Sorted elements:

0 1 2 4 4

88/95

Page 110: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 4

Sorted elements:

0 1 2 4 4

89/95

Page 111: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 4

Sorted elements:

0 1 2 4 4

90/95

Page 112: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 4

Sorted elements:

0 1 2 4 4

91/95

Page 113: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 3 4 4

Sorted elements:

0 1 2 2 4 4

92/95

Page 114: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Elements to be sorted: 2, 4, 4, 1, 0, 2

4. Place each element in “right” place.

0 1 2 3 4

1 1 2 0 2

0 1 2 4 4

Sorted elements:

0 1 2 2 4 4

93/95

Page 115: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

countingSort(a, N)

for (i = 0; i < N; i = i + 1)

f[i] = 0;;

for (i = 0; i < a.length; i = i + 1)

f[a[i]] = f[a[i]] + 1;

for (i = 1; i < N; i = i + 1)

f[i] = f[i-1] + f[i];

for (i = a.length - 1; i >= 0; i = i - 1)

b[f[a[i]] = a[i];

f[a[i]] = f[a[i]] - 1;

for (i = 0; i < a.length; i++)

a[i] = b[i];

94/95

Page 116: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

95/95

Page 117: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

95/95

Page 118: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Counting sort

Question

Express the worst case running time of bucket sort in terms of nand N.

Answer

O(n + N).

Note

If N ∈ O(n) then the worst case running time of bucket sort isO(n).

95/95

Page 119: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Radix Sort Input:

• An array of N numbers. • Each number contains d digits. • Each digit between [0…k-1]

Output: • Sorted numbers.

Each digit (column) can be sorted (e.g., using Counting Sort). Which digit to start from?

Page 120: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

RadixSort

344 125 333 134 224 334 143 225 325 243

Sort by which digit first?

The most significant.

125 134 143 224 225 243 344 333 334 325

Sort by which digit Second?

The next most significant.

125 224 225 325 134 333 334 143 243 344

All meaning in first sort lost.

Page 121: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Radix Sort 1. Start from the least significant digit, sort

2. Sort by the next least significant digit

3. Are the last 2 columns sorted?

4. Generalize: after j iterations, the last j columns are sorted

5. Loop invariant: Before iteration i, the keys have been correctly stable-sorted with respect to the i-1 least-significant digits.

Page 122: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Radix sort

Radix-Sort(A,d) • for i←1 to d • do use a stable sort to sort A on digit i Analysis: Given n d-digit numbers where each digit takes on

up to k values, Radix-Sort sorts these numbers correctly in Θ(d(n+k)) time.

Page 123: Sorting - EECS 2011€¦ · Sorting EECS 2011  1/95

Radix sort – example (binary)

Sorting a sequence of 4-bit integers

1001

0010

1101

0001

1110

0010

1110

1001

1101

0001

1001

1101

0001

0010

1110

1001

0001

0010

1101

1110

0001

0010

1001

1101

1110