Convert pseudo code to java code using List<Lin> ls Problem 1- n <-- length(A) f
ID: 3754139 • Letter: C
Question
Convert pseudo code to java code using List<Lin> ls
Problem 1-
n <-- length(A)
for i = 0 to n-1 exclusive do
min <-- i
for j = i+1 to n exclusive do
if A[j] < A[min]
min <-- j
end if
end for
if min != i
swap(A[i], A[min])
Problem 2
n <-- length(A)
while n > 0
nn <-- 0
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
nn <-- i
end if
end for
n <-- nn
Problem 3
i <-- 1
while i < length(A)
x <-- A[i]
j <-- i - 1
while j >= 0 and A[j] > x
A[j+1] <-- A[j]
j <-- j - 1
end while
A[j+1] <-- x
i <-- i + 1
end while
Explanation / Answer
Please find the equivalent code below.
CODE
==================
import java.util.List;
public class Main {
public static void problem1(List<Integer> A) {
int n = A.size();
int min = 0;
for (int i = 0; i< n-1; i ++) {
min = i;
for (int j = i+1; j < n; j ++) {
if (A.get(j) < A.get(min)) {
min = j;
}
}
if (min != i) {
int temp = A.get(i);
A.set(i, min);
A.set(min, temp);
}
}
}
public static void problem2(List<Integer> A) {
int n = A.size();
int nn;
while (n > 0) {
nn = 0;
for(int i=1; i< n; i++) {
if (A.get(i-1) > A.get(i)) {
int temp = A.get(i);
A.set(i, A.get(i-1));
A.set(i-1, temp);
nn = i;
}
}
n = nn;
}
}
public static void main(List<Integer> A) {
int i = 1;
while (i < A.size()) {
int x = A.get(i);
int j = i - 1;
while (j >=0 && A.get(j) > x) {
A.set(j+1, A.get(j));
j --;
}
A.set(j + 1, x);
i ++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.