Question: You are given the following code for sorting an array of integers: void bubbleSort(int[] arr) { int length = arr.length; for (int i = 0;

You are given the following code for sorting an array of integers:

void bubbleSort(int[] arr) {

int length = arr.length;

for (int i = 0; i < length; i++) {

for (int j = length - 1; j > i; j--) {

if(arr[j] < arr[j-1]) {

int t = arr[j];

arr[j] = arr[j-1];

arr[j-1] = t;

}

}

}

}

(i) Modify the given method so that it uses an appropriate class or interface from the Java Collections Framework for the integer sequence arr instead of an array. Also describe any advantages and disadvantages of your modification.

(ii) Then modify the bubbleSort code you have written above so that the method becomes thread safe (i.e., so that it can be safely executed by multiple concurrently running threads without race conditions). Explain your modification. (Hint: think about what might happen when multiple concurrent threads call this method for the same sequence of numbers.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!