Add a method bubbleSort to the class ArraySorter, that performs a bubble sort of
ID: 3809098 • 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]
A good visualization of the algorithm in action can be found at: https: //en.wikipedia.org/wiki/Bubble_sort#/media/File:Bubble-sort-example-300px. gif
Please use the Implementation of the selection sort below to add the sorting algorithms as static methods. Recommand use IDE and with possilbe comments
Here is codes of Array Sorter
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 ArraySorter
{
/**
Precondition: Every element in anArray has a value.
Action: Sorts the array into ascending order.
*/
/*Bubble sort algorithm --> in Bubble sort algorithm in each iteration largest number moved to end. and each time end is getting reduced by one index*/
public static void bubbleSort(int[] anArray){
/*in first for loop each time end is reduced by one because we don't need to consider last index since it is already sorted*/
for(int i=anArray.length;i>0;i--){
/*in each iteration next index data will be compared if it greater then it will be swapped at the end of all iterations highest number moved to end*/
for(int j=0;j<i-1;j++){
if(anArray[j]>anArray[j+1]){
interchange(j,j+1, anArray);
}
}
}
}
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]
}
}
/*TESTER.JAVA*/
public class Tester {
public static void main(String[] args) {
int[] anArray = {5, 7, 3, 1, 6, 9, 100, 99 , 45, 2};
/*Before Sorting*/
System.out.println("Before Sorting");
for(int a: anArray){
System.out.print(a+" ");
}
ArraySorter.bubbleSort(anArray);
System.out.println("After Sorting");
/*After Sorting*/
for(int a: anArray){
System.out.print(a+" ");
}
}
}
Output:
1
2
3
5
6
7
9
45
99
100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.