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]
2 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]2 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
Janice, who is 15, posts post a picture of herself drinking alcohol and making an obscene gesture on her social network page. wh
mars1129 [50]

answer

A and B

Explanation.

A and B are the answers because this a real life scenario.

it's also can ruin her dreams on going to college to get a degree on what she want's to be and job application on her degree or any kind of job.

8 0
2 years ago
Read 2 more answers
-1
N76 [4]

Answer:A flowchart is a diagram that depicts the steps involved in solving a problem. The following flowchart shows how to output the multiplication table ( n * 1 to m * 1) of a number, n and m:

3 0
3 years ago
This is your code.
lubasha [3.4K]

Answer:

35

Explanation:

We will be going inside B array, because he was in second place in array E,and will be the first element of array B, so it's 35

E can be understanded as:

E=[[21, 'dog', 'red'],[35, 'cat', 'blue'],[12, 'fish', 'green']], so, you can see array E as array of arrays or so-called two-dimensional array

6 0
2 years ago
Can someone tell me how this is a SyntaxError! (Python3)
Basile [38]

Answer:

The expression on line 9 required 2 brackets

Explanation:

Given

The attached code

Required

Why syntax error.

The error points to line 10, but the error is actually from line 9

To get an integer input, the syntax is:

variable-name = int(input("Prompt"))

From the attached code, the line 9 is:

amount = int(input("Enter cheese order weight: ")

By comparing the syntax to the actual code on line 9, only 1 of the brackets is closed.

<em>This, in Python 3 is a sytax error</em>

6 0
3 years ago
What type of object can offer the most effective way to display data and calculations in a presentation?
poizon [28]

This is definitely an opinion, but an excel you can do A and D and just about the same as B also

<u>So i'd say C </u>

8 0
2 years ago
Other questions:
  • Which is the last step in conducting a url research
    11·1 answer
  • Write a program that calculates the cost of a phone call. The user enters a positive integer that
    11·1 answer
  • how to write a function "void funct()" which will accept a string from the user as input and will then display the string backwa
    11·1 answer
  • The data I collect in Google Forms are all compiled in a spreadsheet for me.<br> False <br> True
    12·2 answers
  • What is processor, memory RAM, cDRAM,DVD RAM,video card​
    9·1 answer
  • Please help!!
    8·1 answer
  • 50 POINTS &amp; A FOLLOW!
    11·2 answers
  • The output for the following code will be: Pasta (1 mark)
    6·1 answer
  • Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integer
    9·1 answer
  • Outline the dangers arising as a result of using computers​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!