Common sorting algorithm - Java implementation [to be improved]

Posted by Trader77 on Wed, 29 Dec 2021 13:01:50 +0100

1. Bubble Sort

Compare adjacent elements and exchange if a (n) > A (n + 1).

After a round of exchange, you can ensure that the last bit a(length - 1) is the maximum.

After the ith comparison is completed, you can ensure that the length - 1 - i ~ length - 1 bits are the incremental sort of the maximum number in the array.

Since sorting may have been successful before the end of sorting, you can use a variable to mark whether there is exchange in the previous round of sorting. If there is no exchange, the sorting has been completed.

Fastest: the array to be sorted has met the sorting requirements.

Slowest: the array to be sorted is just opposite to the sorting requirements.

/**
 * @Author HE LONG CAN
 * @Description Bubble sorting
 * @Date 2021-08-07 20:51:41
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] array = new int[]{1,5,3,2,4};
        // 1 3 2 4 5
        // 1 2 3 4 5
        // It is sorted twice, actually three times, and the last inspection.
        
        int[] sortedArray = sort(array);
        System.out.println(Arrays.toString(sortedArray));
    }

    public static int[] sort(int[] array) {
        //Copy a newArray
        int[] newArray = Arrays.copyOf(array, array.length);

        //The outermost loop is used to discard the largest sequence whose tail has been sorted
        for (int i = 1; i < newArray.length; i++) {

            //Are tags exchanged
            boolean isSwitched = false;

            for (int j = 0; j < newArray.length - i; j++) {
                if (newArray[j] > newArray[j + 1]) {
                    isSwitched = true;
                    int middle = newArray[j];
                    newArray[j] = newArray[j + 1];
                    newArray[j + 1] = middle;
                }
            }

            //If there is no exchange
            if (!isSwitched) {
                System.out.println("Total circulation " + i + " round");
                break;
            }
        }

        return newArray;
    }
}

2. Select Sort

First, find the smallest element in the array [0 ~ length - 1] position and exchange it with the element at position 0.

Then find the smallest element in the [1 ~ length - 1] position in the array and exchange it with the element in position 1.

And so on until the end of the loop.

The time complexity is always O(n^2).

/**
 * @Author HE LONG CAN
 * @Description Select sort
 * @Date 2021-08-07 21:48:18
 */
public class SelectSort {
    public static void main(String[] args) {
        int[] array = new int[]{1,5,3,2,4};
        // 1 5 3 2 4
        // 1 2 3 5 4
        // 1 2 3 5 4
        // 1 2 3 4 5

        int[] sortedArray = sort(array);
        System.out.println(Arrays.toString(sortedArray));
    }

    public static int[] sort(int[] array) {
        int[] newArray = Arrays.copyOf(array,array.length);
        for (int i = 0; i < newArray.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < newArray.length; j++) {
                if (newArray[minIndex] > newArray[j]) {
                    minIndex = j;
                }
            }
            
            //If the minimum value found is not i, it needs to be exchanged
            if (i != minIndex) {
                int middle = newArray[i];
                newArray[i] = newArray[minIndex];
                newArray[minIndex] = middle;
            }
        }
        return newArray;
    }
}

3. Insert sort

Start the forward comparison from the element at position 1 and insert the small number before the large number. If a larger number is not found, put it in place.

A sort of hand similar to a poker game.

Insertion sorting is more efficient when operating on almost ordered data.

But insertion sorting is generally inefficient.

/**
 * @Author HE LONG CAN
 * @Description Insert sort
 * @Date 2021-08-07 22:29:17
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] array = new int[]{10,2,45,8,36,4,2,47,52};

        int[] sortedArray = sort(array);
        System.out.println(Arrays.toString(sortedArray));
    }

    public static int[] sort(int[] array) {
        int[] newArray = Arrays.copyOf(array, array.length);

        for (int i = 1; i < newArray.length; i++) {
            int current = newArray[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                //Less than, move J bit to j+1
                if (current < newArray[j]) {
                    newArray[j + 1] = newArray[j];
                }else {
                    break;
                }
            }

            //If the above loop ends normally and j = 1 indicates that it is smaller than current, then we insert it into bit 0, because the original element of bit 0 has been moved to bit 1
            //If the break ends, J > - 1, or 0, that is, the first number is greater than current, then we insert it after bit j + 1.
            newArray[j + 1] = current;
        }

        return newArray;
    }
}

4. Hill sort

Because insertion sorting is more efficient when operating on almost ordered data.

So the idea of Hill sort is to change the array into an almost ordered sequence by some way, and then use insert sort.

Sorting method:

  • Set a growth amount m and group the array according to the growth amount M.
  • Insert and sort each group of data divided into groups.
  • Reduce the growth, generally m = m / 2, repeat the operation. Until m = 1.

Growth selection basis:

int m = 1; //growth
while(m < array.length / 2) {
    m = m * 2 + 1;
}
/**
 * @Author HE LONG CAN
 * @Description Shell Sort 
 * @Date 2021-08-08 21:30:13
 */
public class ShellSort {
    public static void main(String[] args) {
        int[] array = new int[]{9,1,2,5,7,4,8,6,3,5};

        int[] sortedArray = sort(array);
        System.out.println(Arrays.toString(sortedArray));
    }

    public static int[] sort(int[] array) {
        int[] newArray = Arrays.copyOf(array,array.length);

        //Calculate growth
        int m = 1;
        while (m < array.length / 2) {
            m = m * 2 + 1;
        }

        while (m >= 1) {
            for (int i = m; i < newArray.length; i++) {
                int current = newArray[i];
                int j = i - m;
                for (; j >= 0; j -= m) {
                    if (current < newArray[j]) {
                        newArray[j + m] = newArray[j];
                    }else {
                        break;
                    }
                }
                newArray[j + m] = current;
            }
            m /= 2;
        }

        return newArray;
    }
}

Topics: Java JavaSE