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
Which boolean operator (and, not, or or) gives you the least results? *?
Readme [11.4K]
And, as it only includes results with just both terms. Or will give either and not will give everything but.
6 0
3 years ago
Assume your using a three button mouse to access shortcut menus you would
sergeinik [125]
Normally you would click the right hand/secondary mouse button, but you may configure any of the buttons to work within the Keyboard and Mouse section of the System Preferences, that can lead u to right clicking and it brings up a menu of the things that your possibly able to do .


 hOpe thiss theory hass helped you anyy :)
7 0
4 years ago
Why is it difficult to enforce laws against intellectual theft?
Mnenie [13.5K]

Answer:

The best answer is: all intellectual theft occurs online and through digital networks.

Explanation:

<em>Because the implementation is online and through the network, it is difficult to monitor and justify the safety of the victim and the theft. Internet is widely available to all individuals who has access to it. No matter what is the age, gender and nationality of the victim. It is difficult to maintain a law with its very wide scope and the audiences involved. </em>

3 0
3 years ago
open your browser and enter the address of this ftp site in the address box: ftp.cengage. if your browser supports ftp, a logon
Licemer1 [7]

FTP access is available from other companies, but access is constrained unless the user first authenticates by supplying a user name and potentially a password.

The files are frequently intended for specific uses rather than for public release. For instance, you must send a letter house your consumer address list. Both businesses would not want the public to have access to these materials in this situation. Software is a collection of instructions, data, or computer programs that are used to run machines and carry out particular activities. It is the antithesis of hardware, which refers to a computer's external components. A device's running programs, scripts, and applications are collectively referred to as "software" in this context. Software refers to the processes and programs that enable a computer or other electrical device to function.

Learn more about software here-

brainly.com/question/985406

#SPJ4

7 0
1 year ago
Given a line of text as input, output the number of characters excluding spaces, periods, or commas. If the input is:
enot [183]

In python 3.8:

print(len([x for x in input("Enter your text: ") if x not in "., "]))

I hope this helps!

3 0
3 years ago
Other questions:
  • In what way, if any, are colleges and universities related?
    14·1 answer
  • Which of the following is an Example of a technical (“hard”) skill
    6·2 answers
  • Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally y
    11·1 answer
  • __________ is a contemporary term for data and software tools for organizing, analyzing, and providing access to data to help ma
    6·1 answer
  • do you think that some people have difficulty talking to others face to face because of how prevalent texting is today
    15·2 answers
  • An online travel agency such as Travelocity is an example of a(n) _____.
    14·1 answer
  • With that ------ outlook he doesn't trust anyone.
    11·1 answer
  • How to get free PS5?
    8·2 answers
  • Javier downloads an illustration from an online Image library and modifies it for his purposes. The illustration he downloads is
    12·1 answer
  • refer to the exhibit. a user pc has successfully transmitted packets to www.cisco. which ip address does the user pc target in o
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!