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

2 minute read

The sort of Array class is really amazing. It sorts them in ascending order, and in descending order.
However, I didn’t know the essence of rearrangement, so I was afraid that I might be stuck at this point. The method that does 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 numerical value passed to signum.
If the argument is greater than zero ⇒ 1
If the argument is zero ⇒ 0
If the argument is less than zero ⇒ -1
             */
            int num1 = data.compare(i-1, i);
            switch (num1) {
                case 1:
                    // switch
                    data.exchange(i-1, i);
                    // switch from the back
                    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() {
        // create data randomly
        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:index1 data 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: compare=" + compareCount + ", swap =" + exchangeCount);
    }

I feel that I am writing useless code, so I will fix it as soon as I understand.

If the switch does not occur on the first switch, it is confirmed that the indexes before that point are correctly sorted in ascending order, but in the case of my code, the switch on the first switch will probably occur. Although it will not happen, it is useless because the sequence before the index is checked.

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

Let’s fix it so that it is shorter.

Tags:

Updated: