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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started