Summarize several commonly used algorithms (java)

Insert picture description here

Bubble Sort

The main idea: first compare one by one, put the largest one at the back, and ensure that the last value is the largest during each traversal.

public static void bubbleSort(int[] arr){
        //冒泡排序
        int len=arr.length;
        for(int i=1;i<len;i++){
            for(int j=0;j<len-i;j++){
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
    }

Insertion sort

The main idea: insert the unsorted elements into the sorted one by one.
Each time the next element n is retrieved, the elements from 0 to n-1 in the front are sorted. What we need to do is to insert n to the sorted Inside the preface
Insert picture description in

public static void insertSort(int[] array){
        //插入排序
        int i,j,temp;
        for(i=1;i<array.length;i++) {
            temp=array[i];
            for(j=i-1;j>=0;j--) {
                if(temp>array[j]) {
                    break;
                }else {
                    array[j+1]=array[j];
                }
            }
            array[j+1]=temp;
        }

    }

Select sort

The main idea: each time i is the subscript of the minimum value, and then to loop the following elements (because the previous i-1 elements have been sorted), find the minimum subscript of the remaining elements, and get the current i and minimum Value subscripts are exchanged, which is unstable.

public static void selectSort(int[] arr){
        //选择排序
        for(int i = 0; i < arr.length-1; i++){
            int min = i;
            for(int j = i+1; j <arr.length;j++){
                if(arr[j]<arr[min]){
                    min = j;
                }
            }
            if(min!=i){
                swap(arr, i, min);
            }
        }
    }

Quick sort (divide and conquer)

The main idea, in simple terms, is to set a basic value base at will, and then put the one smaller than the base on the left, and the larger one on the right. The two parts are divided into two parts, using the same idea, take out a new base, and the smaller one on the left , Put the big one on the right, and put it down at the end, it will be sorted.
The key point is to quickly sort an array, using double cursors left and right, left++, right–, to traverse all elements, so that small ones can be placed on the left and large ones on the right. The specific implementation review, reference code , Write your own handwriting, and remember each review quickly.

public static void quikSort(int[] arr,int left,int right){
        //快速排序(分治思想)
        if(right<=left)return;
        int k=part(arr,left,right);
        quikSort(arr,left,k-1);
        quikSort(arr,k+1,right);
    }

private static int part(int[] arr, int left, int right) {

        int base=arr[left];
        while (left<right){
            //右边开始
            for(;arr[right]>base&&right>left;right--);
            if(right>left){
                arr[left]=arr[right];
                left++;
            }
            //左边
            for(;arr[left]<base&&right>left;left++);
            if(left<right){
                arr[right]=arr[left];
                right--;
            }

        }
        arr[left]=base;
        return left;
    }

Merge sort

The main idea is to keep dividing into two until it is inseparable, and start merging. Each merge uses a new array temp to store the merged and sorted items, and then assign temp to the element group. The merged paragraph, and then a paragraph by paragraph assignment, the sorting succeeds.
The main understanding of the code is that when merging, the array a is not disassembled, but it is divided into two, and then divided into two paragraphs using (low, mid) (mid+1, high) to understand. Then set the double cursors to i=low and j=mid+1; let them compare the elements in a, and then assign them to Temp, and merge a short section. In general, it succeeded (understand !!!)

Insert picture description here

public static int[] sort(int[] a,int low,int high){
        int mid = (low+high)/2;
        if(low<high){
            sort(a,low,mid);
            sort(a,mid+1,high);
            //左右归并
            merge(a,low,mid,high);
        }
        return a;
    }
     
    public static void merge(int[] a, int low, int mid, int high) {
        int[] temp = new int[high-low+1];
        int i= low;
        int j = mid+1;
        int k=0;
        // 把较小的数先移到新数组中
        while(i<=mid && j<=high){
            if(a[i]<a[j]){
                temp[k++] = a[i++];
            }else{
                temp[k++] = a[j++];
            }
        }
        // 把左边剩余的数移入数组 
        while(i<=mid){
            temp[k++] = a[i++];
        }
        // 把右边边剩余的数移入数组
        while(j<=high){
            temp[k++] = a[j++];
        }
        // 把新数组中的数覆盖nums数组
        for(int x=0;x<temp.length;x++){
            a[x+low] = temp[x];
        }
    }

Heap sort

Insert picture description here
The main idea, first understand the basic definition of binary tree
parent(i)=(i-1)/2
left_child(i)=2 i+1
right_child(i)=2
i+1; if you don’t understand these three, you don’t need to read
it. Deduced properties:
the index of the last leaf node (no child node is called a leaf node) is arr.length-1.
The index of the last non-leaf node is (arr.lenght-1-1)/2 First
look at the algorithm animation:
http://www.cs.usfca.edu/~galles/visualization/HeapSort.html The
remaining algorithm reference:
https: //blog.csdn.net/nrsc272420199/article/details/82559912
is very detailed and easy to understand.
Generally
speaking, it is nothing more than three points: 1. Construct a binary tree, which is composed of an array of large root heaps (the parent node is greater than the left and right child nodes), also called heapify
2. The head node of the tree is the maximum number, and the number at the top of the heap and the bottom of the heap (Last digit) Exchange position
3. Repeat the steps to arrange the current maximum value step by step from right to left.
Difficulties, the principle and algorithm implementation of the big root pile, refer to the blog quoted above, the writing is great.

Guess you like

Origin blog.csdn.net/weixin_43722571/article/details/99566034