sorting. introduction common problem: sort a list of values, starting from lowest to highest. list...

178
Sorting Sorting

Upload: kerrie-walton

Post on 13-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

SortingSorting

Page 2: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

IntroductionIntroduction

Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical order Students names listed alphabetically Student records sorted by ID#

Generally, we are given a list of records that have keys. These keys are used to define an ordering of the items in the list.

Page 3: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

C++ Implementation of SortingC++ Implementation of Sorting

Use C++ templates to implement a generic sorting function.

This would allow use of the same function to sort items of any class.

However, class to be sorted must provide the following overloaded operators: Assignment: = Ordering: >, <, ==

Example class: C++ STL string class In this lecture, we’ll talk about sorting integers; however,

the algorithms are general and can be applied to any class as described above.

Page 4: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quadratic Sorting AlgorithmsQuadratic Sorting Algorithms

We are given n records to sort. There are a number of simple sorting

algorithms whose worst and average case performance is quadratic O(n2): Selection sort Insertion sort Bubble sort

Page 5: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Sorting an Array of IntegersSorting an Array of Integers

Example: we are given an array of six integers that we want to sort from smallest to largest

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]

Page 6: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Start by finding the smallest entry.

[0] [1] [2] [3] [4] [5]

Page 7: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 8: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 9: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Part of the array is now sorted.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 10: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort Algorithm

Find the smallest element in the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 11: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

Swap with the front of the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 12: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

We have increased the size of the sorted side by one element.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 13: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

[0] [1] [2] [3] [4] [5]

Page 14: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

The process continues...

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Swap

with

front

Swap

with

front

Page 15: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

The process continues...

Sorted side Unsorted sideSorted side

is bigger

Sorted sideis bigger

[0] [1] [2] [3] [4] [5]

Page 16: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

The process keeps adding one more number to the sorted side.

The sorted side has the smallest numbers, arranged from small to large.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 17: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

We can stop when the unsorted side has just one number, since that number must be the largest number.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 18: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

The Selection Sort AlgorithmThe Selection Sort Algorithm

The array is now sorted.

We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.

[0] [1] [2] [3] [4] [5]

Page 19: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void selection_sort(Item data[ ], size_t n){ size_t i, j, smallest; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) {

// find smallest in unsorted part of arraysmallest = i;for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)temp = data[i];data[i] = data[smallest];data[smallest] = temp;

}}

Page 20: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Selection Time Sort AnalysisSelection Time Sort Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 1 to n-1

find smallest key in unsorted part of array

swap smallest item to front of unsorted array

decrease size of unsorted array by 1

Page 21: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Selection Time Sort AnalysisSelection Time Sort Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 1 to n-1 O(n)

find smallest key in unsorted part of array O(n)swap smallest item to front of unsorted arraydecrease size of unsorted array by 1

Selection sort analysis: O(n2)

Page 22: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void selection_sort(Item data[ ], size_t n){ size_t i, j, smallest; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) {

// find smallest in unsorted part of arraysmallest = i;for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)temp = data[i];data[i] = data[smallest];data[smallest] = temp;

}}

Outer loop:O(n)

Page 23: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void selection_sort(Item data[ ], size_t n){ size_t i, j, smallest; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) {

// find smallest in unsorted part of arraysmallest = i;for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)temp = data[i];data[i] = data[smallest];data[smallest] = temp;

}}

Outer loop:O(n)

Inner loop:O(n)

Page 24: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

The Insertion Sort algorithm also views the array as having a sorted side and an unsorted side.

[0] [1] [2] [3] [4] [5]

Page 25: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

The sorted side starts with just the first element, which is not necessarily the smallest element.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 26: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

The sorted side grows by taking the front element from the unsorted side...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 27: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

...and inserting it in the place that keeps the sorted side arranged from small to large.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 28: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 29: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sometimes we are lucky and the new inserted item doesn't need to move at all.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 30: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sometimes we are lucky twice in a row.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 31: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element to a separate location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 32: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 33: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 34: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 35: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 36: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

...until you reach the location for the new element.

[0] [1] [2] [3] [4] [5]

Page 37: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element back into the array, at the correct location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 38: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

How to Insert One ElementHow to Insert One Element

The last element must also be inserted. Start by copying it...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 39: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Sorted ResultSorted Result

[0] [1] [2] [3] [4] [5]

Page 40: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void insertion_sort(Item data[ ], size_t n){ size_t i, j; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i) {

// take next item at front of unsorted part of array // and insert it in appropriate location in sorted part of arraytemp = data[i];for(j = i; data[j-1] > temp and j > 0; --j)

data[j] = data[j-1]; // shift element forward

data[j] = temp; }}

Page 41: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Insertion Sort Time AnalysisInsertion Sort Time Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 1 to n-1

take next key from unsorted part of array

insert in appropriate location in sorted part of array:

for j = i down to 0,

shift sorted elements to the right if key > key[i]

increase size of sorted array by 1

Page 42: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Insertion Sort Time AnalysisInsertion Sort Time Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 1 to n-1

take next key from unsorted part of array

insert in appropriate location in sorted part of array:

for j = i down to 0,

shift sorted elements to the right if key > key[i]

increase size of sorted array by 1

Outer loop:O(n)

Page 43: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Insertion Sort Time AnalysisInsertion Sort Time Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 1 to n-1

take next key from unsorted part of array

insert in appropriate location in sorted part of array:

for j = i down to 0,

shift sorted elements to the right if key > key[i]

increase size of sorted array by 1

Outer loop:O(n)

Inner loop:O(n)

Page 44: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void insertion_sort(Item data[ ], size_t n){ size_t i, j; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i) {

// take next item at front of unsorted part of array // and insert it in appropriate location in sorted part of arraytemp = data[i];for(j = i; data[j-1] > temp and j > 0; --j)

data[j] = data[j-1]; // shift element forward

data[j] = temp; }}

O(n)

O(n)

Page 45: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5]

Page 46: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Swap?

Page 47: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Yes!

Page 48: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Swap?

Page 49: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] No.

Page 50: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Swap?

Page 51: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] No.

Page 52: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Swap?

Page 53: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Yes!

Page 54: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Swap?

Page 55: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

[0] [1] [2] [3] [4] [5] Yes!

Page 56: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 57: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 58: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 59: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 60: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 61: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 62: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 63: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 64: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 65: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 66: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 67: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 68: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 69: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 70: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

Continue looping, until done.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 71: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void bubble_sort(Item data[ ], size_t n){ size_t i, j; Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1; ++i) {

for(j = 0; j < n-1;++j) if(data[j] > data[j+1]) // if out of order, swap!

{ temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; }

}}

Page 72: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class Item>void bubble_sort(Item data[ ], size_t n){ size_t i, j; Item temp; bool swapped = true;

if(n < 2) return; // nothing to sort!! for(i = 0; swapped and i < n-1; ++i) { // if no elements swapped in an iteration,

// then elements are in order: done!for(swapped = false, j = 0; j < n-1;++j)

if(data[j] > data[j+1]) // if out of order, swap! { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp;

swapped = true; }

}}

Page 73: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Bubble Sort Time AnalysisBubble Sort Time Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 0 to n-1

for j =0 to n-2

if key[j] > key[j+1] then swap

if no elements swapped in this pass through array, done.

otherwise, continue

Page 74: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Bubble Sort Time AnalysisBubble Sort Time Analysis

In O-notation, what is: Worst case running time for n items? Average case running time for n items?

Steps of algorithm:for i = 0 to n-1

for j =0 to n-2

if key[j] > key[j+1] then swap

if no elements swapped in this pass through array, done.

otherwise, continue

O(n)O(n)

Page 75: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Selection Sort, Insertion Sort, and Bubble Sort all have a worst-case time of O(n2), making them impractical for large arrays.

But they are easy to program, easy to debug. Insertion Sort also has good performance when

the array is nearly sorted to begin with. But more sophisticated sorting algorithms are

needed when good performance is needed in all cases for large arrays.

Next time: Merge Sort, Quick Sort, and Radix Sort.

Timing and Other IssuesTiming and Other Issues

Page 76: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Mergesort and Mergesort and QuicksortQuicksort

Page 77: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Sorting algorithmsSorting algorithms

Insertion, selection and bubble sort have quadratic worst-case performance

The faster comparison based algorithm ?

O(nlogn)

Mergesort and Quicksort

Page 78: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Sorting Algorithms Sorting Algorithms and Average Case Number of Comparisonsand Average Case Number of Comparisons

Simple Sorts Straight Selection Sort Bubble Sort Insertion Sort

More Complex Sorts Quick Sort Merge Sort Heap Sort

O(N2)

O(N*log N)

78

Page 79: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

79

Heap Sort Approach Heap Sort Approach

First, make the unsorted array into a heap by satisfying the order property. Then repeat the steps below until there are no more unsorted elements.

Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements.

Reheap the remaining unsorted elements. (This puts the next-largest element into the root position).

Page 80: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

80

After creating the original heap

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

70

60

12

40

30

8

10

values

70

0

60

1

40

3

30

4

12

2

8

5

root

10

6

Page 81: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

81

Swap root element into last place in unsorted array

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

70

60

12

40

30

8

10

values

70

0

60

1

40

3

30

4

12

2

8

5

root

10

6

Page 82: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

82

After swapping root element into its place

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

10

0

60

1

40

3

30

4

12

2

8

5

root

70

6

10

60

12

40

30

8

70 NO NEED TO CONSIDER AGAIN

Page 83: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

83

After reheaping remaining unsorted elements

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

60

0

40

1

10

3

30

4

12

2

8

5

root

70

6

60

40

12

10

30

8

70

Page 84: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

84

Swap root element into last place in unsorted array

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

60

0

40

1

10

3

30

4

12

2

8

5

root

70

6

60

40

12

10

30

8

70

Page 85: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

85

After swapping root element into its place

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

8

0

40

1

10

3

30

4

12

2

root

70

6

8

40

12

10

30

60

70 NO NEED TO CONSIDER AGAIN

60

5

Page 86: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

86

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

40

0

30

1

10

3

6

4

12

2

root

70

6

40

30

12

10

6

60

70

60

5

After reheaping remaining unsorted elements

Page 87: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

87

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

40

0

30

1

10

3

6

4

12

2

root

70

6

40

30

12

10

6

60

70

60

5

Swap root element into last place in unsorted array

Page 88: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

88

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

6

0

30

1

10

3

12

2

root

70

6

60

5

After swapping root element into its place

40

4

6

30

12

10

40

60

70 NO NEED TO CONSIDER AGAIN

Page 89: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

89

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

30

0

10

1

6

3

12

2

root

70

6

60

5

40

4

30

10

12

6

40

60

70

After reheaping remaining unsorted elements

Page 90: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

90

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

30

0

10

1

6

3

12

2

root

70

6

60

5

40

4

30

10

12

6

40

60

70

Swap root element into last place in unsorted array

Page 91: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

91

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

6

0

10

1

12

2

root

70

6

60

5

40

4

After swapping root element into its place

6

10

12

30

40

60

70

30

3 NO NEED TO CONSIDER AGAIN

Page 92: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

92

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

12

0

10

1

6

2

root

70

6

60

5

40

4

12

10

6

30

40

60

70

30

3

After reheaping remaining unsorted elements

Page 93: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

93

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

12

0

10

1

6

2

root

70

6

60

5

40

4

12

10

6

30

40

60

70

30

3

Swap root element into last place in unsorted array

Page 94: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

94

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

6

0

10

1

root

70

6

60

5

40

4

30

3

After swapping root element into its place

NO NEED TO CONSIDER AGAIN

12

2

6

10

12

30

40

60

70

Page 95: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

95

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

10

0

6

1

root

70

6

60

5

40

4

30

3

12

2

10

6

12

30

40

60

70

After reheaping remaining unsorted elements

Page 96: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

96

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

10

0

6

1

root

70

6

60

5

40

4

30

3

12

2

10

6

12

30

40

60

70

Swap root element into last place in unsorted array

Page 97: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

97

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

values

root

70

6

60

5

40

4

30

3

12

2

After swapping root element into its place

6

10

12

30

40

60

70

10

1

6

0

ALL ELEMENTS ARE SORTED

Page 98: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

template <class ItemType >void HeapSort ( ItemType values [ ] , int numValues )

// Post: Sorts array values[ 0 . . numValues-1 ] into ascending // order by key{

int index ; // Convert array values[ 0 . . numValues-1 ] into a heap.

for ( index = numValues/2 - 1 ; index >= 0 ; index-- )

ReheapDown ( values , index , numValues - 1 ) ;

// Sort the array.

for ( index = numValues - 1 ; index >= 1 ; index-- ){

Swap ( values [0] , values [index] );

ReheapDown ( values , 0 , index - 1 ) ;}

}

98

Page 99: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

99

Heap Sort: Heap Sort: How many comparisons?How many comparisons?

24

0

60

1

30

3

40

4

12

2

8

5

root

10

6

15

7

6

8

18

9

In reheap down, an elementis compared with its 2 children (and swapped with the larger). But only one element ateach level makesthis comparison,and a completebinary tree with N nodes has only O(log2N)levels. 70

10

Page 100: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Merge SortMerge Sort

Apply divide-and-conquer to sorting problem Problem: Given n elements, sort elements

into non-decreasing order Divide-and-Conquer:

If n=1 terminate (every one-element list is already sorted)

If n>1, partition elements into two or more sub-collections; sort each; combine into a single sorted list

How do we partition?

Page 101: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Partitioning - Choice 1Partitioning - Choice 1

First n-1 elements into set A, last element set B Sort A using this partitioning scheme recursively

B already sorted

Combine A and B using method Insert() (= insertion into sorted array)

Leads to recursive version of InsertionSort() Number of comparisons: O(n2)

Best case = n-1

Worst case = 2

)1(

2

nnic

n

i

Page 102: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Partitioning - Choice 2Partitioning - Choice 2

Put element with largest key in B, remaining elements in A

Sort A recursively To combine sorted A and B, append B to sorted A

Use Max() to find largest element recursive SelectionSort()

Use bubbling process to find and move largest element to right-most position recursive BubbleSort()

All O(n2)

Page 103: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Partitioning - Choice 3Partitioning - Choice 3

Let’s try to achieve balanced partitioning A gets n/2 elements, B gets rest half Sort A and B recursively Combine sorted A and B using a process

called merge, which combines two sorted lists into one How? We will see soon

Page 104: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

ExampleExample

Partition into lists of size n/2

[10, 4, 6, 3]

[10, 4, 6, 3, 8, 2, 5, 7]

[8, 2, 5, 7]

[10, 4] [6, 3] [8, 2] [5, 7]

[4] [10] [3][6] [2][8] [5][7]

Page 105: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Example Cont’dExample Cont’d Merge

[3, 4, 6, 10]

[2, 3, 4, 5, 6, 7, 8, 10 ]

[2, 5, 7, 8]

[4, 10] [3, 6] [2, 8] [5, 7]

[4] [10] [3][6] [2][8] [5][7]

Page 106: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Static Method mergeSort()Static Method mergeSort()void mergeSort(Comparable []a, int left, int right)

{

// sort a[left:right]

if (left < right)

{// at least two elements

int mid = (left+right)/2; //midpoint

mergeSort(a, left, mid);

mergeSort(a, mid + 1, right);

merge(a, b, left, mid, right); //merge from a to b

copy(b, a, left, right); //copy result back to a

}

}

Page 107: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Merge FunctionMerge Function

Page 108: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

EvaluationEvaluation

Recurrence equation: Assume n is a power of 2

c1 if n=1

T(n) =

2T(n/2) + c2n if n>1, n=2k

Page 109: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

SolutionSolutionBy Substitution:T(n) = 2T(n/2) + c2n

T(n/2) = 2T(n/4) + c2n/2

T(n) = 4T(n/4) + 2 c2n

T(n) = 8T(n/8) + 3 c2n

T(n) = 2iT(n/2i) + ic2n

Assuming n = 2k, expansion halts when we get T(1) on right side; this happens when i=k T(n) = 2kT(1) + kc2n

Since 2k=n, we know k=logn; since T(1) = c1, we get

T(n) = c1n + c2nlogn;

thus an upper bound for TmergeSort(n) is O(nlogn)

Page 110: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AlgorithmQuicksort Algorithm

Given an array of n elements (e.g., integers): If array only contains one element, return Else

pick one element to use as pivot. Partition elements into two sub-arrays:

Elements less than or equal to pivot Elements greater than pivot

Quicksort two sub-arrays Return results

Page 111: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

ExampleExampleWe are given array of n integers to sort:

40 20 10 80 60 50 7 30 100

Page 112: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Pick Pivot ElementPick Pivot ElementThere are a number of ways to pick the pivot element. In this

example, we will use the first element in the array:

40 20 10 80 60 50 7 30 100

Page 113: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Partitioning ArrayPartitioning Array

Given a pivot, partition the elements of the array such that the resulting array consists of:

1. One sub-array that contains elements >= pivot

2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements below/above pivot.

Page 114: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 115: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

Page 116: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

Page 117: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

Page 118: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

Page 119: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

Page 120: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

Page 121: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

Page 122: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 123: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 124: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 125: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 126: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 127: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

Page 128: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 129: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 130: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 131: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 132: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 133: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 134: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 135: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 136: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 137: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 138: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

7 20 10 30 40 50 60 80 100pivot_index = 4

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 139: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Partition ResultPartition Result

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 140: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Recursion: Quicksort Sub-Recursion: Quicksort Sub-arraysarrays

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 141: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time?

Page 142: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time? Recursion:

1. Partition splits array in two sub-arrays of size n/2

2. Quicksort each sub-array

Page 143: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time? Recursion:

1. Partition splits array in two sub-arrays of size n/2

2. Quicksort each sub-array Depth of recursion tree?

Page 144: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time? Recursion:

1. Partition splits array in two sub-arrays of size n/2

2. Quicksort each sub-array

Depth of recursion tree? O(log2n)

Page 145: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time? Recursion:

1. Partition splits array in two sub-arrays of size n/2

2. Quicksort each sub-array

Depth of recursion tree? O(log2n) Number of accesses in partition?

Page 146: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time? Recursion:

1. Partition splits array in two sub-arrays of size n/2

2. Quicksort each sub-array

Depth of recursion tree? O(log2n) Number of accesses in partition? O(n)

Page 147: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n)

Page 148: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time?

Page 149: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort: Worst CaseQuicksort: Worst Case

Assume first element is chosen as pivot. Assume we get array that is already in order:

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 150: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 151: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 152: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 153: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 154: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 155: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 156: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

> data[pivot]<= data[pivot]

Page 157: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time?

Recursion:1. Partition splits array in two sub-arrays:

• one sub-array of size 0• the other sub-array of size n-1

2. Quicksort each sub-array Depth of recursion tree?

Page 158: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time?

Recursion:1. Partition splits array in two sub-arrays:

• one sub-array of size 0• the other sub-array of size n-1

2. Quicksort each sub-array Depth of recursion tree? O(n)

Page 159: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time?

Recursion:1. Partition splits array in two sub-arrays:

• one sub-array of size 0• the other sub-array of size n-1

2. Quicksort each sub-array Depth of recursion tree? O(n) Number of accesses per partition?

Page 160: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time?

Recursion:1. Partition splits array in two sub-arrays:

• one sub-array of size 0• the other sub-array of size n-1

2. Quicksort each sub-array Depth of recursion tree? O(n) Number of accesses per partition? O(n)

Page 161: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time: O(n2)!!!

Page 162: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n) Worst case running time: O(n2)!!! What can we do to avoid worst case?

Page 163: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Improved Pivot SelectionImproved Pivot Selection

Pick median value of three elements from data array:

data[0], data[n/2], and data[n-1].

Use this median value as pivot.

Page 164: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Improving Performance of Improving Performance of QuicksortQuicksort

Improved selection of pivot. For sub-arrays of size 3 or less, apply brute

force search: Sub-array of size 1: trivial Sub-array of size 2:

if(data[first] > data[second]) swap them

Page 165: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix SortRadix Sort

Limit input to fixed-length numbers or words.Represent symbols in some base b.

Each input has exactly d “digits”.

Sort numbers d times, using 1 digit as key.Must sort from least-significant to most-significant digit.

Must use any “stable” sort, keeping equal-keyed items in same order.

Page 166: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c c a a a c b b a b c c a b b aa a c

Input data:

Page 167: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c c a a a c b b a b c c a b b aa a c

Input data:

a b c

Pass 1: Looking at rightmost position.

Page 168: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a

b a c c a a a c b b a b c c a b b aa a c

Input data:

a b c

Place into appropriate pile.

Page 169: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c

c a a a c b b a b c c a b b aa a c

Input data:

a b c

Place into appropriate pile.

Page 170: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c

c a a

a c b b a b c c a b b aa a c

Input data:

a b c

Place into appropriate pile.

Page 171: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c

c a a

a c b

b a b c c a b b aa a c

Input data:

a b c

Place into appropriate pile.

Page 172: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a c

c a a

a c b

b a b

c c a

b b a

a a c

Input data:

a b c

Place into appropriate pile.

Page 173: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a b a cc a a a c b b a bc c a b b a a a c

a b c

Join piles. Pass 2 looks at next position.

Page 174: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b a

b a c

c a a

a c bb a b

c c a

b b a

a a c

a b c

Place into appropriate pile.

Page 175: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b c

Join piles. Pass 3 looks at next position.

b a cc a a b a b a a c a b a b b a a c bc c a

Page 176: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b c

Join piles. Pass 3 looks at next position.

b a c

c a ab a ba a c

a b a

b b aa c b

c c a

Page 177: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort ExampleRadix Sort Example

a b c

Join piles.

b a c c a ab a ba a c a b a b b aa c b c c a

Page 178: Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical

Radix Sort AlgorithmRadix Sort Algorithm

rsort(A,n):

for d := 0 to n-1

/* Stable sort A, using digit position d as the key. */

for i := 1 to |A|

add A[i] to end of list ((A[i]>>d) mod b)

A = join lists 0..b-1

(dn) time, where d is taken to be a constant.