Sorting numeric digits recursively in Java

I am very new to programming, I just learned about it at the university. I have a problem when I have to recursively solve this problem in java (without using arrays, if, else, while, etc.)

So, the task is to sort numbers from 13542 to 12345.

public static void main(String[] args) {
    System.out.println(sort(13542));

}

public static long sort(long n) {
    return n < 10
            ? n
            : sort(n, 0);
}

public static long sort(long n1, long n2) {
    return n1 > 10
            ? xxx
            : xxx;
}

The problem is that I have no idea what to do. I think my start is fine, but I have problems with the second method.

+4
source share
5 answers

First, recursion simply means that you call yourself something repeatedly. The fact that the assignment is related to recursion is a hint at how your lecturer wants you to resolve it using the recursive method.

, , , .

public int recursiveSort(int toSort){

}

, , .

public Boolean isSorted(int toCheck){
    //TODO: Check if input is sorted
}

public int singleSort(int toSort){
    //TODO: Sorting algorithm
}

public int recursiveSort(int toSort){
    toSort = singleSort(toSort);
    return isSorted(toSort) ? toSort : recursiveSort(toSort);
}

, .
, , , .

+2

2 "":

, , 1 , , (, , ).

, , .
, , .

, . .

public static void main(String[] args) {
    System.out.println(sort(13542));
}

public static long sort(long n) {
    // For testing purposes:
    // System.out.println("sort(" + n + ")");

    if (n < 10) return n;   // Termination condition

    int numOfDigits = (int)(Math.log10(n)+1);
    long largestDigit = n % 10;
    long restOfDigits = n / 10;

    for(int i=0; i<numOfDigits; i++) {
        long current = (long) (n / Math.pow(10, i)) % 10;
        if (current > largestDigit) {
            largestDigit = current;
            restOfDigits = (long) Math.pow(10, i) * (n / (long) Math.pow(10, i + 1))
                    + (n % (long) Math.pow(10, i));
        }
    }

    // Merge the largest number on the right
    return 10 * sort(restOfDigits) + largestDigit;
}

, . , .

+1

, . .

public void eatAllFoodFromTable(Table tbl, Person prsn) {

    if(tbl.hasFood()) {

        prsn.sustain(1);
        tbl.removeFood(1);
        eatAllFoodFromTable(tbl, prsn); /*As you can see here,
            the method calls itself. However, because the method has a condition
            that can prevent it from running indefinitely (or a way to terminate),
            it will repeat until the condition is met, then terminate. This is recursion!*/

    } else {
        //Do nothing.
    }

}

, , sort . , - ( - ), (sort()) , .

, , , .

+1

; , , . , . , . - . ( .)

JavaScript:

function main() {
  console.log(sort(13542));
}

function sort(n) {
  if (n < 10)
    return n;
  
  let r = n % 10;
  let l = (n - r) / 10 % 10;

  let sorted = sort(Math.floor(n / 10) - l + r);
  let last = sorted % 10;
    
  if (l < last)
    return 10 * sort(sorted - last + l) + last;
  else 
    return 10 * sorted + l;
}

main();
Hide result
+1

Many thanks for your help. I think I got it now:

public static long sort(long n) {
    return n < 10
            ? n
            : shuffle(sort(n / (long) Math.pow(10, count(n) / 2)),
                    sort(n % (long) (Math.pow(10, count(n) / 2))));
}

public static long count(long n) {
    return n < 10
            ? 1
            : 1 + count(n / 10);
}

public static long shuffle(long n1, long n2) {
    return (n1 > 0 || n2 > 0)
            ? (n1 % 10 > n2 % 10)
                    ? shuffle(n1 / 10, n2) * 10 + n1 % 10
                    : shuffle(n1, n2 / 10) * 10 + n2 % 10
            : 0;
}

Unfortunately, we were not allowed to use if, else or while. That would be a lot easier. But thanks to everyone :)

0
source

Source: https://habr.com/ru/post/1694194/


All Articles