sorting chapter 8 cs340 1. chapter objectives to learn how to use the standard sorting methods in...

103
SORTING Chapter 8 CS340 1

Upload: shannon-andrews

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340 1

SORTING

Chapter 8

Page 2: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

2

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 arrays, and large arrays

Page 3: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

3

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 into the analysis and comparison of

algorithms which perform the same task

Page 4: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

4

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 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

5

Declaring a Generic Method

Page 6: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

6

Declaring a Generic Method (cont.)

Sample declarations:

public static <T> void sort(T[] items, Comparator<? super T> comp)

<T> represents the generic parameter for the sort method

Page 7: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

7

Declaring a Generic Method (cont.)

Sample declarations:

public static <T> void sort(T[] items, Comparator<? super T> comp)

<T> should also appear in the

method parameter list

Page 8: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

8

Declaring a Generic Method (cont.)

Sample declarations:

public static <T> void sort(T[] items, Comparator<? super T> comp)

The second method parameter means that comp must be an object

that implements the Comparator interface for type T or for a superclass

of type T

Page 9: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

9

Declaring a Generic Method (cont.)

Sample declarations:

public static <T> void sort(T[] items, Comparator<? super T> comp)

For example, you can define a class that

implements Comparator<Number> and use it to sort an array of Integer objects or an

array of Double objects

Page 10: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

10

Declaring a Generic Method (cont.)

Sample declarations:

public static <T extends Comparable<T>> void sort(List<T> list)

<T extends Comparable<T>>

means that generic parameter T must

implement the interface Comparable<T>

Page 11: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

11

Declaring a Generic Method (cont.)

Sample declarations:

public static <T extends Comparable<T>> void sort(List<T> list)

The method parameter list (the object being

sorted) is of type List<T>

Page 12: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

12

CS340

Section 8.2

Selection Sort

Page 13: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

13

Selection Sort

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

through the array, selecting a next smallest item in the array each time and placing it where it belongs in the array While the sort algorithms are not limited to arrays,

throughout this chapter we will sort arrays for simplicity

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

Page 14: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

14

Trace of Selection Sort

n = number of elements in the array

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

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill35 65 30 60 20

n 5

fill

posMin

0 1 2 3 4

Page 15: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

15

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 the

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 16: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

16

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 the

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 17: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

17

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 the

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 18: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

18

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 the

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill20 65 30 60 35

n 5

fill 1

posMin 4fill posMin

0 1 2 3 4

Page 19: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

19

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 the

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 20: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

20

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 the

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill20 30 65 60 35

n 5

fill 1

posMin 2fill

posMin

0 1 2 3 4

Page 21: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

21

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 the

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill20 30 65 60 35

n 5

fill 2

posMin 2fill

posMin

0 1 2 3 4

Page 22: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

22

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 the

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 23: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

23

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 the

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 24: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

24

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 the

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill20 30 35 60 65

n 5

fill 3

posMin 4fill posMin

0 1 2 3 4

Page 25: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

25

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 the

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 26: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

26

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 the

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 27: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

27

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 the

smallest item in the subarray starting at subscript fill

3. Exchange the item at posMin with the one at fill20 30 35 60 65

n 5

fill 3

posMin 3

0 1 2 3 4

Page 28: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

28

Trace of Selection Sort Refinement

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill

posMin

next

0 1 2 3 4

Page 29: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

29

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin

next

0 1 2 3 4

fill

Page 30: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

30

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 0

next

0 1 2 3 4

fill

posMin

Page 31: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

31

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 0

next 1

0 1 2 3 4

fill

posMin

next

Page 32: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

32

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 0

next 1

0 1 2 3 4

fill

posMin

next

Page 33: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

33

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 0

next 2

0 1 2 3 4

fill

posMin

next

Page 34: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

34

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 0

next 2

0 1 2 3 4

fill

posMin

next

Page 35: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

35

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 2

next 2

0 1 2 3 4

fill

posMin

next

Page 36: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

36

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 2

next 3

0 1 2 3 4

fill

posMin

next

Page 37: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

37

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 2

next 3

0 1 2 3 4

fill

posMin

next

Page 38: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

38

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 39: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

39

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 40: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

40

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

35 65 30 60 20

n 5

fill 0

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 41: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

41

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 0

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 42: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

42

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 43: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

43

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 1

next 4

0 1 2 3 4

fill

posMin

next

Page 44: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

44

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 1

next 2

0 1 2 3 4

fill

posMin

next

Page 45: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

45

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 1

next 2

0 1 2 3 4

fill

posMin

next

Page 46: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

46

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 2

next 2

0 1 2 3 4

fill

posMin

next

Page 47: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

47

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 2

next 3

0 1 2 3 4

fill

posMinnext

Page 48: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

48

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 2

next 3

0 1 2 3 4

fill

posMinnext

Page 49: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

49

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 50: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

50

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 65 30 60 35

n 5

fill 1

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 51: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

51

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 1

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 52: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

52

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 53: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

53

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 2

next 4

0 1 2 3 4

fill

posMin

next

Page 54: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

54

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 2

next 3

0 1 2 3 4

fill

posMin

next

Page 55: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

55

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 2

next 3

0 1 2 3 4

fill

posMin

next

Page 56: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

56

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 3

next 3

0 1 2 3 4

fill

posMin

next

Page 57: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

57

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 58: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

58

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 59: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

59

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 65 60 35

n 5

fill 2

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 60: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

60

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 2

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 61: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

61

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 4

next 4

0 1 2 3 4

fill

posMin

next

Page 62: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

62

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 63: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

63

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 64: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

64

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 65: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

65

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 3

next 4

0 1 2 3 4

fill

posMin

next

Page 66: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

66

Trace of Selection Sort Refinement (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

20 30 35 60 65

n 5

fill 3

posMin 3

next 4

0 1 2 3 4

Page 67: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

67

Analysis of Selection Sort

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

This loop is performed n-1

times

Page 68: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

68

Analysis of Selection Sort (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

There are n-1 exchanges

Page 69: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

69

Analysis of Selection Sort (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

This comparison is performed (n – 1 - fill)

times for each value of fill and can be

represented by the following series:

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

Page 70: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

70

Analysis of Selection Sort (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

 

Page 71: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

71

Analysis of Selection Sort (cont.)

1. for fill = 0 to n – 2 do

2. Initialize posMin to fill

3. for next = fill + 1 to n – 1 do

4. if the item at next is less than the item at posMin

5. Reset posMin to next

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

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

Page 72: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

72

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

426 - 427)

Page 73: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

73

Making Sort Methods Generic To avoid a warning message about an

unchecked call to compareTo, change the method heading topublic static <T extends Comparable<T>> void sort(T[] table {

and change the variable temp from Comparable to type TT temp = table[fill];

Page 74: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

74

CS340

Section 8.3

Bubble Sort

Page 75: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

75

Bubble Sort

Also 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 76: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

76

Trace of Bubble Sort

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

60

42

75

83

27

[0]

[1]

[2]

[3]

[4]

Page 77: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

77

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

60

42

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 0

Page 78: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

78

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 79: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

79

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 80: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

80

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 81: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

81

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

83

27

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 82: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

82

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 1

exchanges made 1

Page 83: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

83

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in 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 84: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

84

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 85: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

85

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 86: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

86

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

75

27

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 0

Page 87: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

87

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 1

Page 88: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

88

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 2

exchanges made 1

Page 89: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

89

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 0

Page 90: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

90

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

60

27

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 0

Page 91: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

91

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 1

Page 92: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

92

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 3

exchanges made 1

Page 93: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

93

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

42

27

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 0

Page 94: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

94

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Page 95: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

95

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in 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 96: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

96

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in not sorted

27

42

60

75

83

[0]

[1]

[2]

[3]

[4]

pass 4

exchanges made 1

Page 97: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

97

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in 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 98: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

98

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in 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 99: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

99

Trace of Bubble Sort (cont.)

1. do

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. while the array in 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 100: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

100

Trace of Bubble Sort (cont.)

1. Initialize exchanges to false

2. for each pair of adjacent array elements

3. if the values in a pair are out of order

4. Exchange the values

5. 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 101: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

101

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)

Compared to selection sort with its O(n2) comparisons and O(n) exchanges, bubble sort usually performs worse

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

Page 102: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

102

Analysis of Bubble Sort (cont.)

The best case occurs when the array is already sorted 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 103: SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following

CS340

103

Code for Bubble Sort

Listing 8.2 (BubbleSort.java, pages 430 - 431)