Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java, Write a program that implements a method that receives an array para

ID: 3595787 • Letter: U

Question

Using Java,

Write a program that implements a method that receives an array parameter and sorts that array using the bubble-sort algorithm show below. The bubble-sort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped: otherwise, the values remain unchanged. The technique is called a bubble sort because the smaller values gradually "bubble" their way to the top.

The algorithm may be described as follows:

boolean changed;

do{

changed = false;

for(int i = 0; i < list.length - 1; i++){

    if(list[i] > list[i + 1]){

      swap list[i] with list[i + 1];
      changed = true;
    }
}
}while(changed);

Explanation / Answer

Please find my implementation.

import java.util.Arrays;

public class BubbleSort {

   public static void bubbleSort(int[] list) {

       boolean changed;

       do{

           changed = false;

           for(int i = 0; i < list.length - 1; i++){

               if(list[i] > list[i + 1]){

                   int temp = list[i];

                   list[i] = list[i+1];

                   list[i+1] = temp;

                   changed = true;

               }

           }

       }while(changed);

   }

  

   public static void main(String[] args) {

      

       int[] list = {5,3,12,6,14,76};

      

       System.out.println(Arrays.toString(list));

      

       bubbleSort(list);

      

       System.out.println(Arrays.toString(list));

      

   }

}

/*

Sample run:

[5, 3, 12, 6, 14, 76]

[3, 5, 6, 12, 14, 76]

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote