Sorting algorithm (a) selecting bubble insertion sort

A bubble sort

:( principle bubble sort in ascending analysis of two numbers) of consecutive cycles, more size, the larger number is the greatest number after number to the right of the first cycle into the far right, and so after it like bubbles in the water to take up the same place every time the maximum number of end.

public static void main(String[] args) {
        int[] arr = new int[]{89, 32, 55, 543, 7, 8};
        sort(arr);
    }
    private static void sort(int[] arr) {
        for (int j = 0; j < arr.length; j++) {
            for (int i = 0; i < arr.length - j -1; i++) {
                int before = arr[i];
                int after = arr[i + 1];
                if (before > after) {
                    arr[i] = after;
                    arr[i + 1] = before;
                }
            }
            System.out.println(Arrays.toString(arr));
        }
    }

Output:

[32, 55, 89, 7, 8, 543]
[32, 55, 7, 8, 89, 543]
[32, 7, 8, 55, 89, 543]
[7, 8, 32, 55, 89, 543]
[7, 8, 32, 55, 89, 543]
[7, 8, 32, 55, 89, 543]

Second, the selection sort

Selection sort in ascending principle :( analysis) circulating inside a find the smallest number, put it to the far left (only the leftmost and minimum values ​​other swap not move), so

    public static void main(String[] args) {
        int[] arr = new int[]{89, 32, 55, 543, 7, 8};
        sort(arr);
    }

    private static void sort(int[] arr) {
        for (int j = 0; j < arr.length; j++) {
            int min = j;
            for (int i = j + 1; i < arr.length; i++) {
                int num = arr[i];
                if (num < arr[min]) {
                    min = i;
                }
            }
            swap(arr, min, j);
            System.out.println(Arrays.toString(arr));
        }
    }

    private static void swap(int[] arr, int min, int j) {
        if (min != j) {
            int temp = arr[min];
            arr[min] = arr[j];
            arr[j] = temp;
        }
    }

Third, insertion sort

Insertion sort Principle: Each step will be a sort of records, has been inserted into the front row ordered sequence in order to better until complete insertion of all the elements so far.
Here Insert Picture Description

    public static void main(String[] args) {
        int[] arr = new int[]{89, 32, 55, 543, 7, 8};
        sort(arr);
    }
    private static void sort(int[] arr) {
        for (int j = 1; j < arr.length; j++) {
            int index = j;
            for (int i = j - 1; i >= 0; i--) {
                int after = arr[index];
                int before = arr[i];
                if (after < before) {
                    arr[i] = after;
                    arr[index] = before;
                    index = i;
                }
            }
            System.out.println(Arrays.toString(arr));
        }
    }
Published 64 original articles · won praise 9 · views 8803

Guess you like

Origin blog.csdn.net/maomaoqiukqq/article/details/103950666