I need help understanding the below java code. We are not allow to use in our cl
ID: 655392 • Letter: I
Question
I need help understanding the below java code. We are not allow to use in our class the ArrayList sort method, so our professor provided us with an alternative. Suppose that I have an ArrayList<Integer> n, that contains the following values: 10, 30, 40, 50, 80, 90. Based of the code below, are the following initial values correct:
n.get(i) = 10,
n.get(j) = 40,
n.get(jMin) = 30,
Please kindly comment the code below. If possible, please draw the interpretation of this code on a piece of paper and attach it to the answer. Thank you in advace for your assistance!
public static void sort(ArrayList<Integer> n) {
for (int i = 0; i < n.size() - 1; i = i + 1) {
int jMin = i + 1;
for (int j = i + 2; j < n.size(); j = j + 1)
// Is n.get(j) = 40 and n.get(jMin) = 30 in the first loop?
if (n.get(j) < n.get(jMin))
jMin = j;
// // Is n.get(i) = 10 and n.get(jMin) = 30 in the first loop?
if (n.get(i) > n.get(jMin)) {
int t = n.get(i);
n.set(i, n.get(jMin));
n.set(jMin, t);
}
}
}
Explanation / Answer
The following staemnet from the code is tobe corrected.
// // Is n.get(i) = 10 and n.get(jMin) = 30 in the first loop?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.