Consider the following correct implementation of the selection sort algorithm: public static...

  • 53 + Users Viewed

  • 0 + Downloaded Solutions

  • Illinois, US Mostly Asked From

Consider the following correct implementation of the selection sort algorithm:

public static ArrayList selectionSort(ArrayList arr)

{

int currentMinIndex; //Line 3

int counter = 0;

for (int i = 0; i < arr.size() - 1; i++)

{

currentMinIndex = i;

for (int j = i + 1; j < arr.size(); j++)

{

if(arr.get(j) < arr.get(currentMinIndex))

{

currentMinIndex = j;

}

}

if (i != currentMinIndex) //Line 16

{

int temp = arr.get(currentMinIndex);

arr.set(currentMinIndex, arr.get(i)); //Line 19

arr.set(i, temp);

}

}

return arr;

}

What would be the most effective way to alter this selection sort algorithm so that an ArrayList would be sorted from greatest to smallest value?

Change Line 3 to int currentMaxIndex;

Change Line 5 to for(int i= arr.size() -1; i > 0; i --)

Change Line 10 to if(arr.get(j) > arr.get(currentMinIndex))

Change Line 16 to if (i > currentMinIndex)

Change Line 19 to arr.set(arr.get(i), currentMinIndex);

Expert's Answer

Recent Questions in Programming Languages

Plagiarism Checker

Submit your documents and get free Plagiarism report

Free Plagiarism Checker
Ask A New Question
Copy and paste your question here...
Attach Files
    Solve your Math Problem with one click
    MathSeolver Search Calculator