ne variables named numPasses, numCompares, and numSwaps accurately keep track se
ID: 3741494 • Letter: N
Question
ne variables named numPasses, numCompares, and numSwaps accurately keep track seber of passes, compares, and swaps made in this bubble sort? Explain your answer. Lab 8-2: Using a Bubble Sort In this lab, you will complete a Java program that uses an array to store data for of Marengo. The village of Marengo conducted a census and collected household data, including the number of occupants in each household. The exact nde of household records has not yet been determined, but you know that Marengo has t than 300 households. The program is described in Chapter 8, Exercise 5, in Programming Logic and Design. The program should allow the user to enter each household size and determine the mean and median household size in Marengo. The program shou the mean and median household size in Marengo. The file provided for this lab contains the village records that contain the necessary variable declarations and input statements. You need to write the code that rts the household sizes in ascending order using a bubble sort, and then prints the mean so nd median household size in Marengo. Comments in the code tell you where to write your statements 1. Open the source code file named Householdsize. java using Notepad or the text editor of your choice. Write the bubble sort. 2. 3. 4. Output the mean and median household size in Marengo. Save this source code file in a directory of your choice, and then make that directory your working directory Compile the source code file, Householdsize.java. Execute the program with the following input, and record the output. Household sizes: 4, 1, 2, 4, 3, 3, 2, 2, 2, 4, 5, 6 5. 6.
Explanation / Answer
Program to find mean,median of household in acsending order using bubble sort:
Input:: 4,1,2,4,3,3,2,2,2,4,5,6
output:: mean=3.1666667,median=3.0
import java.io.*;
import java.util.*;
class HouseholdSize
{
public static void main(String args[]) throws IOException
{
int i,j,n;
System.out.println("Enter No.of household data: "); //Declaring array size
Scanner input=new Scanner(System.in);
n=input.nextInt();
System.out.print("Enter the household sizes: ");
int arr[]=new int[n]; //Initializing array with 'n' elements
for (i=0;i<n;i++)
{
arr[i]=input.nextInt(); // Taking array input elements
}
for (i=0;i<n;i++) // Sorting array using bubble sort
{
for (j=0;j<n;j++)
{
if (arr[i]<arr[j])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (i=0;i<n;i++) //Printing Sorted array elements
{
System.out.print(" "+arr[i]);
}
int sum=0;
double mean,median;
for (i=0;i<n;i++) // Finding mean of the elements
{
sum=sum+arr[i];
}
mean=(float)sum/n;
System.out.println(" Mean: "+mean);
if (n%2!=0) // Calculating median of elements
{
median=arr[n/2];
}
else
{
median=(double)(arr[(n - 1) / 2] + arr[n / 2]) / 2;
}
System.out.println(" Median: "+median);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.