This is Bubble Sort Java Code, Please add comment on each line of code to descri
ID: 3792280 • Letter: T
Question
This is Bubble Sort Java Code, Please add comment on each line of code to describe what they doing
public class BubbleSort {
public static void Swap(int[] arrayswap, int i, int j)
{
int position = arrayswap[i];
arrayswap[i] = arrayswap[j];
arrayswap[j] = position;
}
public static void BubbleSort( int[] A) {
for (int i = 1; i <= A.length; i++) {
for(int j = A.length; j >= i+1; j--)
{
if(A[j-1] < A[j-2])
{
Swap(A,j-1,j-2);
}
}
}
}
public static void main(String [] args){
int[] random = new int[]{2, 9, -11, 8, 5, 9001, 3};
BubbleSort(random);
System.out.println("Expect random to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(random));
int[] LowtoHight = new int[]{2, 9, -11, 8, 5, 9001, 3};
BubbleSort(LowtoHight);
System.out.println("Expect LowtoHight to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(LowtoHight));
int[] HighttoLow = new int[]{9001, 9, 8, 5, 3, 2, -11};
BubbleSort(HighttoLow);
System.out.println("Expect HighttoLow to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(HighttoLow));
}
}
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
import java.util.Arrays;
/**
*
* @author Namburi Ramesh
*/
public class BubbleSort {
//Below function interchanges two integers at positions 'i' and 'j' in 'arrayswap' array
public static void Swap(int[] arrayswap, int i, int j)
{
int position = arrayswap[i]; //stores integer at position 'i' in 'arrayswap' array in 'position' variable
arrayswap[i] = arrayswap[j]; //exchanges integer at position 'j' in 'arrayswap' array to position 'i' in 'arrayswap' array
arrayswap[j] = position; //exchanges integer at position 'i' in 'arrayswap' array which is stored in 'position' variable to position 'j' in 'arrayswap' array
}
/*
Below function implements the BubbleSort Algorithm. It works in the following way:-
In the first iteration of the outer for loop it finds the smallest element in the array from position '0' to position 'n-1' (n= length of array) and stores it at position '0' of array
In the second iteration of the outer for loop it finds the smallest element in the array from position '1' to position 'n-1' (n= length of array) and stores it at position '1' of array
In the third iteration of the outer for loop it finds the smallest element in the array from position '2' to position 'n-1' (n= length of array) and stores it at position '0' of array
Likewise it proceeds until the entire array is sorted
*/
public static void BubbleSort( int[] A) {
for (int i = 1; i <= A.length; i++) {
for(int j = A.length; j >= i+1; j--)
{
//compare the two elements at positions 'j-1' and 'j-2' and interchanges them if A[j-1] is less than A[j-2]
if(A[j-1] < A[j-2])
{
Swap(A,j-1,j-2);
}
}
}
}
public static void main(String [] args){
int[] random = new int[]{2, 9, -11, 8, 5, 9001, 3}; // initilises array with the given elements
BubbleSort(random); //calls the method 'BubbleSort' and passes the array to be sorted
System.out.println("Expect random to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(random)); // prints the sorted array
int[] LowtoHight = new int[]{2, 9, -11, 8, 5, 9001, 3};
BubbleSort(LowtoHight);
System.out.println("Expect LowtoHight to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(LowtoHight));
int[] HighttoLow = new int[]{9001, 9, 8, 5, 3, 2, -11};
BubbleSort(HighttoLow);
System.out.println("Expect HighttoLow to be: -11, 2, 3, 5, 8, 9, 9001: " + Arrays.toString(HighttoLow));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.