Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubbl

ID: 3851017 • Letter: #

Question

(Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubble sort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped, otherwise, the values remains unchanged. The technique is called a bubble sort or sinking sort because the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually "bubble" their way to the top and the larger values "sink" to the bottom. Write a test program that reads in ten double numbers, invokes the method, and displays the sorted numbers. (Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee's seven day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write program that displays employees and their total hours in decreasing order of the total hours. (Locate the largest element) Write the following method that returns the location of the largest element in a two-dimensional array. Public static int[] localeLargest(double[]) The return value is a one dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two- dimensional array. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Here is a sample run

Explanation / Answer

Hi,

1. Bubble sort works exactly the way it is explained in the question. we use two loops to repeatedly swap the adjacent elements if they are in wrong order. below is the implementation in java.

public static void main(String[] args) {

List<Double> a= new ArrayList<>();

Scanner s = new Scanner(System.in);

System.out.println("enter 10 double numbers");

for (int i = 0; i < 10; ++i)

{

a.add(s.nextDouble());

}

s.close();

System.out.println("before sort"+a);

bubbleSort(a);

System.out.println("after sort"+a);

}

static void bubbleSort(List<Double> a)

{

int n = a.size();

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (a.get(j) > a.get(j+1))

{

// swap temp and arr[i]

Double temp = a.get(j);

a.set(j, a.get(j+1));

a.set(j+1, temp);

}

}

2. For this question, we can use a tree map to keep the values in sorted order like below

void getWorkingHours(int [][] employees)

{

Map<Integer,Integer> m= new HashMap<Integer,Integer>();

for(int i=0;i<8;i++)// since 8 employees

{

int sum=0;

for(int j=0;j<7;j++) // since 7 columns i.e 7 days

{

sum+=employees[i][j]; // add the number of hours for each day

}

m.put(sum, i);// adding the number of hours and employee number to hashmap

}

Map<Integer,Integer> descendingorderMAP = new TreeMap<Integer,Integer>(new Comparator<Integer>() // using a //tree map to keep the sorted order of employees according to the numer of hours

{

@Override

public int compare(Integer o1, Integer o2) {

return o2.compareTo(o1);// this is a comparator over the key values, basically says, which key should come first

}

});

descendingorderMAP.putAll(m);

// Iterate over them

for (Map.Entry<Integer, Integer> entry : descendingorderMAP.entrySet()) { // iterating over the tree map

System.out.println("Employee "+entry.getValue()+" worked for "+entry.getKey()+" hours");

}

}

3. This one is pretty simple, you just have to iterate through all the elements and check if the current element is greater than prev max, if it is then save its location, we can do it the same way we did the first question, for taking input you can use the same main already written in the first part of this answer, function is as below,

public static int[] locateLargest(double[][] a)

{

int row=0,col=0;

int[] largest = new int[2];

double max=0; // initializing the starting max and location

for (int i = 0; i < a.length; i++)// iterating rows

{

for (int j = 0; j < a[i].length; j++) // iterating columns

{

if (a[i][j] > max) // checking if current element greater than existing max

{

row = i;

col = j;

max = a[i][j];

}

}

}

largest[0]=row;

largest[1]=col;

return largest;// returning the location of max element found.

}

Thumbs up if this was helpful, otherwise let me know in comments. Good Day.