Convert pseudo code to java code Problem 1- n <-- length(A) for i = 0 to n-1 exc
ID: 3753431 • Letter: C
Question
Convert pseudo code to java code
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
Convert pseudo code to java code
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
Convert pseudo code to java code
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 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 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
Kindly give a thumps up if you like tha answer
Problem1
Sorting
public class Pseudocode {
public static void main(String[] args)
{
int[] A = new int[]{ 12,2,36,400,5,6,7,18,9,103 };
int n=A.length;
int i,j,min=0,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
min=j;
}
if(min!=i)
{
temp = A[i];
A[i] = A[min];
A[min] = temp;
}
}
//Print Array A
for(i=0; i<n; i++){
System.out.print(A[i] + " ");
}
}
}
Problem 2
public class Sample1 {
public static void main(String[] args)
{
int[] A = new int[]{ 12,2,36,400,5,6,7,18,9,103 };
int n=A.length;
int i,nn,temp;
while(n>0)
{
nn=0;
for(i=1;i<=n-1;i++)
{
if(A[i-1]>A[i])
{
temp = A[i-1];
A[i-1] = A[i];
A[i] = temp;
nn=i;
}
}
n=nn;
}
//Print Array A
for(i=0; i<A.length; i++)
{
System.out.print(A[i] + " ");
}
}
}
Problem 3
public class Sample2 {
public static void main(String[] args)
{
int[] A = new int[]{ 12,2,36,400,5,6,7,18,9,103 };
int i=1,j,x;
while(i<A.length)
{
x=A[i];
j=i-1;
while(j>=0&&A[j]>x)
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=x;
i=i+1;
}
//Print Array A
for(i=0; i<A.length; i++)
{
System.out.print(A[i] + " ");
}
}
}
Output
2 5 6 7 9 12 18 36 103 400
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.