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

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)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!