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
How does a firewall provide network security
ehidna [41]
Firewalls are connected with a network device which blocks untrusted network forming a barrier in between trusted and un-trusted network.
8 0
3 years ago
Who plays Roblox and wants to be friends on it
qaws [65]
I have roblox and I would like to be friends on it
4 0
3 years ago
Read 2 more answers
Melissa needs to put a topic name on her email that she will send will to her teacher . choose the name of the field
Lera25 [3.4K]

It is A. I am pretty sure

8 0
3 years ago
Read 2 more answers
How do I use files and functions in programming?
maw [93]

answer:

1.The program comes to a line of code containing a "function call".

2.The program enters the function (starts at the first line in the function code).

3.All instructions inside of the function are executed from top to bottom.

4.The program leaves the function and goes back to where it started from.

5.Any data computed and RETURNED by the function is used in place of the function in the original line of code.

3 0
3 years ago
What is the name of the FOLDER in which the file named "script" is contained?
yulyashka [42]

Answer:

The default location for local logon scripts is

  • the Systemroot\System32\Repl\Imports\Scripts folder

5 0
3 years ago
Other questions:
  • Explain how can you protect your computer from malware
    15·2 answers
  • Which Command Prompt commands in Windows is used for listing a computer connections to shared resources
    10·1 answer
  • What does limited access to a document mean?
    14·2 answers
  • Which of the following correctly orders the investments from LOWER risk to HIGHER risk?
    7·2 answers
  • Conduct online research to determine specific conflict-resolution and management techniques and skills that would be beneficial
    5·1 answer
  • The _ are the devices that allow a computer system to communicate and interact with the outside world as well as store informati
    11·1 answer
  • The communication channel used in IMC must rev: 12_06_2018_QC_ CDR-223 Multiple Choice match the traditional channel used in tha
    14·1 answer
  • A function ________ contains the statements that make up the function.
    5·1 answer
  • I need help please !!!!
    12·1 answer
  • Communication between a computer and a keyboard involves ______________ transmission.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!