Sorting
description
Transcript of Sorting
![Page 1: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/1.jpg)
SORTINGChapter 8
![Page 2: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/5.jpg)
Using Java Sorting Methods (cont.)
![Page 6: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/6.jpg)
Declaring a Generic Method
![Page 7: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/7.jpg)
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 8: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/8.jpg)
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 9: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/9.jpg)
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 10: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/10.jpg)
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 11: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/11.jpg)
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 12: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/12.jpg)
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 13: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/13.jpg)
Example 8.2public class Person implements Comparable<Person> { // Data Fields /* The last name */ private String lastName; /* The first name */ private String firstName; /* Birthday represented by an integer from 1 to 366 */ private int birthDay;
// Methods /** Compares two Person objects based on names. The result is based on the last names if they are different; otherwise, it is based on the first names. @param obj The other Person @return A negative integer if this person’s name
precedes the other person’s name; 0 if the names are the same; a positive integer if this person’s name follows the other person’s name.
*/ @Override public int compareTo(Person other) { // Compare this Person to other using last names. int result = lastName.compareTo(other.lastName); // Compare first names if last names are the same. if (result == 0)
return firstName.compareTo(other.firstName); else
return result; }
// Other methods . . .}
![Page 14: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/14.jpg)
Example 8.3import java.util.Comparator;
public class ComparePerson implements Comparator<Person> { /** Compare two Person objects based on birth date.
@param left The left-hand side of the comparison @param right The right-hand side of the comparison @return A negative integer if the left person’s birthday
precedes the right person’s birthday; 0 if the birthdays are the same; a positive integer if the left person’s birthday follows the right person’s birthday.
*/ @Override public int compare(Person left, Person right) {
return left.getBirthDay() - right.getBirthDay(); }}
![Page 15: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/15.jpg)
Section 8.2
Selection Sort
![Page 16: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/16.jpg)
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 17: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/17.jpg)
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 18: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/18.jpg)
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 19: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/19.jpg)
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 20: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/20.jpg)
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 21: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/21.jpg)
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 22: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/22.jpg)
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 23: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/23.jpg)
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 24: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/24.jpg)
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 25: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/25.jpg)
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 26: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/26.jpg)
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 27: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/27.jpg)
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 28: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/28.jpg)
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 29: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/29.jpg)
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 30: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/30.jpg)
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 31: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/31.jpg)
Trace of Selection Sort Refinement
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill
posMin
next
0 1 2 3 4
![Page 32: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/32.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin
next
0 1 2 3 4
fill
![Page 33: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/33.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 0
next
0 1 2 3 4
fillposMin
![Page 34: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/34.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 0
next 1
0 1 2 3 4
fillposMin
next
![Page 35: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/35.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 0
next 1
0 1 2 3 4
fillposMin
next
![Page 36: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/36.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 0
next 2
0 1 2 3 4
fillposMin
next
![Page 37: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/37.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 0
next 2
0 1 2 3 4
fillposMin
next
![Page 38: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/38.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 2
next 2
0 1 2 3 4
fillposMinnext
![Page 39: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/39.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 40: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/40.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 41: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/41.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 42: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/42.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 43: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/43.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill35 65 30 60 20
n 5
fill 0
posMin 4
next 4
0 1 2 3 4
fillposMinnext
![Page 44: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/44.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 0
posMin 4
next 4
0 1 2 3 4
fillposMinnext
![Page 45: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/45.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 4
next 4
0 1 2 3 4
fill
posMinnext
![Page 46: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/46.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 1
next 4
0 1 2 3 4
fillposMin
next
![Page 47: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/47.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 1
next 2
0 1 2 3 4
fillposMin
next
![Page 48: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/48.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 1
next 2
0 1 2 3 4
fillposMin
next
![Page 49: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/49.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 2
next 2
0 1 2 3 4
fill
posMinnext
![Page 50: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/50.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 51: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/51.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 52: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/52.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 53: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/53.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 65 30 60 35
n 5
fill 1
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 54: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/54.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 1
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 55: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/55.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 56: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/56.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 2
next 4
0 1 2 3 4
fillposMin
next
![Page 57: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/57.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 58: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/58.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 2
next 3
0 1 2 3 4
fillposMin
next
![Page 59: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/59.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 3
next 3
0 1 2 3 4
fillposMinnext
![Page 60: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/60.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 3
next 4
0 1 2 3 4
fill
posMinnext
![Page 61: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/61.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 3
next 4
0 1 2 3 4
fill
posMinnext
![Page 62: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/62.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 65 60 35
n 5
fill 2
posMin 4
next 4
0 1 2 3 4
fillposMinnext
![Page 63: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/63.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 2
posMin 4
next 4
0 1 2 3 4
fillposMinnext
![Page 64: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/64.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 4
next 4
0 1 2 3 4
fillposMinnext
![Page 65: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/65.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 3
next 4
0 1 2 3 4
fillposMin
next
![Page 66: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/66.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 3
next 4
0 1 2 3 4
fillposMin
next
![Page 67: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/67.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 3
next 4
0 1 2 3 4
fillposMin
next
![Page 68: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/68.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 3
next 4
0 1 2 3 4
fillposMin
next
![Page 69: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/69.jpg)
Trace of Selection Sort Refinement (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill20 30 35 60 65
n 5
fill 3
posMin 3
next 4
0 1 2 3 4
![Page 70: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/70.jpg)
Analysis of Selection Sort
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill
This loop is performed n-1
times
![Page 71: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/71.jpg)
Analysis of Selection Sort (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill
There are n-1 exchanges
![Page 72: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/72.jpg)
Analysis of Selection Sort (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. 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 73: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/73.jpg)
Analysis of Selection Sort (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. Exchange the item at posMin with the one
at fill
![Page 74: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/74.jpg)
Analysis of Selection Sort (cont.)
1. for fill = 0 to n – 2 do2. Initialize posMin to fill3. for next = fill + 1 to n – 1 do4. if the item at next is less than the
item at posMin5. Reset posMin to next6. 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 75: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/75.jpg)
Code for Selection Sort (cont.) Listing 8.1(SelectionSort.java, pages
426 - 427)
![Page 76: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/76.jpg)
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 77: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/77.jpg)
Section 8.3
Bubble Sort
![Page 78: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/78.jpg)
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 79: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/79.jpg)
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 80: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/80.jpg)
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 81: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/81.jpg)
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 82: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/82.jpg)
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 83: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/83.jpg)
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 84: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/84.jpg)
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 85: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/85.jpg)
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 86: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/86.jpg)
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 87: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/87.jpg)
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 88: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/88.jpg)
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 89: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/89.jpg)
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 90: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/90.jpg)
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 91: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/91.jpg)
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 92: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/92.jpg)
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 93: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/93.jpg)
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 94: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/94.jpg)
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 95: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/95.jpg)
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 96: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/96.jpg)
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 97: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/97.jpg)
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 98: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/98.jpg)
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 99: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/99.jpg)
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 100: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/100.jpg)
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 101: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/101.jpg)
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 102: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/102.jpg)
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 103: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/103.jpg)
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 104: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/104.jpg)
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 105: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/105.jpg)
Code for Bubble Sort Listing 8.2 (BubbleSort.java, pages
430 - 431)
![Page 106: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/106.jpg)
Section 8.4
Insertion Sort
![Page 107: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/107.jpg)
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 108: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/108.jpg)
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 109: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/109.jpg)
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 110: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/110.jpg)
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 111: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/111.jpg)
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 112: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/112.jpg)
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 113: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/113.jpg)
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 114: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/114.jpg)
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 115: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/115.jpg)
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 116: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/116.jpg)
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 117: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/117.jpg)
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 118: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/118.jpg)
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 119: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/119.jpg)
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 120: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/120.jpg)
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 121: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/121.jpg)
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 122: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/122.jpg)
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 123: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/123.jpg)
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 124: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/124.jpg)
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 125: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/125.jpg)
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 126: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/126.jpg)
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 127: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/127.jpg)
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 128: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/128.jpg)
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 129: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/129.jpg)
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 130: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/130.jpg)
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 131: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/131.jpg)
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 132: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/132.jpg)
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 133: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/133.jpg)
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 134: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/134.jpg)
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 135: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/135.jpg)
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 136: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/136.jpg)
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 137: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/137.jpg)
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 138: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/138.jpg)
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 139: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/139.jpg)
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 140: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/140.jpg)
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 141: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/141.jpg)
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 142: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/142.jpg)
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 143: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/143.jpg)
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 144: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/144.jpg)
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 145: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/145.jpg)
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 146: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/146.jpg)
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 147: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/147.jpg)
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 148: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/148.jpg)
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 149: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/149.jpg)
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 150: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/150.jpg)
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 151: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/151.jpg)
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 152: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/152.jpg)
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 153: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/153.jpg)
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 154: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/154.jpg)
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 155: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/155.jpg)
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 156: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/156.jpg)
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 157: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/157.jpg)
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 158: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/158.jpg)
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 159: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/159.jpg)
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 160: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/160.jpg)
Code for Insertion Sort Listing 8.3 (InsertionSort.java, page
434)
![Page 161: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/161.jpg)
Section 8.5
Comparison of Quadratic Sorts
![Page 162: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/162.jpg)
Comparison of Quadratic Sorts
![Page 163: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/163.jpg)
Comparison of Quadratic Sorts (cont.)
Comparison of growth rates
![Page 164: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/164.jpg)
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 165: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/165.jpg)
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 166: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/166.jpg)
Comparisons versus Exchanges
In Java, an exchange requires a switch of two object references using a third object reference as an intermediary
A comparison requires an execution of a compareTo method
The cost of a comparison depends on its complexity, but is generally more costly than an exchange
For some other languages, an exchange may involve physically moving information rather than swapping object references. In these cases, an exchange may be more costly than a comparison
![Page 167: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/167.jpg)
Section 8.6
Shell Sort: A Better Insertion Sort
![Page 168: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/168.jpg)
Shell Sort: A Better Insertion Sort
A Shell sort is a type of insertion sort, but with O(n3/2) or better performance than the O(n2) sorts
It is named after its discoverer, Donald Shell
A Shell sort can be thought of as a divide-and-conquer approach to insertion sort
Instead of sorting the entire array, Shell sort sorts many smaller subarrays using insertion sort before sorting the entire array
![Page 169: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/169.jpg)
Trace of Shell Sort
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
![Page 170: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/170.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 1
![Page 171: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/171.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 2
![Page 172: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/172.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 3
![Page 173: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/173.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 4
![Page 174: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/174.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 5
![Page 175: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/175.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 6
![Page 176: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/176.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 7
![Page 177: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/177.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 75 55 90
[5] [6] [7] [8] [9]
85 34 45 62 57
[10][11][12][13][14]
65
[15]
gap value 7
subarray 1
Sort subarray 1
![Page 178: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/178.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 34 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7
subarray 2
Sort subarray 2
![Page 179: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/179.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 34 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 3
subarray 3
![Page 180: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/180.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 34 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 4
subarray 4
![Page 181: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/181.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 60
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 34 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 5
subarray 5
![Page 182: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/182.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 60 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 5
subarray 5
![Page 183: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/183.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
90 70 57 55 90
[5] [6] [7] [8] [9]
85 60 45 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 6
subarray 6
![Page 184: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/184.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 70 57 55 90
[5] [6] [7] [8] [9]
85 60 90 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 6
subarray 6
![Page 185: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/185.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 70 57 55 90
[5] [6] [7] [8] [9]
85 60 90 62 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 7
subarray 7
![Page 186: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/186.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 7
subarray 7
![Page 187: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/187.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 1
subarray 1
![Page 188: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/188.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 7Sort subarray 2
subarray 2
![Page 189: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/189.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 7 Sort on smaller gap value next
![Page 190: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/190.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3 Sort on smaller gap value
![Page 191: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/191.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 192: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/192.jpg)
Trace of Shell Sort (cont.)
40 35 80 75 34
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 193: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/193.jpg)
Trace of Shell Sort (cont.)
40 34 80 75 35
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 194: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/194.jpg)
Trace of Shell Sort (cont.)
40 34 80 75 35
[0] [1] [2] [3] [4]
45 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 195: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/195.jpg)
Trace of Shell Sort (cont.)
40 34 45 75 35
[0] [1] [2] [3] [4]
80 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 196: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/196.jpg)
Trace of Shell Sort (cont.)
40 34 45 75 35
[0] [1] [2] [3] [4]
80 62 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 197: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/197.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
80 75 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 198: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/198.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
80 75 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 199: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/199.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
80 75 57 55 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 200: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/200.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 80 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 201: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/201.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 80 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 202: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/202.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 80 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 203: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/203.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 80 90
[5] [6] [7] [8] [9]
85 60 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 204: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/204.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
85 80 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 205: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/205.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
85 80 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 206: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/206.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
85 80 90 70 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 207: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/207.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
70 80 90 85 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 2
subarray 2
![Page 208: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/208.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
70 80 90 85 75
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 209: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/209.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 3
subarray 3
![Page 210: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/210.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 90
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
65
[15]
gap value 3Sort subarray 1
subarray 1
![Page 211: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/211.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 75 57 60 65
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 3Sort subarray 1
subarray 1
![Page 212: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/212.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 3Sort subarray 1
subarray 1
![Page 213: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/213.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 3 Sort on gap value of 1
(a regular insertion sort)
![Page 214: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/214.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 215: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/215.jpg)
Trace of Shell Sort (cont.)
40 34 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 216: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/216.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 217: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/217.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 218: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/218.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 219: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/219.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 220: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/220.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 221: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/221.jpg)
Trace of Shell Sort (cont.)
34 40 45 62 35
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 222: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/222.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 62
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 223: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/223.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 62
[0] [1] [2] [3] [4]
55 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 224: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/224.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
62 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 225: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/225.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
62 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 226: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/226.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
62 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 227: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/227.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
62 65 57 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 228: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/228.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 62 65 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 229: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/229.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 62 65 60 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 230: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/230.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 231: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/231.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 232: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/232.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 233: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/233.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 75
[5] [6] [7] [8] [9]
70 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 234: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/234.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 235: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/235.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 236: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/236.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 237: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/237.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 238: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/238.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 239: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/239.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 90 85 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 240: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/240.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 85 90 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 241: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/241.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 85 90 80
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 242: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/242.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 80 85 90
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 243: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/243.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 85 80 90
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 244: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/244.jpg)
Trace of Shell Sort (cont.)
34 35 40 45 55
[0] [1] [2] [3] [4]
57 60 62 65 70
[5] [6] [7] [8] [9]
75 75 85 80 90
[10][11][12][13][14]
90
[15]
gap value 1 Sort on gap value of 1
(a regular insertion sort)
![Page 245: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/245.jpg)
Shell Sort Algorithm
Shell Sort Algorithm
1. Set the initial value of gap to n / 2
2. while gap > 0
3. for each array element from position gap to the last element
4. Insert this element where it belongs in its subarray.
5. if gap is 2, set it to 1
6. else gap = gap / 2.2. // chosen by experimentation
![Page 246: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/246.jpg)
Shell Sort Algorithm (cont.)
Refinement of Step 4, the Insertion Step
4.1 nextPos is the position of the element to insert
4.2 Save the value of the element to insert in nextVal
4.3 while nextPos > gap and the element at nextPos – gap > nextVal
4.4 Shift the element at nextPos – gap to position nextPos
4.5 Decrement nextPos by gap
4.6 Insert nextVal at nextPos
![Page 247: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/247.jpg)
Analysis of Shell Sort Because the behavior of insertion sort is
closer to O(n) than O(n2) when an array is nearly sorted, presorting speeds up later sorting
This is critical when sorting large arrays where the O(n2) performance becomes significant
![Page 248: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/248.jpg)
Analysis of Shell Sort (cont.) A general analysis of Shell sort is an open research
problem in computer science Performance depends on how the decreasing
sequence of values for gap is chosen If successive powers of 2 are used for gap,
performance is O(n2) If successive values for gap are based on Hibbard's
sequence, 2k – 1 (i.e. 31, 15, 7, 3, 1)
it can be proven that the performance is O(n3/2) Other sequences give similar or better performance
![Page 249: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/249.jpg)
Analysis of Shell Sort (cont.)
![Page 250: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/250.jpg)
Code for Shell Sort Listing 8.4 (ShellSort.java, pages 439
– 440)
![Page 251: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/251.jpg)
Section 8.7
Merge Sort
![Page 252: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/252.jpg)
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 253: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/253.jpg)
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 254: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/254.jpg)
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 255: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/255.jpg)
Code for Merge Listing 8.5 (MergeSort.java, page 442)
![Page 256: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/256.jpg)
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 257: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/257.jpg)
(recursive) Algorithm for Merge Sort
![Page 258: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/258.jpg)
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 259: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/259.jpg)
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 260: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/260.jpg)
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 261: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/261.jpg)
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 262: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/262.jpg)
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 263: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/263.jpg)
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 264: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/264.jpg)
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 265: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/265.jpg)
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 266: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/266.jpg)
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 267: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/267.jpg)
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 268: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/268.jpg)
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 269: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/269.jpg)
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 270: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/270.jpg)
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 271: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/271.jpg)
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 272: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/272.jpg)
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 273: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/273.jpg)
Analysis of Merge Sort (cont.)
![Page 274: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/274.jpg)
Code for Merge Sort Listing 8.6 (MergeSort.java, pages 445
– 446)
![Page 275: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/275.jpg)
Section 8.8
Heapsort
![Page 276: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/276.jpg)
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 277: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/277.jpg)
First Version of a Heapsort Algorithm
When used as a priority queue, a heap maintains a smallest value at the top
The following algorithm places an array's data into a heap, then removes each heap item (O(n log n)) and moves it back into
the array This version of the algorithm requires n extra storage
locationsHeapsort Algorithm: First Version
1. Insert each value from the array to be sorted into a priority queue (heap).
2. Set i to 0
3. while the priority queue is not empty
4. Remove an item from the queue and insert it back into the array at position i
5. Increment i
![Page 278: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/278.jpg)
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
Then, move the top item to the bottom of the
heap reheap, ignoring the item moved to the
bottom
![Page 279: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/279.jpg)
Trace of Heapsort
89
76 74
37 32 39 66
20 26 18 28 29 6
![Page 280: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/280.jpg)
Trace of Heapsort (cont.)
89
76 74
37 32 39 66
20 26 18 28 29 6
![Page 281: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/281.jpg)
Trace of Heapsort (cont.)
89
76 74
37 32 39 66
20 26 18 28 29 6
![Page 282: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/282.jpg)
Trace of Heapsort (cont.)
6
76 74
37 32 39 66
20 26 18 28 29 89
![Page 283: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/283.jpg)
Trace of Heapsort (cont.)
76
6 74
37 32 39 66
20 26 18 28 29 89
![Page 284: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/284.jpg)
Trace of Heapsort (cont.)
76
37 74
6 32 39 66
20 26 18 28 29 89
![Page 285: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/285.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 286: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/286.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 287: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/287.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 288: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/288.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 289: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/289.jpg)
Trace of Heapsort (cont.)
29
37 74
26 32 39 66
20 6 18 28 76 89
![Page 290: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/290.jpg)
Trace of Heapsort (cont.)
74
37 29
26 32 39 66
20 6 18 28 76 89
![Page 291: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/291.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 292: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/292.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 293: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/293.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 294: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/294.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 295: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/295.jpg)
Trace of Heapsort (cont.)
28
37 66
26 32 39 29
20 6 18 74 76 89
![Page 296: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/296.jpg)
Trace of Heapsort (cont.)
66
37 28
26 32 39 29
20 6 18 74 76 89
![Page 297: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/297.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 298: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/298.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 299: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/299.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 300: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/300.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 301: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/301.jpg)
Trace of Heapsort (cont.)
18
37 39
26 32 28 29
20 6 66 74 76 89
![Page 302: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/302.jpg)
Trace of Heapsort (cont.)
39
37 18
26 32 28 29
20 6 66 74 76 89
![Page 303: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/303.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 304: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/304.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 305: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/305.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 306: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/306.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 307: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/307.jpg)
Trace of Heapsort (cont.)
6
37 29
26 32 28 18
20 39 66 74 76 89
![Page 308: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/308.jpg)
Trace of Heapsort (cont.)
37
6 29
26 32 28 18
20 39 66 74 76 89
![Page 309: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/309.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 310: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/310.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 311: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/311.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 312: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/312.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 313: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/313.jpg)
Trace of Heapsort (cont.)
20
32 29
26 6 28 18
37 39 66 74 76 89
![Page 314: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/314.jpg)
Trace of Heapsort (cont.)
32
20 29
26 6 28 18
37 39 66 74 76 89
![Page 315: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/315.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 316: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/316.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 317: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/317.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 318: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/318.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 319: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/319.jpg)
Trace of Heapsort (cont.)
18
26 29
20 6 28 32
37 39 66 74 76 89
![Page 320: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/320.jpg)
Trace of Heapsort (cont.)
29
26 18
20 6 28 32
37 39 66 74 76 89
![Page 321: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/321.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 322: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/322.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 323: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/323.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 324: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/324.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 325: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/325.jpg)
Trace of Heapsort (cont.)
18
26 28
20 6 29 32
37 39 66 74 76 89
![Page 326: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/326.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 327: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/327.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 328: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/328.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 329: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/329.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 330: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/330.jpg)
Trace of Heapsort (cont.)
6
26 18
20 28 29 32
37 39 66 74 76 89
![Page 331: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/331.jpg)
Trace of Heapsort (cont.)
26
6 18
20 28 29 32
37 39 66 74 76 89
![Page 332: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/332.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 333: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/333.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 334: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/334.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 335: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/335.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 336: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/336.jpg)
Trace of Heapsort (cont.)
6
20 18
26 28 29 32
37 39 66 74 76 89
![Page 337: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/337.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 338: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/338.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 339: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/339.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 340: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/340.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 341: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/341.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 342: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/342.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 343: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/343.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 344: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/344.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 345: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/345.jpg)
Trace of Heapsort (cont.)
6
18 20
26 28 29 32
37 39 66 74 76 89
![Page 346: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/346.jpg)
Trace of Heapsort (cont.)
6
18 20
26 28 29 32
37 39 66 74 76 89
![Page 347: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/347.jpg)
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 348: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/348.jpg)
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 349: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/349.jpg)
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 350: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/350.jpg)
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 351: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/351.jpg)
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 352: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/352.jpg)
Code for Heapsort Listing 8.7 (HeapSort.java, pages 449 -
451)
![Page 353: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/353.jpg)
Section 8.9
Quicksort
![Page 354: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/354.jpg)
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 355: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/355.jpg)
Trace of Quicksort
44 75 23 43 55 12 64 77 33
![Page 356: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/356.jpg)
Trace of Quicksort (cont.)
44 75 23 43 55 12 64 77 33
Arbitrarily select the first element
as the pivot
![Page 357: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/357.jpg)
Trace of Quicksort (cont.)
55 75 23 43 44 12 64 77 33
Swap the pivot with the element in the middle
![Page 358: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/358.jpg)
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 359: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/359.jpg)
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 360: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/360.jpg)
Quicksort Example(cont.)
12 33 23 43 44 55 64 77 75
44 is now in its correct position
![Page 361: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/361.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Now apply quicksort recursively to the
two subarrays
![Page 362: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/362.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 12
![Page 363: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/363.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 12
![Page 364: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/364.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 33
![Page 365: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/365.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
![Page 366: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/366.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
![Page 367: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/367.jpg)
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 368: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/368.jpg)
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 369: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/369.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 55
![Page 370: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/370.jpg)
Trace of Quicksort (cont.)Pivot value =
64
12 23 33 43 44 55 64 77 75
![Page 371: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/371.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 77
![Page 372: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/372.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Pivot value = 77
![Page 373: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/373.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Pivot value = 77
![Page 374: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/374.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Left subarray has single value;
it is sorted
![Page 375: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/375.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
![Page 376: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/376.jpg)
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 377: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/377.jpg)
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 378: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/378.jpg)
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 379: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/379.jpg)
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 380: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/380.jpg)
Code for Quicksort Listing 8.8 (QuickSort.java, pages 453
- 454)
![Page 381: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/381.jpg)
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 382: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/382.jpg)
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 383: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/383.jpg)
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 384: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/384.jpg)
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 385: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/385.jpg)
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 386: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/386.jpg)
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 387: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/387.jpg)
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 388: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/388.jpg)
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 389: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/389.jpg)
Trace of Partitioning (cont.)
Exchange these values
up down
44 75 23 43 55 12 64 77 33
![Page 390: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/390.jpg)
Trace of Partitioning (cont.)
Exchange these values
44 33 23 43 55 12 64 77 75
![Page 391: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/391.jpg)
Trace of Partitioning (cont.)
Repeat
44 33 23 43 55 12 64 77 75
![Page 392: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/392.jpg)
Trace of Partitioning (cont.)
Find first value at left end greater than pivot
up
44 33 23 43 55 12 64 77 75
![Page 393: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/393.jpg)
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 394: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/394.jpg)
Trace of Partitioning (cont.)
Exchange
44 33 23 43 12 55 64 77 75
![Page 395: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/395.jpg)
Trace of Partitioning (cont.)
Repeat
44 33 23 43 12 55 64 77 75
![Page 396: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/396.jpg)
Trace of Partitioning (cont.)
Find first element at left end greater than pivot
up
44 33 23 43 12 55 64 77 75
![Page 397: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/397.jpg)
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 398: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/398.jpg)
Trace of Partitioning (cont.)
Since down has "passed" up, do not exchange
updown
44 33 23 43 12 55 64 77 75
![Page 399: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/399.jpg)
Trace of Partitioning (cont.)
Exchange the pivot value with the value at down
updown
44 33 23 43 12 55 64 77 75
![Page 400: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/400.jpg)
Trace of Partitioning (cont.)
Exchange the pivot value with the value at down
down
12 33 23 43 44 55 64 77 75
![Page 401: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/401.jpg)
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 402: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/402.jpg)
Algorithm for Partitioning
![Page 403: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/403.jpg)
Code for partition when Pivot is the largest or smallest value
![Page 404: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/404.jpg)
Code for partition (cont.) Listing 8.9 (QuickSort1, page 457)
![Page 405: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/405.jpg)
Revised Partition Algorithm Quicksort is O(n2) when each split yields
one empty subarray, which 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 the these items as the
pivot
![Page 406: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/406.jpg)
Trace of Revised Partitioning
44 75 23 43 55 12 64 77 33
![Page 407: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/407.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 55 12 64 77 33
middlefirst last
![Page 408: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/408.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 55 12 64 77 33
Sort these values
middlefirst last
![Page 409: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/409.jpg)
Trace of Revised Partitioning (cont.)
33 75 23 43 44 12 64 77 55
Sort these values
middlefirst last
![Page 410: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/410.jpg)
Trace of Revised Partitioning (cont.)
33 75 23 43 44 12 64 77 55
Exchange middle with
first
middlefirst last
![Page 411: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/411.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 33 12 64 77 55
Exchange middle with
first
middlefirst last
![Page 412: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/412.jpg)
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 413: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/413.jpg)
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 414: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/414.jpg)
Code for Revised partition Method
Listing 8.10 (QuickSort2, page 459)
![Page 415: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/415.jpg)
Section 8.10
Testing the Sort Algorithms
![Page 416: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/416.jpg)
Testing the Sort Algorithms Use a variety of test cases
small and large arrays arrays in random order arrays that are already sorted arrays with duplicate values
Compare performance on each type of array
![Page 417: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/417.jpg)
Driver to Test Sort Algorithms Listing 8.11(TestSort.java, page 461)
![Page 418: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/418.jpg)
Section 8.11
The Dutch National Flag Problem (optional)
![Page 419: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/419.jpg)
The Dutch National Flag Problem
A variety of partitioning algorithms for quicksort have been published
A popular variation uses a single left-to-right scan of the array
Edsger W. Dijkstra developed a partitioning algorithm using both techniques to partition an array into three segments
![Page 420: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/420.jpg)
Problem Partition a disordered three-color flag into
the appropriate three segments by writing software for an unscrambling machine
![Page 421: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/421.jpg)
Analysis The unscrambling machine can
look at one thread in the flag and determine its color
swap the position of two threads in the flag execute while loops and if statements
![Page 422: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/422.jpg)
Design – Loop Invariant As with quicksort, values between first
and up are less than or equal to the pivot Values between down and last are
greater than the pivot Values between up and down are
unknown Initially,
The unknown region is the entire array (first == up and down == last)
![Page 423: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/423.jpg)
Design Have four regions:
Red (light gray) white blue unknown (dark gray)
Flag is complete: if 0 <= i < red then threads[i] is red if white < i <= blue then threads[i] is white if blue < i < HEIGHT then threads[i] is blue
If red <= i <= white then the flag is not complete and the color is unknown
![Page 424: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/424.jpg)
Algorithm
![Page 425: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/425.jpg)
Preserving the Loop Invariant Assume we know the value of threads[white] either leave it where it is (in the white
region if it is white) or place it in the region where it belongs
![Page 426: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/426.jpg)
Preserving the Loop Invariant Three cases
1. The color of threads[white] is white Decrement the value of white, increasing the size of the white region
by one thread2. The color of threads[white] is red
The color of threads[red] is unknown Swap the thread at threads[red] with the thread at
threads[white], adding the thread to the end of the red region and reducing the size of the unknown region by one thread
3. The color of threads[white] is blue The color of threads[blue] is white Swap the thread at threads[white] with the thread at
threads[blue] Decrement both blue and white, inserting the thread at the
beginning of the blue region and reducing the size of the unknown region by one thread
![Page 427: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/427.jpg)
Implementation Listing 8.12 (DutchNationalFlag.java,
page 465)
![Page 428: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/428.jpg)
Summary
Comparison of Sort Algorithms
![Page 429: Sorting](https://reader035.fdocuments.in/reader035/viewer/2022070420/56815ebc550346895dcd3e93/html5/thumbnails/429.jpg)
Sort Review