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
victus00 [196]
3 years ago
14

Create a generic bubble sort method. Refer to the pseudo code below for a description of the sorting algorithm. Your method must

be able to sort an array of any type of objects that implements the Comparable interface. Create a main method that creates an array of 100 integers. The integers are to be randomly selected, ranging from 0 to 999, inclusive. Also, create an array of 100 doubles. The doubles are to be randomly generated and ranging from 0 to 100. Display the contents of both arrays before sorting and after sorting in ascending order.
for (int k=1;k for (int i=0; i if(list[i] >list[i+1])
swap list[i] with list[i+1];
}
}
Computers and Technology
1 answer:
tangare [24]3 years ago
7 0

Answer:

import java.util.Arrays;

import java.util.Random;

public class GenericSort{

/**

* Method for swapping the value

* @param list

* @param i

* @param j

*/

private static <T> void swap(T[] list, int i, int j) {

if (i != j) {

T temp = list[i];

list[i] = list[j];

list[j] = temp;

}

}

/**

* Generic method for sorting array

* who implement the comparable interface

* @param list

*/

public static <T extends Comparable<T>> void bubble_sort_(T[] list) {

//used the given algo

for (int k=1;k<list.length; k++){

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

//using the compareTo method from comparable interface

if(list[i].compareTo(list[k])>0) {

swap(list,k,i);

}

}

}

}

public static void main(String[] args) {

Random rand = new Random();

//Array for int and double

Integer intArray[ ] = new Integer[100];

Double doubleArray []= new Double[100];

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

intArray[i] = rand.nextInt(1000);

doubleArray[i] = 0 + (1000 - 0) * rand.nextDouble();

}

System.out.println("Integer Array Before Sorting ...");

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

bubble_sort_(intArray);

System.out.println("Integer Array After sorting.....");

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

System.out.println("\n\nDouble array Before Sorting ....");

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

bubble_sort_(doubleArray);

System.out.println("Double array after sorting ...");

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

}

}

Explanation:

You might be interested in
Hanging out with friends, watching your favorite TV show, and buying a pair of new shoes are all examples of _____ for doing wel
SpyIntel [72]
<span>Hanging out with friends, watching your favorite TV show, and buying a pair of new shoes are all examples of rewards for doing well in school. As the result of doing well in studies, a person can hang out with friends, watch their own favorite TV show and can buy a new pair of shoes. Hence all these things are the rewards that are being generated for a good performance in school.</span>
8 0
3 years ago
Read 2 more answers
How does abstraction help us write programs
Misha Larkins [42]

Answer:

Abstraction refines concepts to their core values, stripping away ideas to the fundamentals of the abstract idea. It leaves the common details of an idea. Abstractions make it easier to understand code because it concentrates on core features/actions and not on the small details.

This is only to be used for studying purposes.

Hope it helps!

4 0
3 years ago
The process of bringing data or a file from one program to another is called
Digiron [165]
The answer is importing
8 0
2 years ago
Read 2 more answers
True or False
anastassius [24]

I used this from a other.

False

Software is the collection of large program or instruction of codes, which is used to perform some task. It is of two types of system software and application software.

The system software is used as a container for the application software and it is used to handle all other software it is used to operate the system.

Application software is used to perform any operation. This type of software can be used by the user if it is installed on the local machine on any system software

The operating system is also the part of the system software because it is used to operate the system and it is also the soul of the computer system which is also the function of the system software but the above question states that the operating system is not the part of the system software which is not correct. Hence false is the correct answer to the above question.

5 0
3 years ago
Jacob would like to include an arrow in his newsletter to point to some important information. He should _____.
qwelly [4]

Some people either add a shape as I do. But for this particular question it would be, either add word art or create a list with bullets. I would rather do word art or insert shape because that means you do not have to get the single bullet.


I really do hope this helped you. I hope you have a great day. And if I am incorrect I apologize.

3 0
3 years ago
Read 2 more answers
Other questions:
  • Suppose that sum is an int variable. The statement sum += 7; is equivalent to the statement sum = sum + 7;
    7·1 answer
  • The major difference between a calculator and a computer, when performing calculations, is that a
    10·1 answer
  • Among the following, which is the best protection against ransomware?
    8·1 answer
  • What is the theory of trouble shooting
    10·2 answers
  • How should font appear in a slide presentation compared to the font in a document
    7·1 answer
  • Select each task that may be completed using a word processor.
    8·2 answers
  • Which value can be entered to cause the following code segment to display the message "That number is acceptable."? ____.
    15·1 answer
  • A popular database software program called ____ offers a wizard and QBE tool to help build and generate SQL queries
    9·1 answer
  • The appropriate semaphore in C to give one more turn to writer so it can clean up IPC objects is WRITE_SEM. Is it true or false
    5·1 answer
  • What is the function of ALU? <br>​
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!