java study notes 10: java language commonly used to sort the array (bubble sort, selection sort, quick sort, direct insertion sort)

A bubble sort
1, the basic idea
provided sort table length n, from front to back or back to front pairwise comparison values of adjacent elements, if not the relative order of the two (A [i-1]> A [ I]), then the switch which, as a result of the exchange of the smallest element to be sorted to the first position of the sequence, we call it a trip bubbling. The next trip bubbling, the smallest element is determined before the trip is no longer involved comparison, a reduction to be sorted sequence elements, each sequence of the trip bubbling results in the smallest element into the "top" sequence.
2, illustrating the principles of the bubble sort
Here Insert Picture Description
code is as follows:

public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr={24, 69, 80, 57, 13};
        finalSort(arr);
        System.out.println(Arrays.toString(arr));
    }   
    private static void finalSort(int[] arr) {
        for (int j = 0; j <arr.length-1; j++) {
            for (int i = 0; i < arr.length - 1-j; i++) {
                if (arr[i] > arr[i + 1]) {
                    int t = arr[i + 1];
                    arr[i + 1] = arr[i];
                    arr[i] = t;
                }
            }
        }
    }
 }

Second, selection sort
1, the basic idea selection sort (assuming descending order):
initialize an array: int [] array = {n} data
1st Sort: take out the element index 0 of this element compared with after each element, not smaller than the movable element, both the large numerical ratio of the element is switched, sequentially to the last comparison, so to put the maximum index of 0.
The second sort: the index of the element 1 is taken out, for comparison, is not smaller than the movable element and with each element after the element, the element than the large value of the exchange between the two, to turn the final comparison, such an array, the second largest value is placed in the position index 1.
And so on, the second last element taken out, compared with the last element, not smaller than the movable element, the element than the position of both the large exchange.
2, illustrating the principles of selection sort
Here Insert Picture Description
3, the code to achieve the following

public class ArrayDemo2 {
    public static void main(String[] args) {
        int[] arr = {24, 69, 80, 57, 13};
        // tuiDaosort(arr);
        for (int index = 0; index < arr.length - 1; index++) {
            for (int i = 1 + index; i < arr.length; i++) {
                if (arr[index] > arr[i]) {
                    int t = arr[index];
                    arr[index] = arr[i];
                    arr[i] = t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

Third, the rapid sorting
1, Quick Sort thinking
digging fill number:
① form the baseline of the first pit dug
② by the forward looking after smaller than its number, after dig to find this number to fill a pit before
③ looking from front to back than or equal to its large number, this number is dug up to find the former to fill a pit
④ ②③ two steps repeated.
2, the following diagram Here Insert Picture Description
3, as follows:

public class Test {
    public static void main(String[] args) {
        int[] arr={2,3,5,10,10,22,0,1,-1,-4,-3,-2};
        QuickSortUtils.quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}
class QuickSortUtils {
    public static void quickSort(int[] arr, int start, int end) {
        if (start < end) {
            //找到分左右两半的中间索引
            int index = getIndex(arr, start, end);//挖坑填数
            //对左右两区进行递归调用
            quickSort(arr, start, index - 1); //对左半变部分进行递归
            quickSort(arr, index + 1, end);//对右半部分进行递归
        }
    }
    private static int getIndex(int[] arr, int start, int end) {
        int i = start;
        int j = end;
        //找一个基准数
        int x = arr[i];
        while (i < j) {
            // 2. 由后向前找比他小的数,找到后挖出此数填到前一个坑中。
            while (i < j && arr[j] >= x) { //如果 这个数比基准数大,让索引倒退
                j--;
            }
            if (i < j) {
                arr[i] = arr[j]; //把这个数填到上一个坑中
                i++; //顺便让 i 递增一下
            }
            //3由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
            while (i < j && arr[i] < x) { //如果这个数比基准数小,就让这个索引往前跑

                i++;
            }
            if (i < j) {
                arr[j] = arr[i]; //填到上一个坑
                j--; //顺便让 j 递减一下
            }
        }
        //等上述完毕之后,把基准数填到最后一个坑位里
        arr[i] = x;
        return i;
    }
}

Fourth, direct insertion sort
1, the algorithm ideas: direct insertion sort, is the simplest sorting method is to his basic operation of a record is inserted into a sorted list of length m, and allowed to remain orderly. to obtain a new ordered list of length m + 1 assuming that there is a set of elements {k1, k2 ..., kn} , sorting the outset that is an ordered sequence k1, k2 are inserted into the table so that a length of the order 1 sequence, making a table ordered sequence of length 2, and then inserted into the table so k3 ordered sequence of length 2, and making a table for the ordered sequence length of 3, and so on, so that finally inserted kn table length ordered sequence of n-1, to obtain a long table is an ordered sequence of n.
2, code is implemented as follows:

public class ArrayDemo3 {
    public static void main(String[] args) {
        //直接插入排序:每一次把后面一个元素插入到前一个有序列表中,插入进去后,使之仍保持有序
        int[] arr = {1, 0, -1, 2, 4, 6, 7, 10, 8, 5, -2};
        //外层循环定义轮次 [0,-1,1]
        for (int i = 1; i < arr.length; i++) {
            int j = i;
            while (j > 0 && arr[j] < arr[j - 1]) { //如果当前元素小于我前一个元素,交换位置
                int t = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = t;
                j--;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
Published 24 original articles · won praise 11 · views 2057

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/85346446