SORTING Chapter 8 CS340 1. Chapter Objectives To learn how to use the standard sorting methods in...
-
Upload
shannon-andrews -
Category
Documents
-
view
217 -
download
0
Transcript of SORTING Chapter 8 CS340 1. Chapter Objectives To learn how to use the standard sorting methods in...
CS340 1
SORTING
Chapter 8
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
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
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)
CS340
5
Declaring a Generic Method
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
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
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
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
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>
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>
12
CS340
Section 8.2
Selection Sort
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
CS340
72
Code for Selection Sort (cont.) Listing 8.1(SelectionSort.java, pages
426 - 427)
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];
74
CS340
Section 8.3
Bubble Sort
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
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]
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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)
CS340
103
Code for Bubble Sort
Listing 8.2 (BubbleSort.java, pages 430 - 431)