Top Banner
Week 11 Sorting Algorithms
102

Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Dec 24, 2015

Download

Documents

Eugenia Pope
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Week 11 Sorting Algorithms

Page 2: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Sorting

Page 3: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Sorting Algorithms

• A sorting algorithm is an algorithm that puts elements of a list in a certain order.

• We need sorting algorithm in lots of areas: Science Engineering Business……

Page 5: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Quadratic 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 6: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 7: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Selection sort

Page 8: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Page 9: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Page 10: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Page 11: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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.

0

10

20

30

40

50

60

70

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

Sorted side Unsorted side

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

Page 12: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Find the smallest element in the unsorted side.

Sorted side Unsorted side

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

Page 13: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 14: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 15: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 16: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 17: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 18: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 19: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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 20: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

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 21: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Selection sort

• animation

Page 22: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 23: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Selection 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 24: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Selection 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 25: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 26: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 27: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Insertion sort

Page 28: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 29: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 30: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 31: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 32: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

0

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 33: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 34: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

The Insertionsort AlgorithmThe Insertionsort Algorithm

• Sometimes we are lucky twice in a row.

0

10

20

30

40

50

60

70

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

Sorted side Unsorted side

Page 35: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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

Sorted side Unsorted side

Page 36: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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

10

20

30

40

50

60

70

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

10

20

30

40

50

60

70

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

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

Page 37: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]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

10

20

30

40

50

60

70

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

0

10

20

30

40

50

60

70

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

Page 38: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]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

10

20

30

40

50

60

70

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

0

10

20

30

40

50

60

70

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

Page 39: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]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

10

20

30

40

50

60

70

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

0

10

20

30

40

50

60

70

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

Page 40: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]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

10

20

30

40

50

60

70

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

0

10

20

30

40

50

60

70

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

Page 41: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]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

10

20

30

40

50

60

70

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

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

Sorted side Unsorted side

Page 42: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

How to Insert One ElementHow to Insert One Element

0

10

20

30

40

50

60

70

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

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

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

Sorted side Unsorted side

Page 43: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

0

10

20

30

40

50

60

70

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

Sorted ResultSorted Result

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

Page 44: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Insertion Sort

• animation

Page 45: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 46: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Insertion 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 47: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Insertion 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 48: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Insertion 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 49: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 50: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 51: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Bubble sort

Page 52: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 56: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 57: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 58: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 59: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 60: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 61: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 62: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 64: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 65: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 66: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 67: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 68: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 69: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 71: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 72: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 73: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 74: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 75: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 76: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 77: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Bubble sort

• animation

Page 78: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 79: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

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 80: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Bubble 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 81: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Bubble 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 82: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

• 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 understand, easy to program, easy to debug.

• Insertion Sort also has good performance when the array is nearly sorted to begin with.

Timing and Other Issues Timing and Other Issues

Page 83: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Next class

• More sophisticated sorting algorithms are needed when good performance is needed in all cases for large arrays.

• Next class: Merge Sort, Heap Sort. – O(N log N)

• Review the “recursion algorithm” and “heap structure” we discussed last week.

Page 84: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Merge sort

Page 85: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Merge sort

1. Partitioning: Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).

2. Merging: Recursively merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.

Recursion

Page 86: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

86

5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6

Page 87: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

87

0

0

0

Page 88: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Main IndexMain Index ContentsContents88

The Merge Algorithm Example

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

s u b lis t Bs u b lis t A

firs t las tm id

The merge algorithm takes a sequence of elements in a vector v having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid.

Page 89: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Main IndexMain Index ContentsContents8989 Main IndexMain Index ContentsContents

The Merge Algorithm… (Cont…)

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

7

s u b lis t Bs u b lis t A

in d exA in d exB

t em p Vect o r

7 1 0

t em p Vect o r

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

s u b lis t Bs u b lis t A

in d exA in d exrB

Page 90: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Main IndexMain Index ContentsContents9090 Main IndexMain Index ContentsContents

The Merge Algorithm… (Cont…)

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

7 1 0 1 2

s u b lis t Bs u b lis t A

in d exA in d exB

t em p Vect o r

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

7 1 0 1 2 1 7 1 9 2 1 2 5

s u b lis t Bs u b lis t A

in d exA in d exB

t em p Vect o r

Page 91: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Main IndexMain Index ContentsContents91

The Merge Algorithm… (Cont…)

7 1 0 1 9 2 5 1 2 1 7 2 1 3 0 4 8

7 1 0 1 2 1 7 1 9 2 1 2 5 3 0 4 8

s u b lis t Bs u b lis t A

in d exA in d exB

t em p Vect o r

las t

7 10 12 17 19 21 25 30 48

t e m p Ve c t o r

7 10 12 17 19 21 25 30 48

firs t la s t

Page 92: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Merge sort

• animation

Page 93: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

mergeSort()

Page 94: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Main IndexMain Index ContentsContents9494 Main IndexMain Index ContentsContents

Partitioning and Merging of Sublists in mergeSort()

(25 10 7 19 3 48 12 17 56 30 21)

(25 10 7 19 3 )

(7 19 3 )(25 10 )

[3 7 10 19 25]

(48 12 17 56 30 21 )

(56 30 21 )(48 12 17 )

[12 17 21 30 48 56]

[3 7 10 12 17 19 21 25 30 48 56]

(10 )

[10 25]

(25 ) (19 3 )(7 )

[3 7 19]

(56 )

[21 30 56]

(12 17 )(48 )

[12 17 48]

(21 )(3 )

[3 19]

(19 )

(30 21 )

[21 30]

(30 )(17 )

[12 17]

(12 )

Page 95: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Complexity of merge sort

Page 96: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Complexity of merge sort

Worst, best, average caseComplexity: O(n log n)

Page 97: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Heap sort

Page 98: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Heaps• For a maximum heap, the value of a parent is greater than or

equal to the value of each of its children. • For a minimum heap, the value of the parent is less than or

equal to the value of each of its children.• We assume that a heap is a maximum heap.

Page 99: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Heap sort

1. Build a heap of N elements – the maximum element is at the top of the heap

2. Perform N Delete operations– the elements are extracted in sorted order

Page 100: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Heap sort

• animation

Page 101: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Heap sort – Running Time Analysis

1. Build a binary heap of N elements O(N), see slide 21 of lecture ‘heap’

2. Perform N Delete operations– Each Delete operation takes O(log N), See slide 23 of lecture ‘heap’

Total: O(N log N)

Total time complexity: O(N) + O(N log N) O(N log N)

Page 102: Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Reading

• Chapter 7