Java: Sortuj tablicę liczb całkowitych bez użycia Arrays.sort ()

To jest instrukcja w jednym z ćwiczeń w naszej klasie Java. Przede wszystkim chciałbym powiedzieć, że „odrabiam pracę domową” i nie jestem po prostu leniwy, prosząc kogoś na Stack Overflow, aby mi na to odpowiedział. Ten konkretny przedmiot był moim problemem ze wszystkich innych ćwiczeń, ponieważ starałem się znaleźć „idealny algorytm” do tego celu.

Napisz program JAVA, który wprowadzi 10 wartości całkowitych i wyświetli w kolejności rosnącej lub malejącej. Uwaga: Arrays.sort () nie jest dozwolone.

To jest kod, który wymyśliłem, działa, ale ma jedną oczywistą wadę. Jeśli wprowadzę tę samą wartość dwa razy lub więcej, na przykład:

5, 5, 5, 4, 6, 7, 3, 2, 8, 10

Tylko jedno z trzech wprowadzonych 5s zostanie zliczone i uwzględnione w wyniku. Otrzymane dane wyjściowe (dla porządku rosnącego) to:

2 3 4 5 0 0 6 7 8 10.

import java.util.Scanner;

public class Exer3AscDesc
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int tenNums[]=new int[10], orderedNums[]=new int[10];
        int greater;
        String choice;

        //get input
        System.out.println("Enter 10 integers : ");
        for (int i=0;i<tenNums.length;i++)
        {
            System.out.print(i+1+"=> ");
            tenNums[i] = scan.nextInt();
        }
        System.out.println();

        //imperfect number ordering algorithm
        for(int indexL=0;indexL<tenNums.length;indexL++)
        {
            greater=0;
            for(int indexR=0;indexR<tenNums.length;indexR++)
            {
                if(tenNums[indexL]>tenNums[indexR])
                {
                    greater++;
                }
            }
            orderedNums[greater]=tenNums[indexL];
        }

        //ask if ascending or descending
        System.out.print("Display order :\nA - Ascending\nD - Descending\nEnter your choice : ");
        choice = scan.next();

        //output the numbers based on choice
        if(choice.equalsIgnoreCase("a"))
        {
            for(greater=0;greater<orderedNums.length;greater++)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
        else if(choice.equalsIgnoreCase("d"))
        {
            for(greater=9;greater>-1;greater--)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
    }
}

questionAnswers(12)

yourAnswerToTheQuestion