Interview must test!!! Insertion sort/selection sort/heap sort/quick sort...

1 Insertion sort

Ideas:
  
(1) Divide the array into "sorted interval" and "range to be sorted", use bound to identify these two intervals, bound is the first position of the interval to be sorted;
(2) Divide the first position of the interval to be sorted An element (that is, the element at the position of the bound mark) is compared with the element in the sorted interval.

public static void insertSort(int[] arr) {
    
    
        // 划分“已排区间” 和 “待排区间”
        int bound = 1;
        // 用于控制边界值向后走
        // 即,待排区间往后缩小
        for (; bound < arr.length; bound ++) {
    
    
            int v = arr[bound];
            // 待排区间的前一个元素
            int cur = bound - 1;
            // 已排区间从后往前遍历
            // 寻找合适的位置插入 arr[bound]
            for (; cur >= 0; cur --) {
    
    
                if (arr[cur] > v) {
    
    
                // 元素向后移动一位
                   arr[cur + 1] = arr[cur];
                } else {
    
    
                    break;
                }
            }
            // 找到合适位置,插入
            arr[cur + 1] = v;
        }
    }

Time complexity: O( N 2 )
Space complexity: O(1)
Stable sorting

2 Hill sort

Idea:
  Specify an interval (interval Hill sequence, length / 2, length / 4, length / 8… 1), divide the array into several groups, sort each group separately, and then adjust the grouping changes to gradually make the array change Be orderly. Two methods can be constructed. The first method constructs the grouping coefficient, and the second method uses the previous gap as the basis for group insertion.
  Hill sort is an upgraded version of insertion sort, so its code implementation and idea are very similar to insertion sort.

public static void shellSort(int[] arr) {
    
    
    int gap = arr.length / 2;
    while (gap >= 1) {
    
    
        _shellSort(arr,gap);
        gap = gap / 2;
    }
}

public static void _shellSort(int[] arr,int gap) {
    
    
    int bound = gap;
    for (; bound < arr.length ; bound++) {
    
    
        int v = arr[bound];
        int cur = bound - gap;
        for (; cur >= 0 ; cur = cur - gap) {
    
    
            if (arr[cur] > v) {
    
    
                arr[cur + gap] = arr[cur];
            } else {
    
    
                break;
            }
        }
        arr[cur + gap] = v;
    }
}

Time complexity: O(N 2 ) => O(N 1.3 )
Space complexity: O(1)
unstable sort

3 Select sort

(1) Divide the array into two intervals, the interval to be sorted and the interval already sorted, but note that the difference here is that it is different from the insertion sort.The insertion sort bound can be any element, and the selection sort bound must start from 0. Because selection sorting will select the smallest value and place it at the end of the ranked interval when selecting and sorting in the subsequent selection. If you choose randomly at the beginning, the subsequent elements may not be larger than this element. Make sure that the first selected element is the smallest of the array. Elements;

(2) Traverse the interval to be arranged, find the smallest element by playing a ring, and use the first position of the interval to be arranged as the ring.
  

public static void selectOrder(int[] arr) {
    
    
    int bound = 0;
    for (;bound < arr.length; bound ++) {
    
    
        for (int cur = bound + 1; cur < arr.length; cur++) {
    
    
            if (arr[cur] < arr[bound]) {
    
    
                swap(arr,cur,bound);
            }
        }
    }
}

public static void swap (int[] arr,int cur,int bound) {
    
    
    int tmp = arr[cur];
    arr[bound] = arr[cur];
    arr[cur] = tmp;
}

Time complexity: O(N 2 )
Space complexity: O(1)
Unstable sorting

4 heap sort

(1) Create a large
heap with the current array; (2) Delete the top element of the heap, that is, exchange the top element and the last element, and then delete the last element from the heap. Note that it is not deleted from the array, the maximum value is Is placed at the end of the array;
(3) Adjust downward from element 0 to make it a heap again;
(4) Exchange the first element and the last element of the heap again, and the last element at this time is The second-to-last element of the array puts the second-largest number in the second-to-last position of the array, and so on, continuously looping.
  

public static void heapSort(int[] arr) {
    
    
    createHeap(arr);
    int heapSize = arr.length;
    for (int i = 0; i < arr.length; i++) {
    
    
        // 交换堆上的 第一个和最后一个元素
        // 注意堆的最后一个元素并不是每次都是数组的最后一个元素
        swap(arr,0,heapSize - 1);
        // 删除堆的最后一个元素,但是它还存在在数组中
        heapSize --;
        // 只用调整一次,因为其本身已经是堆,只是交换后的第一个元素需要调整
        shiftDown(arr,heapSize,0);
    }
}

private static void createHeap(int[] arr) {
    
    
    for (int i = ((arr.length - 1) - 1 ) / 2; i >= 0 ; i--) {
    
    
        shiftDown(arr,arr.length,i);
    }
}

public static void shiftDown(int[] arr,int size,int index) {
    
    
    int parent = index;
    int child = 2 * parent + 1;
    while (child < size) {
    
    
        if (child + 1 < size && arr[child + 1] > arr[child]) {
    
    
            child = child + 1;
        }
        if (arr[child] > arr[parent]) {
    
    
            swap(arr,child,parent);
        }
        parent = child;
        child = 2 * parent + 1;
    }
}

Time complexity: O( Nlog N )
Space complexity: O(1)
Unstable sorting

5 Bubble sort

Idea:
  Compare two adjacent elements, if they do not meet the ascending order, exchange them. If traversing from back to front, after one traversal, the maximum value will come to the end; if traversing from front to back, the minimum value will be after one traversal. Coming to the end.

public static void bubbleSort(int[] arr) {
    
    
    for (int bound = 0; bound < arr.length; bound++) {
    
    
        for (int cur = arr.length - 1; cur > bound ; cur--) {
    
    
            if (arr[cur - 1] > arr[cur]) {
    
    
                swap(arr,cur - 1,cur);
            }
        }
    }
}

Time complexity: O(N 2 )
Space complexity: O(1)
Stable sorting

6 Quick sort

Ideas:
(1) Select a "reference value" in the array to be sorted, and then organize the array into: the left side is smaller than the reference value, and the right side is larger than the reference value;
(2) Use the left and right subscripts to go from both sides Middle traversal.

public static void quickSort(int[] arr) {
    
    
    _quickSort(arr,0,arr.length - 1);
}

public static void _quickSort(int[] arr,int left,int right) {
    
    
    if (left >= right) {
    
    
        return;
    }
    // index 表示基准值所在的位置
    int index = partition(arr,left,right);
    _quickSort(arr,left,index - 1);
    _quickSort(arr,index + 1,right);
}

public static int partition(int[] arr,int left,int right) {
    
    
    // v 用来存放基准值
    int v = arr[right];
    int l = left;
    int r = right;
    while (l < r) {
    
    
        // 找到大于基准值的元素的位置
        while (l < r && arr[l] <= v) {
    
    
            l ++;
        }
        // 找到小于基准值的元素的位置
        while (l < r && arr[r] >= v) {
    
    
            r --;
        }
        // 交换两个位置的元素
        swap(arr,l,r);
    }
    // 当 l == r 时,循环进不去
    // 交换 l位置 和 right位置的元素,right 为基准值的位置
    swap(arr,l,right);
    return l;
}

Time complexity: O(NlogN) Worst O(N)
Space complexity: O(logN) Worst O(N) Depends on the depth of recursion
Unstable sorting

Optimization methods for quick sorting:
1. Take the middle of the three numbers: that is, take three numbers, the first number, the last number and the middle number of the array, whichever is the middle number;
2. When the interval to be processed When it is relatively small, the recursion is not continued, and the insertion sort is performed directly on the interval;
3. When the recursion depth reaches a certain depth and the current interval to be processed is still relatively large, you can also use heap sort

Guess you like

Origin blog.csdn.net/qq_50916191/article/details/115174360