Question #58532

3.3 Write a recursive pseudo code algorithm that returns the maximum among the first n elements of an array.


3.4 Write a recursive algorithm that prints all the permutations of the first n characters of a string. For example the call print (“ABC”, 3) would print

ABC, ACB, BAC, BCA, CBA, CAB

3.5 Implement the algorithms written in part 3.3 and 3.4 in Java Language.

Expert's answer

Question #58532, Programming & Computer Science / Java | JSP | JSF

3.3 Write a recursive pseudo code algorithm that returns the maximum among the first nn elements of an array.

Answer:


function max(n[], m) {
if m != 0 && n[m] < max(n, m-1) then return max(n, m-1) else
return n[m]
}


3.4 Write a recursive algorithm that prints all the permutations of the first nn characters of a string. For example the call print ("ABC", 3) would print

Answer:


procedure permut(s, i) {
if (i == 0) {
print(s + " ");
} else {
k = s[i];
for (int j = 0; j <= i; j++) {
char[] c = s.toArray();
c[i] = c[j];
c[j] = k;
permut(c.toString, i-1);
}
}
}


3.5 Implement the algorithms written in part 3.3 and 3.4 in Java Language.


public static int max(int[] n, int m) {
    if (m != 0 && n[m] < max(n, m-1)) return max(n, m-1);
    else return n[m];
}
public static void permut(String s, int i) {
    if (i == 0) {
        System.out.print(s + " ");
    } else {
        char k = s.charAt(i);
        for (int j = 0; j <= i; j++) {
            char[] c = s.toCharArray();
            c[i] = c[j];
            c[j] = k;
            permut(new String(c), i-1);
        }
    }
}


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS