1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Anton [14]
3 years ago
13

Write a function in Java that takes in the int[] array, sorts it using bubble sort, then returns back the sorted array.

Computers and Technology
1 answer:
diamong [38]3 years ago
6 0
<h2>Answer </h2><h2></h2>

//Class to test the bubble sort

public class BubbleSortTest{

   

//Method to sort the array using bubbleSort

//Takes the array to be sorted as argument

public static int[] bubbleSort(int[] arr) {      

  //Create an integer to hold the length of the array

  int n = arr.length;  

   

  //Create a temporary variable to hold the values of the

                       //array

  //for swapping purposes. Initialize it to zero

  int temp = 0;  

   

  //Cycle through the array element by element

  for(int i = 0; i < n; i++){  

   

   //For each element

   for(int j = 1; j < (n-i); j++){  

     

    //if the element is greater than the element after it

    if(arr[j-1] > arr[j]){  

     //swap the elements  

     temp = arr[j-1];  

     arr[j-1] = arr[j];  

     arr[j] = temp;  

        }    //End of if        

   }   //End of inner for

    }   //End of outer for

 

 //return the sorted array

 return arr;

}

 

   //main method

    public static void main(String [] args){

  //create a sample array

        int [] myArray = {56, 23, 7, 4, 6};

   

  //Call the bubbleSort() method to sort the array

        int [] sortedArray = bubbleSort(myArray);

   

  //Print out the sorted array

        for(int i = 0; i < sortedArray.length; i++){

            System.out.print(sortedArray[i] + " ");

        }

   

    } //End of main method

 

     

}  //End of class BubbleSortTest

<h2>Sample Output</h2>

4 6 7 23 56

<h2>Explanation:</h2>

The code above has been written in Java and it contains comments explaining necessary parts of the code. Please go through the comments. A sample output has also been provided.

For clarity, the following shows the actual function alone without using or including it in a class.

=========================================================

//Method to sort the array using bubbleSort

//Takes the array to be sorted as argument

public static int[] bubbleSort(int[] arr) {  

       

       //Create an integer to hold the length of the array

       int n = arr.length;  

       

       //Create a temporary variable to hold the values of the array

       //for swapping purposes. Initialize it to zero

       int temp = 0;  

       

       //Cycle through the array element by element

       for(int i = 0; i < n; i++){  

           

           //For each element

           for(int j = 1; j < (n-i); j++){  

               

               //if the element is greater than the element after it

               if(arr[j-1] > arr[j]){  

                   //swap the elements  

                   temp = arr[j-1];  

                   arr[j-1] = arr[j];  

                   arr[j] = temp;  

            }  

                     

           }  

        }  

   

   //return the sorted array

   return arr;

}

=========================================================

You might be interested in
Virtual classrooms are examples of communication or not​
3241004551 [841]

Answer:

I would say yes

Explanation:

8 0
3 years ago
Read 2 more answers
For the question below, refer to the following recursive factorial method.
Mashcka [7]

Answer:

(d) 6

Explanation:

When we call factorial(3) first it will check if it greater than 1 since it is greater then return 3*factorial(2) will be called on factorial(2) it will again check that 2>1 since it is greater then return 2*factorial(1) will be called.In factorial(1) we have is not greater than 1 so it will return 1.return 2*factorial(1) will return 2 and 2 that is returned by factorial(2) will be multiplied with 3.So it will return 6.

8 0
4 years ago
In the replace dialog box, clicking what button changes highlighted text
Andre45 [30]

ANSWER: Replace Button

EXPLANATION: After opening the replace dialog box and putting the desired values in the boxes, we need to click the 'Replace' button to change the highlighted text. If the word which needs to be changed appear multiple times in the document, then clicking on 'Replace All' is advised as it would change all the old words with the new ones.

7 0
3 years ago
Read 2 more answers
Does the security burden fall primarily on the user? On
liubo4ka [24]

Answer:

yes and no because of the security

Explanation:

yes and no because of the security

6 0
3 years ago
A device combined with a switch, router, and wireless access point is called what?
ra1l [238]

It is called a wireless router!

8 0
3 years ago
Read 2 more answers
Other questions:
  • Gina is upgrading your computer with a new processor. She installs the processor into your motherboard and adds the cooling syst
    5·2 answers
  • A weak fuel to air mixture along with normal airflow through a turbine engine may result in?
    8·1 answer
  • What are two parts of a cell reference?
    7·1 answer
  • Assume that a two-dimensional rectangular array of integers called matrix has been declared with six rows and eight columns. Wri
    13·1 answer
  • Sandra wants to have her new technology up and running as soon as possible. She is looking for a tool that she can
    8·1 answer
  • Which module is missing from most expert systems? a. Knowledge base subsystem b. Inference engine c. User interface subsystem d.
    5·1 answer
  • What file name would allow you to easily find and edit your document later in Word Online?
    14·2 answers
  • Catastrophic injuries and illnesses account for two-thirds of total health care costs in the country of Gnut. The Gnuti governme
    15·1 answer
  • ____ a British mathematician devolved the concept of a programmable digital computer and worked with Ada Lovelace to design the
    13·1 answer
  • David is taking a test. To answer a question, he first covers up the answer choices. Next, he tries to answer the question. Then
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!