Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List...

Post on 13-Jan-2016

217 views 0 download

transcript

SortingSorting

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.

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.

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

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]

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]

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]

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]

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]

The Selection Sort Algorithm

Find the smallest element in the unsorted side.

Sorted side Unsorted side

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

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]

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]

The Selection Sort AlgorithmThe Selection Sort Algorithm

The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

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

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

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]

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]

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

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]

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;

}}

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

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)

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)

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)

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]

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

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

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

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

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

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

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

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]

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]

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]

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]

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]

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

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

Sorted ResultSorted Result

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

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

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

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)

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)

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)

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]

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?

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!

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?

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.

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?

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.

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?

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!

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?

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!

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

}}

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

}}

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

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)

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

Mergesort and Mergesort and QuicksortQuicksort

Sorting algorithmsSorting algorithms

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

The faster comparison based algorithm ?

O(nlogn)

Mergesort and Quicksort

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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)

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

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]

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]

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

}

}

Merge FunctionMerge Function

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

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)

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

ExampleExampleWe are given array of n integers to sort:

40 20 10 80 60 50 7 30 100

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

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.

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

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

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

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

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

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

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]

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]

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.

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.

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.

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.

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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]

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]

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

What is best case running time?

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

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?

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)

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?

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)

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

Best case running time: O(n log2n)

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

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

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

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

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

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

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

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

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

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]

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?

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)

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?

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)

Quicksort AnalysisQuicksort Analysis

Assume that keys are random, uniformly distributed.

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

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?

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.

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

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.

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:

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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

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.