+ All Categories
Home > Documents > SORTING Chapter 8. Chapter Objectives To learn how to use the standard sorting methods in the...

SORTING Chapter 8. Chapter Objectives To learn how to use the standard sorting methods in the...

Date post: 20-Jan-2018
Category:
Upload: donald-ryan
View: 217 times
Download: 0 times
Share this document with a friend
Description:
Introduction  Sorting entails arranging data in order  Familiarity with sorting algorithms is an important programming skill  The study of sorting algorithms provides insight into  problem solving techniques such as divide and conquer  the analysis and comparison of algorithms which perform the same task
284
SORTING Chapter 8
Transcript
Page 1: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

SORTINGChapter 8

Page 2: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Chapter Objectives To learn how to use the standard sorting methods in

the Java API To learn how to implement the following sorting

algorithms: selection sort bubble sort insertion sort Shell sort merge sort heapsort quicksort

To understand the differences in performance of these algorithms, and which to use for small, medium, and large arrays

Page 3: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Introduction Sorting entails arranging data in order Familiarity with sorting algorithms is an

important programming skill The study of sorting algorithms provides

insight into problem solving techniques such as divide

and conquer the analysis and comparison of algorithms

which perform the same task

Page 4: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Using Java Sorting Methods The Java API provides a class Arrays with

several overloaded sort methods for different array types

The Collections class provides similar sorting methods for Lists

Sorting methods for arrays of primitive types are based on the quicksort algorithm

Sorting methods for arrays of objects and Lists are based on the merge sort algorithm

Both algorithms are O(n log n)

Page 5: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Using Java Sorting Methods (cont.)

Page 6: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Bubble Sort

Page 7: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Bubble Sort A quadratic sort Compares adjacent array elements and

exchanges their values if they are out of order

Smaller values bubble up to the top of the array and larger values sink to the bottom; hence the name

Page 8: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Interview Question What is the most efficient way to sort a

million integers? It really depends…

Quick-sort is the “safe” answer.

What about sorting a trillion integers?

8

Page 9: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

60

42

75

83

27

[0]

[1]

[2]

[3]

[4]

Page 10: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

60

42

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 0

Page 11: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 12: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 13: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 14: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 15: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 16: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 2

At the end of pass 1, the last item (index [4]) is guaranteed

to be in its correct position. There is no need to test it

again in the next pass

Page 17: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 18: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 19: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 20: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 1

Page 21: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 1

Page 22: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 0

Page 23: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 0

Page 24: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 1

Page 25: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 1

Page 26: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 0

Page 27: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Page 28: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Page 29: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Where n is the length of the array, after the completion of n – 1

passes (4, in this example) the array is sorted

Page 30: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Sometimes an array will be sorted before

n – 1 passes. This can be detected if there are no exchanges made during a pass through the array

Page 31: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. do2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. while the array is not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

The algorithm can be modified to detect exchanges (next)

Page 32: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Bubble Sort (cont.)

1. Initialize exchanges to false2. for each pair of adjacent array

elements3. if the values in a pair are out of

order4. Exchange the values5. Set exchanges to true

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

The algorithm can be modified to detect exchanges

Page 33: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Bubble Sort The number of comparisons and exchanges is

represented by(n – 1) + (n – 2) + ... + 3 + 2 + 1

Worst case: number of comparisons is O(n2) number of exchanges is O(n2)

If the array is sorted early, the later comparisons and exchanges are not performed and performance is improved

Page 34: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Bubble Sort (cont.)

The best case occurs when the array is sorted already one pass is required (O(n) comparisons) no exchanges are required (O(1) exchanges)

Bubble sort works best

on arrays nearly sorted and worst

on inverted arrays (elements are in reverse sorted order)

Page 35: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Bubble Sort Listing 8.2 (BubbleSort.java, pages

430 - 431)

Page 36: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Selection Sort

Page 37: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Selection Sort Selection sort is relatively easy to understand It sorts an array by making several passes

through the array,. It selects a next smallest item in the array

each time and placing it where it belongs in the array

All items to be sorted must be Comparable objects. For example, any int values must be wrapped in Integer objects

Page 38: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sortn = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

35 65 30 60 20n 5

fill

posMin

0 1 2 3 4

Page 39: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

35 65 30 60 20n 5

fill 0

posMinfill

0 1 2 3 4

Page 40: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

35 65 30 60 20n 5

fill 0

posMin 4fill posMin

0 1 2 3 4

Page 41: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 65 30 60 35n 5

fill 0

posMin 4fill posMin

0 1 2 3 4

Page 42: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 65 30 60 35n 5

fill 1

posMin 4fill posMin

0 1 2 3 4

Page 43: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 65 30 60 35n 5

fill 1

posMin 2fill

posMin

0 1 2 3 4

Page 44: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 65 60 35n 5

fill 1

posMin 2fill

posMin

0 1 2 3 4

Page 45: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 65 60 35n 5

fill 2

posMin 2fillposMin

0 1 2 3 4

Page 46: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 65 60 35n 5

fill 2

posMin 4fill posMin

0 1 2 3 4

Page 47: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 35 60 65n 5

fill 2

posMin 4fill posMin

0 1 2 3 4

Page 48: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 35 60 65n 5

fill 3

posMin 4fill posMin

0 1 2 3 4

Page 49: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 35 60 65n 5

fill 3

posMin 3fill

posMin

0 1 2 3 4

Page 50: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 35 60 65n 5

fill 3

posMin 3fill

posMin

0 1 2 3 4

Page 51: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Selection Sort (cont.)

n = number of elements in the array

1. for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

20 30 35 60 65n 5

fill 3

posMin 3

0 1 2 3 4

Page 52: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Selection Sort

This loop is performed n-1

times

n = number of elements in the array

1.for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

Page 53: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Selection Sort (cont.)

There are n-1 exchanges

n = number of elements in the array

1.for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

Page 54: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Selection Sort (cont.)

n = number of elements in the array

1.for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

(n – 1 - fill) comparisons are performed for each value of fill and can be

represented by the following series:

(n-1) + (n-2) + ... + 3 + 2 + 1

Page 55: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Selection Sort (cont.)

For very large n we can ignore all but the significant term in the expression,

so the number of•comparisons is O(n2)•exchanges is O(n)

An O(n2) sort is called a quadratic sort

n = number of elements in the array

1.for fill = 0 to n – 2 do2. Set posMin to the subscript of a smallest

item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill

Page 56: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Selection Sort (cont.) Listing 8.1(SelectionSort.java, pages

426 - 427)

Page 57: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Insertion Sort

Page 58: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Insertion Sort Another quadratic sort, insertion sort, is

based on the technique used by card players to arrange a hand of cards The player keeps the cards that have been

picked up so far in sorted order When the player picks up a new card, the

player makes room for the new card and then inserts it in its proper place

Page 59: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort1. for each array element from the

second (nextPos = 1) to the last2. Insert the element at nextPos

where it belongs in the array, increasing the length of the sorted subarray by 1 element

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

To adapt the insertion algorithm to an array that is filled with data, we start with a sorted

subarray consisting of only the first element

Page 60: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextPos

Page 61: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextPos

Page 62: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextPos

Page 63: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextPos

Page 64: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextPos

Page 65: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextPos

Page 66: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextPos

Page 67: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextPos

Page 68: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos -

Page 69: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Page 70: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

nextPos 1

nextVal

loop position

Page 71: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

nextPos 1

nextVal

nextPosloop position

Page 72: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 73: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 74: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 75: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 76: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 77: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 78: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

loop position

Page 79: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 25

loop position nextPos

Page 80: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 81: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 82: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 83: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 84: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 85: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 86: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 87: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 88: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 89: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 90: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 15

loop position nextPos

Page 91: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 92: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 93: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 94: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 95: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 96: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 97: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 98: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 99: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 100: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

Page 101: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 20

loop position nextPos

Page 102: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 103: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 104: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 105: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 106: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 107: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 108: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Insertion Sort Refinement (cont.)

1. for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 17. Insert nextVal at nextPos

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

Page 109: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Additional example with visual card animation http://courses.cs.vt.edu/~csonline/

Algorithms/Lessons/InsertionCardSort/insertioncardsort.swf

109

Page 110: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Insertion Sort The insertion step is performed n – 1

times In the worst case, all elements in the

sorted subarray are compared to nextVal for each insertion

The maximum number of comparisons then will be:

1 + 2 + 3 + ... + (n – 2) + (n – 1) which is O(n2)

Page 111: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Insertion Sort (cont.)

In the best case (when the array is sorted already), only one comparison is required for each insertion

In the best case, the number of comparisons is O(n) The number of shifts performed during an insertion is one

less than the number of comparisons Or, when the new value is the smallest so far, it is the

same as the number of comparisons A shift in an insertion sort requires movement of only 1

item, while an exchange in a bubble or selection sort involves a temporary item and the movement of three items The item moved may be a primitive or an object reference The objects themselves do not change their locations

Page 112: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Insertion Sort Listing 8.3 (InsertionSort.java, page

434)

Page 113: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Comparison of Quadratic Sorts

Page 114: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Comparison of Quadratic Sorts

Page 115: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Comparison of Quadratic Sorts (cont.)

Comparison of growth rates

Page 116: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Comparison of Quadratic Sorts (cont.)

Insertion sort gives the best performance for most arrays takes advantage of any partial sorting in the array

and uses less costly shifts Bubble sort generally gives the worst

performance—unless the array is nearly sorted Big-O analysis ignores constants and overhead

None of the quadratic search algorithms are particularly good for large arrays (n > 1000)

The best sorting algorithms provide n log n average case performance

Page 117: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Comparison of Quadratic Sorts (cont.)

All quadratic sorts require storage for the array being sorted

However, the array is sorted in place While there are also storage

requirements for variables, for large n, the size of the array dominates and extra space usage is O(1)

Page 118: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Merge Sort

Page 119: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Merge A merge is a common data processing

operation performed on two sequences of data with the following characteristics Both sequences contain items with a common compareTo method

The objects in both sequences are ordered in accordance with this compareTo method

• The result is a third sequence containing all the data from the first two sequences

Page 120: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Merge AlgorithmMerge Algorithm

1. Access the first item from both sequences.

2. while not finished with either sequence

3. Compare the current items from the two sequences, copy the smallercurrent item to the output sequence, and access the next item from theinput sequence whose item was copied.

4. Copy any remaining items from the first sequence to the output sequence.

5. Copy any remaining items from the second sequence to the output sequence.

Page 121: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Merge For two input sequences each containing

n elements, each element needs to move from its input sequence to the output sequence

Merge time is O(n) Space requirements

The array cannot be merged in place Additional space usage is O(n)

Page 122: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Merge Listing 8.5 (MergeSort.java, page 442)

Page 123: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Interview Question Given two Lists of integers, one sorted in

ascending order and the other sorted in descending order, write an algorithm (in Java), which returns a combined List that is sorted.

Given two sorted integer arrays, write an algorithm to get back the intersection. What if one array is really larger than the

other array?

123

Page 124: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Merge Sort We can modify merging to sort a single,

unsorted array1. Split the array into two halves2. Sort the left half3. Sort the right half4. Merge the two

This algorithm can be written with a recursive step

Page 125: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

(recursive) Algorithm for Merge Sort

Page 126: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

Page 127: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30

50 60 45 30

Page 128: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3050

50 60

60

Page 129: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3050 60

60

Page 130: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3045

45 30

30

Page 131: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30

45

45 304530

Page 132: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3045 304530

30 45 50 60

Page 133: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

Page 134: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

90 20

Page 135: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

90 20

20 90

Page 136: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 80 15

80 15

Page 137: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 80 15

80 15

15 80

Page 138: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 15 80

15 20 80 90

Page 139: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 85 1530 45 50 60 15 20 80 90

15 20 30 45 50 60 80 90

Page 140: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Merge Sort Each backward step requires a

movement of n elements from smaller-size arrays to larger arrays; the effort is O(n)

The number of steps which require merging is log n because each recursive call splits the array in half

The total effort to reconstruct the sorted array through merging is O(n log n)

Page 141: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Merge Sort (cont.) 

Page 142: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Merge Sort Listing 8.6 (MergeSort.java, pages 445

– 446)

Page 143: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Heapsort

Page 144: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Heapsort Merge sort time is O(n log n) but still

requires, temporarily, n extra storage locations

Heapsort does not require any additional storage

As its name implies, heapsort uses a heap to store the array

Page 145: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Revising the Heapsort Algorithm

In heaps we've used so far, each parent node value was not greater than the values of its children

We can build a heap so that each parent node value is not less than its children. i.e., a Max Heap.

Then, move the top item to the bottom of the

heap reheap, ignoring the item moved to the

bottom

Page 146: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort

89

76 74

37 32 39 66

20 26 18 28 29 6

Page 147: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

89

76 74

37 32 39 66

20 26 18 28 29 6

Page 148: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

89

76 74

37 32 39 66

20 26 18 28 29 6

Page 149: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

76 74

37 32 39 66

20 26 18 28 29 89

Page 150: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

6 74

37 32 39 66

20 26 18 28 29 89

Page 151: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

37 74

6 32 39 66

20 26 18 28 29 89

Page 152: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

Page 153: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

Page 154: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

Page 155: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

Page 156: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

37 74

26 32 39 66

20 6 18 28 76 89

Page 157: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

74

37 29

26 32 39 66

20 6 18 28 76 89

Page 158: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

Page 159: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

Page 160: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

Page 161: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

Page 162: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

28

37 66

26 32 39 29

20 6 18 74 76 89

Page 163: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

66

37 28

26 32 39 29

20 6 18 74 76 89

Page 164: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

66

37 39

26 32 28 29

20 6 18 74 76 89

Page 165: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

66

37 39

26 32 28 29

20 6 18 74 76 89

Page 166: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

66

37 39

26 32 28 29

20 6 18 74 76 89

Page 167: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

66

37 39

26 32 28 29

20 6 18 74 76 89

Page 168: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

37 39

26 32 28 29

20 6 66 74 76 89

Page 169: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

39

37 18

26 32 28 29

20 6 66 74 76 89

Page 170: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

39

37 29

26 32 28 18

20 6 66 74 76 89

Page 171: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

39

37 29

26 32 28 18

20 6 66 74 76 89

Page 172: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

39

37 29

26 32 28 18

20 6 66 74 76 89

Page 173: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

39

37 29

26 32 28 18

20 6 66 74 76 89

Page 174: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

37 29

26 32 28 18

20 39 66 74 76 89

Page 175: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

37

6 29

26 32 28 18

20 39 66 74 76 89

Page 176: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

37

32 29

26 6 28 18

20 39 66 74 76 89

Page 177: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

37

32 29

26 6 28 18

20 39 66 74 76 89

Page 178: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

37

32 29

26 6 28 18

20 39 66 74 76 89

Page 179: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

37

32 29

26 6 28 18

20 39 66 74 76 89

Page 180: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

20

32 29

26 6 28 18

37 39 66 74 76 89

Page 181: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

32

20 29

26 6 28 18

37 39 66 74 76 89

Page 182: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

32

26 29

20 6 28 18

37 39 66 74 76 89

Page 183: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

32

26 29

20 6 28 18

37 39 66 74 76 89

Page 184: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

32

26 29

20 6 28 18

37 39 66 74 76 89

Page 185: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

32

26 29

20 6 28 18

37 39 66 74 76 89

Page 186: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

26 29

20 6 28 32

37 39 66 74 76 89

Page 187: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

26 18

20 6 28 32

37 39 66 74 76 89

Page 188: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

26 28

20 6 18 32

37 39 66 74 76 89

Page 189: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

26 28

20 6 18 32

37 39 66 74 76 89

Page 190: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

26 28

20 6 18 32

37 39 66 74 76 89

Page 191: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

29

26 28

20 6 18 32

37 39 66 74 76 89

Page 192: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

26 28

20 6 29 32

37 39 66 74 76 89

Page 193: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

28

26 18

20 6 29 32

37 39 66 74 76 89

Page 194: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

28

26 18

20 6 29 32

37 39 66 74 76 89

Page 195: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

28

26 18

20 6 29 32

37 39 66 74 76 89

Page 196: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

28

26 18

20 6 29 32

37 39 66 74 76 89

Page 197: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

26 18

20 28 29 32

37 39 66 74 76 89

Page 198: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

26

6 18

20 28 29 32

37 39 66 74 76 89

Page 199: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

26

20 18

6 28 29 32

37 39 66 74 76 89

Page 200: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

26

20 18

6 28 29 32

37 39 66 74 76 89

Page 201: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

26

20 18

6 28 29 32

37 39 66 74 76 89

Page 202: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

26

20 18

6 28 29 32

37 39 66 74 76 89

Page 203: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

20 18

26 28 29 32

37 39 66 74 76 89

Page 204: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

20

6 18

26 28 29 32

37 39 66 74 76 89

Page 205: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

20

6 18

26 28 29 32

37 39 66 74 76 89

Page 206: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

20

6 18

26 28 29 32

37 39 66 74 76 89

Page 207: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

20

6 18

26 28 29 32

37 39 66 74 76 89

Page 208: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

6 20

26 28 29 32

37 39 66 74 76 89

Page 209: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

6 20

26 28 29 32

37 39 66 74 76 89

Page 210: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

6 20

26 28 29 32

37 39 66 74 76 89

Page 211: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

18

6 20

26 28 29 32

37 39 66 74 76 89

Page 212: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

18 20

26 28 29 32

37 39 66 74 76 89

Page 213: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Heapsort (cont.)

6

18 20

26 28 29 32

37 39 66 74 76 89

Page 214: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Revising the Heapsort Algorithm

If we implement the heap as an array each element

removed will be placed at the end of the array, and

the heap part of the array decreases by one element

Page 215: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm for In-Place Heapsort

Algorithm for In-Place Heapsort

1. Build a heap by rearranging the elements in an unsorted array

2. while the heap is not empty

3. Remove the first item from the heap by swapping it with the last item in the heap and restoring the heap property

Page 216: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm to Build a Heap Start with an array table of length table.length

Consider the first item to be a heap of one item

Next, consider the general case where the items in array table from 0 through n-1 form a heap and the items from n through table.length – 1 are not in the heap

Page 217: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm to Build a Heap (cont.)

Refinement of Step 1 for In-Place Heapsort

1.1 while n is less than table.length

1.2 Increment n by 1. This inserts a new item into the heap

1.3 Restore the heap property

Page 218: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Heapsort Because a heap is a complete binary

tree, it has log n levels Building a heap of size n requires finding

the correct location for an item in a heap with log n levels

Each insert (or remove) is O(log n) With n items, building a heap is O(n log

n) No extra storage is needed

Page 219: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Heapsort Listing 8.7 (HeapSort.java, pages 449 -

451)

Page 220: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Quicksort

Page 221: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Quicksort Developed in 1962 Quicksort selects a specific value called a pivot

and rearranges the array into two parts (called partioning) all the elements in the left subarray are less than

or equal to the pivot all the elements in the right subarray are larger

than the pivot The pivot is placed between the two subarrays

The process is repeated until the array is sorted

Page 222: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort

44 75 23 43 55 12 64 77 33

Page 223: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

44 75 23 43 55 12 64 77 33

Arbitrarily select the first element

as the pivot

Page 224: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

55 75 23 43 44 12 64 77 33

Swap the pivot with the element in the middle

Page 225: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

55 75 23 43 44 12 64 77 33

Partition the elements so that all values less than or equal to the pivot are to the left,

and all values greater than the pivot are to

the right

Page 226: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Partition the elements so that all values less than or equal to the pivot are to the left,

and all values greater than the pivot are to

the right

Page 227: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Quicksort Example(cont.)

12 33 23 43 44 55 64 77 75

44 is now in its correct position

Page 228: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Now apply quicksort recursively to the

two subarrays

Page 229: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 12

Page 230: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 12

Page 231: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 33

Page 232: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Page 233: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Page 234: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Left and right subarrays have

single values; they are sorted

Page 235: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Left and right subarrays have

single values; they are sorted

Page 236: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 55

Page 237: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)Pivot value =

64

12 23 33 43 44 55 64 77 75

Page 238: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 77

Page 239: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Pivot value = 77

Page 240: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Pivot value = 77

Page 241: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Left subarray has single value;

it is sorted

Page 242: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Page 243: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm for Quicksort We describe how to do the partitioning later The indexes first and last are the end points of

the array being sorted The index of the pivot after partitioning is pivIndex

Algorithm for Quicksort

1. if first < last then

2. Partition the elements in the subarray first . . . last so that the pivot value is in its correct place (subscript pivIndex)

3. Recursively apply quicksort to the subarray first . . . pivIndex - 1

4. Recursively apply quicksort to the subarray pivIndex + 1 . . . last

Page 244: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Quicksort If the pivot value is a random value selected

from the current subarray, then statistically half of the items in the subarray

will be less than the pivot and half will be greater If both subarrays have the same number of

elements (best case), there will be log n levels of recursion

At each recursion level, the partitioning process involves moving every element to its correct position—n moves

Quicksort is O(n log n), just like merge sort

Page 245: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Quicksort (cont.) The array split may not be the best case,

i.e. 50-50 An exact analysis is difficult (and beyond

the scope of this class), but, the running time will be bounded by a constant x n log n

Page 246: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Analysis of Quicksort (cont.) A quicksort will give very poor behavior if,

each time the array is partitioned, a subarray is empty.

In that case, the sort will be O(n2) Under these circumstances, the overhead

of recursive calls and the extra run-time stack storage required by these calls makes this version of quicksort a poor performer relative to the quadratic sorts We’ll discuss a solution later

Page 247: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Quicksort Listing 8.8 (QuickSort.java, pages 453

- 454)

Page 248: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm for Partitioning

44 75 23 43 55 12 64 77 33

If the array is randomly ordered, it does not matter which element is the pivot.

For simplicity we pick the element with subscript first

Page 249: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

If the array is randomly ordered, it does not matter which element is the pivot.

For simplicity we pick the element with subscript first

Page 250: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

For visualization purposes, items less than or equal to

the pivot will be colored blue; items greater than the

pivot will be colored light purple

Page 251: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

For visualization purposes, items less than or equal to

the pivot will be colored blue; items greater than the

pivot will be colored light purple

Page 252: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Search for the first value at the left end of the array that

is greater than the pivot value

44 75 23 43 55 12 64 77 33

Page 253: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Search for the first value at the left end of the array that

is greater than the pivot value

up

44 75 23 43 55 12 64 77 33

Page 254: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Then search for the first value at the right end of the

array that is less than or equal to the pivot value

up

44 75 23 43 55 12 64 77 33

Page 255: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Then search for the first value at the right end of the

array that is less than or equal to the pivot value

up down

44 75 23 43 55 12 64 77 33

Page 256: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Exchange these values

up down

44 75 23 43 55 12 64 77 33

Page 257: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Exchange these values

44 33 23 43 55 12 64 77 75

Page 258: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Repeat

44 33 23 43 55 12 64 77 75

Page 259: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Find first value at left end greater than pivot

up

44 33 23 43 55 12 64 77 75

Page 260: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Find first value at right end less than or equal to pivot

up down

44 33 23 43 55 12 64 77 75

Page 261: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Exchange

44 33 23 43 12 55 64 77 75

Page 262: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Repeat

44 33 23 43 12 55 64 77 75

Page 263: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Find first element at left end greater than pivot

up

44 33 23 43 12 55 64 77 75

Page 264: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Find first element at right end less than or equal to

pivot

updown

44 33 23 43 12 55 64 77 75

Page 265: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Since down has "passed" up, do not exchange

updown

44 33 23 43 12 55 64 77 75

Page 266: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Exchange the pivot value with the value at down

updown

44 33 23 43 12 55 64 77 75

Page 267: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

Exchange the pivot value with the value at down

down

12 33 23 43 44 55 64 77 75

Page 268: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Partitioning (cont.)

The pivot value is in the correct position; return the value of down and assign it to the pivot index pivIndex

down

12 33 23 43 44 55 64 77 75

Page 269: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm for Partitioning

Page 270: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for partition when Pivot is the largest or smallest value

Page 271: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for partition (cont.) Listing 8.9 (QuickSort1, page 457)

Page 272: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Interview Question Describe an efficient algorithm that finds

the element of a set that would be at position k if the elements were sorted. Solution based on quicksort

Why is quicksort better than merge sort? Does in place matter?

272

Page 273: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Revised Partition Algorithm Quicksort is O(n2) when each split yields

one empty subarray. This is the case when the array is presorted

A better solution is to pick the pivot value in a way that is less likely to lead to a bad split Use three references: first, middle, last Select the median of these items as the

pivot

Page 274: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning

44 75 23 43 55 12 64 77 33

Page 275: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

44 75 23 43 55 12 64 77 33

middlefirst last

Page 276: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

44 75 23 43 55 12 64 77 33

Sort these values

middlefirst last

Page 277: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

33 75 23 43 44 12 64 77 55

Sort these values

middlefirst last

Page 278: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

33 75 23 43 44 12 64 77 55

Exchange middle with

first

middlefirst last

Page 279: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

44 75 23 43 33 12 64 77 55

Exchange middle with

first

middlefirst last

Page 280: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Trace of Revised Partitioning (cont.)

44 75 23 43 33 12 64 77 55

Run the partition algorithm using the first element

as the pivot

Page 281: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Algorithm for Revised partition Method

Algorithm for Revised partition Method

1. Sort table[first], table[middle], and table[last]

2. Move the median value to table[first] (the pivot value) by exchanging table[first] and table[middle].

3. Initialize up to first and down to last

4. do

5. Increment up until up selects the first element greater than the pivot value or up has reached last

6. Decrement down until down selects the first element less than or equal to the pivot value or down has reached first

7. if up < down then

8. Exchange table[up] and table[down]

9. while up is to the left of down

10. Exchange table[first] and table[down]

11. Return the value of down to pivIndex

Page 282: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Code for Revised partition Method

Listing 8.10 (QuickSort2, page 459)

Page 283: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Summary

Comparison of Sort Algorithms

Page 284: SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java…

Sort Review

From Ryan:https://www.youtube.com/watch?v=kPRA0W1kECg


Recommended