I am trying to return the permutations of an integer in descending order. Currently I have code which returns all the permutations of a number however, it is not in descending order. I am unsure to whether I should use an array instead and it may be easier. My current code is:
// Function to print all the permutations of str
static void printPermutn(String str, String ans)
{
// If string is empty
if (str.length() == 0) {
System.out.print(ans + " ");
return;
}
for (int i = 0; i < str.length(); i++) {
// 1st character of str
char ch = str.charAt(i);
// Rest of the string after excluding
// the 1st character
String ros = str.substring(0, i) +
str.substring(i + 1);
// Recurvise call
printPermutn(ros, ans + ch);
}
}