Question

Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish.

Step 1. Create the pseudocode that you will use to write the program. Place the algorithm in a Word document.

Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1.  Generate 11 random integer numbers between 1 and 100, and place each random number in a different element of a single-dimension array starting with the first number generated. 2.  Display the array's contents in the order the numbers are initially inserted. This is called an unsorted list. An example is below.

3. Using the bubble sort, now sort the array from smallest integer to the largest. The bubble sort must be in its own method; it cannot be in the main method. Here is the code for the bubble sort method. Use this code, and not some code you find online. Bubble Sort Code: public static void bubbleSort(int[] list) { int temp; for (int i = list.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } }

4. Display the array's contents after the bubble sort is completed. An example is below. 5. Locate the median of the 11 numbers. The formula for finding the position of the median in a list of sorted numbers is: ([the number of data points] + 1) ÷ 2. In this case, 11 is the number of data points. You must use implement this formula to locate the cell in the array that contains the median and not just display the item at location 6 of the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class MyClass {
public static void main(String args[]) {
//Random function for generating random number
Random rand = new Random();
//int r = rand.nextInt(100) + 1;
int list[] = new int[11];
//Storing the random number in array
for(int i=0; i<list.length; i++){
list[i] = rand.nextInt(100) + 1;
}
//Printing out the contents of Array of random elements
System.out.println("Array Before Sorting: "+Arrays.toString(list));
//Calling the bubbleSort function to sort the array
bubbleSort(list);
//Printing out the contents of Array of random elements after sorting
System.out.println("Array After Sorting: "+Arrays.toString(list));
int med = list[(list.length )/2];
System.out.println("Median of the Array: "+med);
}
//Given bubble sort function
public static void bubbleSort(int[] list) {
int temp;
for (int i = list.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT