[JAVA] How to replace characters that you do not understand [Principle]

The Array class sort is really good, isn't it? It sorts in ascending order without permission, and even in descending order.
However, I was afraid that I would get stuck if I didn't know the essence of sorting. How to not use the sort method.

Target code

public class Change {

    public static void main(String[] args) {
        ListManager data = new ListManager();

        for (int i = 1; i < 100; i++) {
            /*
The following values are returned depending on the value passed to signum.
If the argument is greater than zero ⇒ 1
When the argument is zero ⇒ 0
If the argument is less than zero ⇒-1
             */
            int num1 = data.compare(i - 1, i);
            switch (num1) {
                case 1:
                    //Swap
                    data.exchange(i - 1, i);
                    //Replace from behind
                    for (int n = i; n > 0; n--) {
                        int num2 = data.compare(n - 1, n);
                        switch (num2) {
                            case 1:
                                data.exchange(n - 1, n);
                            default:
                                break;
                        }
                    }
                default:
                    break;
            }
        }
        data.checkResult();
    }
}

public class ListManager {
    private int[] dataList;
    private int compareCount;
    private int exchangeCount;

    public ListManager() {
        //Randomly create data
        Random random = new Random();
        dataList = new int[100];
        for (int i = 0; i < dataList.length; i++) {
            dataList[i] = random.nextInt(10000);
        }
    }

    /**
     *Compare two data
     *
     * @param index1
     * @param index2
     * @return -1:The data of index1 is small, 1:index2 data is small, 0:Same data
     */
    public int compare(int index1, int index2) {
        compareCount++;
        return (int) Math.signum(dataList[index1] - dataList[index2]);
    }

    /**
     *Swap two data
     *
     * @param index1
     * @param index2
     */
    public void exchange(int index1, int index2) {
        exchangeCount++;
        int tmp = dataList[index1];
        dataList[index1] = dataList[index2];
        dataList[index2] = tmp;
    }

    /**
     *Sort check
     */
    public void checkResult() {
        int data = dataList[0];
        for (int i = 1; i < dataList.length; i++) {
            if (data > dataList[i]) {
                throw new RuntimeException("Not sorted: [" + (i - 1) + "]=" + data + ", [" + i + "]=" + dataList[i]);
            }
            data = dataList[i];
        }
        System.out.println("Sort OK:Comparison=" + compareCount + ",Swap=" + exchangeCount);
    }

I feel like I'm writing some useless code, so I'll fix it as soon as I understand it.

If the replacement does not occur on the first switch, it will be confirmed that the index is sorted correctly in ascending order before the index you are looking at at that time, but in the case of my code, the replacement will occur on the first switch. I don't think it will happen, but it's useless because I'm checking the sort before the index.

Is it enough to check the previous order only when the replacement occurs?

I'll try to make it shorter.

Recommended Posts

How to replace characters that you do not understand [Principle]
[Ruby] When you want to replace multiple characters
How to make @Transactional work that does not work if you use it incorrectly
How to think when you suddenly understand about generics
Do you really understand? How to check the library and license used by the app
Java reserved word summary that you do not know unexpectedly
Convenient use of Map that you do not know unexpectedly
How to solve the problem that you can not pull image from docker hub with Minikube
How to handle items in GSON that do not know whether they are single or array
How to fix the problem that Aptana Studio does not start
How to resolve VS Code "Build failed, do you want to continue?"
[Swift] How to replace multiple strings
How do you write in Scala that was written in Java? (List → Map)
How do you write the ternary operator (? :)
How to do API-based control with cancancan
What to do if you install Ubuntu
Understand in 5 minutes !! How to use Docker
What to do when you want to delete a migration file that is "NO FILE"
What to do if you get Could not locate Gemfile or .bundle / directory
[Error] How to resolve the event that the screen does not transition after editing
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
Android Studio memo that you want to display Toast characters in large size
How to get only articles and transactions that you commented or messaged [includes]