Add a method bubbleSort to the class ArraySorter, that performs a bubble sort of
ID: 3809502 • Letter: A
Question
Add a method bubbleSort to the class ArraySorter, that performs a bubble
sort of an array. The bubble sort algorithm examines all adjacent pairs
of elements in the array from the beginning to the end and interchanges
(swaps) any two elements that are out of order. Each interchange makes the
array more sorted than it was, until it is entirely sorted. The algorithm in
pseudocode follows:
Bubble sort algorithm to sort an array a
Repeat the following until the array a is sorted:
for (index = 0; index <a.length - 1; index++)
if (a[index] > a[index + 1])
Interchange the values of a[index] and a[index + 1]
Please add a method bubleSort to the class ArraySort and follow the algorium above carefully. I will give the code for ArraySort class below. Also please use IDE to finish this question with comments and make sure it is working. In the result, it should print out multiple arrays that show how the bubbleSort process from beginng to final result step by step . Also do not erase or build a whole new class, the question need to add an method inside the given class ArraySort.
here is the ArraySorter class
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]
}
}
Explanation / Answer
public class ArraySortBubbleTest {
public static void main ( String[] args )
{
int[] anArray = new int[] {10,1,89,45, 32, 67, 9, 100,25};
System.out.println("Before bubble sorting");
ArraySorter.print(anArray);
System.out.println("bubble sorting....steps");
ArraySorter.bubbleSort(anArray);
System.out.println("After bubble sorting");
ArraySorter.print(anArray);
}
}
class ArraySorter
{
public static void bubbleSort(int[] anArray) {
int temp;
for(int i=0; i < anArray.length; i++){
for(int j=1; j < (anArray.length-i); j++){
if(anArray[j-1] > anArray[j]){
interchange(j-1,j,anArray);
}
}
print(anArray);
}
}
public static void print(int[] anArray) {
for(int i=0; i < anArray.length; i++){
System.out.print(anArray[i]+" ");
}
System.out.println();
}
/**
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]
}
}
------output---------------------------------------
Before bubble sorting
10 1 89 45 32 67 9 100 25
bubble sorting....steps
1 10 45 32 67 9 89 25 100
1 10 32 45 9 67 25 89 100
1 10 32 9 45 25 67 89 100
1 10 9 32 25 45 67 89 100
1 9 10 25 32 45 67 89 100
1 9 10 25 32 45 67 89 100
1 9 10 25 32 45 67 89 100
1 9 10 25 32 45 67 89 100
1 9 10 25 32 45 67 89 100
After bubble sorting
1 9 10 25 32 45 67 89 100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.