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

Questions 1 and 2 refer to the incomplete definition of the SelectionSorter clas

ID: 3701011 • Letter: Q

Question

Questions 1 and 2 refer to the incomplete definition of the SelectionSorter class below. public class SelectionSorterí public SelectionSorterlint [I anArray) a anArray; public void sort)f for (int i«Q kalength. 1; i++){ int minPos minimumPosition(i): swap(minPos, i //Returns the position of the smallest element //in a[from]... a(a.length-1] private int minimumPosition(int from... //Exchanges ali] with ali swaplint i, int j)... private intl) a The following segment of code is executed. SelectionSorter sorter-new SelectionSorter(a); sorter.sort() Consider arrays initialized with the values below I 12,3, 4,5, 6 II. 6, 5, 4, 3,2 III. 12, 6,1,5,4 1. Which of the following statements regarding the number of comparison made to sort the array is true? Explain. a. I takes fewer comparisons than both II and Ill b. Il takes fewer comparisons than both I and II. c. Ill takes fewer comparisons than both I and II. d. I and II require the same number of comparisons, which is different from the number of comparisons required to sort III. I, II, and IlI require the same number of comparisons. e.

Explanation / Answer

1. Answer is e.

The three arrays require same number of comparisions. Finding minimum procedure doesn't know which is the smallest element in the array, unless it iterates through all the array.

So for finding first smallest element n-1 comparisons

second minimum n-2

2. Answer b.

i loop variable selects one element at a from 0, 1, 2, ... , array.length.

for each i, loop varibale j goes from i+1 to array length and finds index j which has the minimum array element. and swaps with ithelement. which means that jth element is in its final position.

third minimum n-3 and it goes on n-4, n-5,. . . ,3, 2, 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote