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
Use humorous and monster in a sentence.
Sphinxa [80]

Answer: Here are a few...

1) The monstrous elephant trampled through the jungle crushing everything in its path.

2) Because it was so monstrous, the maid often got lost in the massive home she cleaned.

3) Monstrous amounts of foods are needed to feed the hundreds of refugees who showed up at the soup kitchen.

Explanation:

I hope this helps :D

4 0
3 years ago
While excel is saving your file, it briefly displays a message on the status bar indicating __________.
bezimeni [28]
While Excel is saving your file, it briefly displays a message on the status bar indicating the amount of the file saved.
5 0
3 years ago
Read 2 more answers
Describe under what workloads c-scan would be preferred over c-look?
Gelneren [198K]

Many filesystems attempt to logically group inodes together in an attempt to keep data close together on the disk. Consider a file system configuration of 1KB size blocks, 16384 blocks/group, and 4096 inodes/group, with each inode sized 128 bytes.

3 0
3 years ago
Write a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle.
Phantasy [73]

\tt L=(float(input("Enter\:Length\:of\:rectangle=")))

\tt B=(float(input("Enter\:Breadth\:of\:the\:rectangle=")))

\tt P=2*(L+B)

\tt A=L*B

\tt print("Perimeter\:of\:the\:rectangle=",P)

\tt print("Area\:of\:the\:rectangle=",A)

7 0
3 years ago
List the difference between sdram and dram​
IRISSAK [1]

Answer:

i need this for a challenge

Explanation:

4 0
3 years ago
Other questions:
  • Print person1's kids, call the incNumKids() method, and print again, outputting text as below. End each line with a newline.
    9·1 answer
  • Who knows coding questions? Please help! Thanks.
    6·1 answer
  • Web pages are accessed through a software program called a _____.A) ​web crawlerB) ​web browserC) ​web serverD) ​web app drawer
    5·1 answer
  • If you want to code a method that can only be called from the class that the method is in, you code ____________________ as the
    15·2 answers
  • A ___ is a mobile device that typically ranges from seven to ten inches in size.
    14·1 answer
  • categorize each job role as an administrative job or management job auditor director legal secretary payroll clerk etc ​
    8·1 answer
  • Im on Edge 2021 and i did an assignment that says, "Processing." The assignment won't give me a grade and says it is overdue. wh
    10·1 answer
  • Describe the operation of IPv6 Neighbor Discovery. ​
    7·1 answer
  • Directions Interview your parent or any member of your family. Identity
    12·1 answer
  • What does a computer monitor look like when struck really hard?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!