Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. A test harness program fro testing sorting methods is provided with the rest

ID: 3765149 • Letter: 1

Question

1. A test harness program fro testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch10 package. The program includes a swap method that is used by all of the sorting methods to swap array elements. a.Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. b.Implement your approach. c.Test your new program by running the selectionSort method. Your program should report 49 swaps.

Explanation / Answer

def selection_sort!(a) for i in 0..a.size-2 do # Invariant: a[0..i-1] contain smallest values in order smallest = i for j in i+1..a.size-1 do # Invariant: a[smallest] is the smallest value in a[i..j-1] if a[j] < a[smallest] smallest = j end end a[i], a[smallest] = a[smallest], a[i] end end