How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5....

81
How to Win Coding Competitions: Secrets of Champions Week 3: Sorting and Search Algorithms Lecture 8: Integer sorting Maxim Buzdalov Saint Petersburg 2016

Transcript of How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5....

Page 1: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

How to Win Coding Competitions: Secrets of Champions

Week 3: Sorting and Search AlgorithmsLecture 8: Integer sorting

Maxim BuzdalovSaint Petersburg 2016

Page 2: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 3: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 4: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 5: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 6: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 7: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 8: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

In this video:

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 9: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

In this video:

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 10: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

In this video:

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 11: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Sorting integers

How can we speed up sorting, if we know that we are sorting integers?

I Integers are typically bounded by 2W for some word size W

I We can use integers as indices for arrays

I We can perform mathematical operations with integers

We are no longer limited by the Ω(N logN) lower bound. . .

I . . . because we can perform operations which reveal more information

In this video:

I Counting sort

I Bucket sort

I Radix sort

2 / 5

Page 12: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5 1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 13: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5 1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 14: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seen

This is Counting sort!

5 1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 15: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5 1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 16: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5 1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 17: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

0 0 0 0 1

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 18: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

2 5 4 3 2 1 4 3 1 5

1 2 3 4 5

1 0 0 0 1

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 19: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5 4 3 2 1 4 3 1 5

1 2 3 4 5

1 1 0 0 1

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 20: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

4 3 2 1 4 3 1 5

1 2 3 4 5

1 1 0 0 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 21: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

3 2 1 4 3 1 5

1 2 3 4 5

1 1 0 1 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 22: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

2 1 4 3 1 5

1 2 3 4 5

1 1 1 1 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 23: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 4 3 1 5

1 2 3 4 5

1 2 1 1 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 24: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

4 3 1 5

1 2 3 4 5

2 2 1 1 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 25: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

3 1 5

1 2 3 4 5

2 2 1 2 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 26: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 5

1 2 3 4 5

2 2 2 2 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 27: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

5

1 2 3 4 5

3 2 2 2 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 28: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 2 3 4 5

3 2 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 29: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1

1 2 3 4 5

2 2 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 30: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1

1 2 3 4 5

1 2 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 31: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1

1 2 3 4 5

0 2 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 32: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2

1 2 3 4 5

0 1 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 33: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2

1 2 3 4 5

0 0 2 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 34: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3

1 2 3 4 5

0 0 1 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 35: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3

1 2 3 4 5

0 0 0 2 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 36: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4

1 2 3 4 5

0 0 0 1 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 37: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4

1 2 3 4 5

0 0 0 0 3

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 38: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4 5

1 2 3 4 5

0 0 0 0 2

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 39: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4 5 5

1 2 3 4 5

0 0 0 0 1

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 40: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4 5 5 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 41: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4 5 5 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 42: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Counting sort

Given: N integers, each from [1;M], M is quite small. How to sort them efficiently?

I “Quite small” is “we can afford an array of M elements”

Idea: Just count how many times a certain value was seenThis is Counting sort!

1 1 1 2 2 3 3 4 4 5 5 5

1 2 3 4 5

0 0 0 0 0

Running time: Θ(N + M), additional space: Θ(M)

I Faster than comparison-based sorting algorithms when M is small

3 / 5

Page 43: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/Z 1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5

4 / 5

Page 44: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indices

I Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/Z 1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5

4 / 5

Page 45: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/Z 1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5

4 / 5

Page 46: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/Z 1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5

4 / 5

Page 47: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/Z 1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5

4 / 5

Page 48: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1

2

3

4

5 Z

4 / 5

Page 49: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

2/E 5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1 K

2

3

4

5 Z

4 / 5

Page 50: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/W 4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1 K

2 E

3

4

5 Z

4 / 5

Page 51: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

4/J 3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1 K

2 E

3

4

5 Z W

4 / 5

Page 52: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

3/U 2/K 1/Z 4/A 3/Q 1/X 5/E

1 K

2 E

3

4 J

5 Z W

4 / 5

Page 53: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

2/K 1/Z 4/A 3/Q 1/X 5/E

1 K

2 E

3 U

4 J

5 Z W

4 / 5

Page 54: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/Z 4/A 3/Q 1/X 5/E

1 K

2 E K

3 U

4 J

5 Z W

4 / 5

Page 55: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

4/A 3/Q 1/X 5/E

1 K Z

2 E K

3 U

4 J

5 Z W

4 / 5

Page 56: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

3/Q 1/X 5/E

1 K Z

2 E K

3 U

4 J A

5 Z W

4 / 5

Page 57: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/X 5/E

1 K Z

2 E K

3 U Q

4 J A

5 Z W

4 / 5

Page 58: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

5/E

1 K Z X

2 E K

3 U Q

4 J A

5 Z W

4 / 5

Page 59: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1 K Z X

2 E K

3 U Q

4 J A

5 Z W E

4 / 5

Page 60: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K

1 Z X

2 E K

3 U Q

4 J A

5 Z W E

4 / 5

Page 61: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z

1 X

2 E K

3 U Q

4 J A

5 Z W E

4 / 5

Page 62: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X

1

2 E K

3 U Q

4 J A

5 Z W E

4 / 5

Page 63: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E

1

2 K

3 U Q

4 J A

5 Z W E

4 / 5

Page 64: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K

1

2

3 U Q

4 J A

5 Z W E

4 / 5

Page 65: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U

1

2

3 Q

4 J A

5 Z W E

4 / 5

Page 66: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q

1

2

3

4 J A

5 Z W E

4 / 5

Page 67: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J

1

2

3

4 A

5 Z W E

4 / 5

Page 68: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A

1

2

3

4

5 Z W E

4 / 5

Page 69: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A 5/Z

1

2

3

4

5 W E

4 / 5

Page 70: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A 5/Z 5/W

1

2

3

4

5 E

4 / 5

Page 71: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort!

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A 5/Z 5/W 5/E

1

2

3

4

5

4 / 5

Page 72: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort! Θ(N + M) time, Θ(N + M) space.

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A 5/Z 5/W 5/E

1

2

3

4

5

4 / 5

Page 73: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Bucket sort

Given: N key-value pairs, keys are integers from [1;M], M is quite small.How to sort them efficiently?

I We cannot “just count” integers anymoreI But we still can profit from using the keys as array indicesI Have an array of lists of size M, and collect the pairs under their keys

This is Bucket sort! Θ(N + M) time, Θ(N + M) space. It is also stable.

1/K 1/Z 1/X 2/E 2/K 3/U 3/Q 4/J 4/A 5/Z 5/W 5/E

1

2

3

4

5

4 / 5

Page 74: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 75: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 76: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 77: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

Solution:

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 78: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

Solution:

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 79: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

Solution:

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 80: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

Solution:

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

5 / 5

Page 81: How to Win Coding Competitions: Secrets of Champions Week ... fileI Bucket sort I Radix sort 2/5. Sorting integers How can wespeed upsorting, if we know that we aresorting integers?

Radix sort

Given: N integer arrays, each of length L.Each integer is from [1;M], M is quite small.How to sort these arrays lexicographically?

I Strings are integer arrays. M = 26 for Latin alphabet, 256 for ASCII, . . .

I Big integers are integer arrays. M = 10 – or maybe 16, or 65536, or . . .

Solution:

I Use bucket sort by the L-th value → Θ(N + M)

I Use bucket sort by the L− 1-th value → Θ(N + M)

I . . .

I Use bucket sort by the 1-st value → Θ(N + M)

This is Radix sort! Θ((N + M)L) time, Θ(N + M) space.

5 / 5