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
Write a program that will print out statistics for eight coin tosses. The user will input either an "h" for heads or a "t" for t
shusha [124]

Answer:

Written in Python

head = 0

tail = 0

for i in range(1,9):

     print("Toss "+str(i)+": ")

     toss = input()

     if(toss == 'h'):

           head = head + 1

     else:

           tail = tail + 1

print("Number of head: "+str(head))

print("Number of tail: "+str(tail))

print("Percent head: "+str(head * 100/8))

print("Percent tail: "+str(tail * 100/8))

Explanation:

The next two lines initialize head and tail to 0, respectively

head = 0

tail = 0

The following is an iteration for 1 to 8

<em>for i in range(1,9): </em>

<em>      print("Toss "+str(i)+": ") </em>

<em>      toss = input()  </em><em>This line gets user input</em>

<em>      if(toss == 'h'):  </em><em>This line checks if input is h</em>

<em>            head = head + 1 </em>

<em>      else:  </em><em>This line checks otherwise</em>

<em>            tail = tail + 1 </em>

The next two lines print the number of heads and tails respectively

print("Number of head: "+str(head))

print("Number of tail: "+str(tail))

The next two lines print the percentage of heads and tails respectively

print("Percent head: "+str(head * 100/8))

print("Percent tail: "+str(tail * 100/8))

3 0
3 years ago
Program documentation _____.
otez555 [7]

Answer:

Option A is the correct option for the following question.

Explanation:

Because Program documentation is the documentation in which contain the information about the software and also contain the logic of the modules of the program is user-friendly and convenient language about that application or the software and it is hard copy or any digital manual through which the programmer has to know about the application.

So, that's why the following option is correct.

7 0
3 years ago
Most presentations use text: A. To maximize area and style. B. At a minimum. C. To draw attention to content. D. Without restrai
klemol [59]

The answer is most likely A.


Hope this Helped!


;D

4 0
3 years ago
Read 2 more answers
A medium is a:
Nostrana [21]
C is a medium. And the answer
4 0
3 years ago
Read 2 more answers
You are working on a graphical app, which includes multiple different shapes. The given code declares a base Shape class with an
ycow [4]

Answer:

maybe

Explanation:

5 0
3 years ago
Other questions:
  • If you work up to your potential but fall short of your goals, you
    15·2 answers
  • why is it important to use a general search engine, like GOOGLE, to look up the name of the organization, institution, agency or
    7·1 answer
  • What is TRUE about the following array?
    15·1 answer
  • Convert 78 to binary
    6·2 answers
  • In the language of the World Wide Web, "this page cannot be displayed" means A. your computer's software is damaged. B. the ISP
    10·2 answers
  • Write an application named Perfect that displays every perfect number from 1 through 10,000. A number is perfect if it equals th
    5·1 answer
  • What are the peripherals of a computer ​
    12·1 answer
  • Desktop and personal computer are also known as​
    7·1 answer
  • Which of the following is not a bus type A. Address bus B. Data bus C. Memory bus D. Control bus ​
    7·2 answers
  • If I bought mine craft p.e. for 7.99 and hook my Micro soft account up, will i get java edition
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!