Revise the method selectionSort that appears in Listing 7.10 so that it

Revise the method selectionSort that appears in Listing 7.10 so that it calls the method described in the previous exercise.

Listing 7.10

/**
Class for sorting an array of base type int from smallest to largest.
*/
public class ArraySorter
{
/**
Precondition: Every element in anArray has a value.
Action: Sorts the array into ascending order.
*/
public static void selectionSort(int[] anArray)
{
for (int index = 0; index < anArray.length − 1; index++)
{ // Place the correct value in anArray[index]
int indexOfNextSmallest = getIndexOfSmallest(index,
anArray);
interchange(index, indexOfNextSmallest, anArray);
//Assertion:anArray[0] <= anArray[1] <=…<= anArray[index]
//and these are the smallest of the original array elements.
//The remaining positions contain the rest of the original
//array elements.
}
}
/**
Returns the index of the smallest value in the portion of the
array that begins at the element whose index is startIndex and
ends at the last element.
*/
private static int getIndexOfSmallest(int startIndex, int[] a)
{
int min = a[startIndex];
int indexOfMin = startIndex;
for (int index = startIndex + 1; index < a.length; index++)
{
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
//min is smallest of a[startIndex] through a[index]
}
}
return indexOfMin;
}
/**
Precondition: i and j are valid indices for the array a.
Postcondition: Values of a[i] and a[j] have been interchanged.
*/
private static void interchange(int i, int j, int[] a)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp; //original value of a[i]
}
}

 

Stressed over that homework?

Essay deadline breathing down your neck?

Let’s cut to the chase: Why struggle when you can ace it with zero hassle?

Whether it’s essays, research papers, or assignments — we’ve got you covered.

✅ Expert writers
✅ 100% original work
✅ No AI tools, just real pros

Stressed about your essay or homework? Get a top-quality custom essay NOW!!! Stop worrying. Start succeeding.

GradeEssays.com
We are GradeEssays.com, the best college essay writing service. We offer educational and research assistance to assist our customers in managing their academic work. At GradeEssays.com, we promise quality and 100% original essays written from scratch.
Contact Us

Enjoy 24/7 customer support for any queries or concerns you have.

Phone: +1 213 3772458

Email: support@gradeessays.com

© 2024 - GradeEssays.com. All rights reserved.

WE HAVE A GIFT FOR YOU!

15% OFF 🎁

Get 15% OFF on your order with us

Scroll to Top