Summary of seven sorting methods (stability, spatial complexity, time complexity)

Source: Internet
Author: User
Tags array length constant relative
Bubble Sort:Bubbling sort swaps their positions only when the size of adjacent elements does not meet the requirements, and it does not change the relative order of the same elements, so it is a stable sorting algorithm. Because only the cached temp variable in the bubbling sort requires memory space, the spatial complexity is constant O (1). The worst-case scenario is that each time a swap is required, the total traversal and exchange will be nearly N²/2 times, and the time complexity is O (n²). The best case is that the inner loop is traversed once to find that the sort is right, so exit the loop with a time complexity of O (n)

For the bubble sort why the best case can be explained by the O (n) Complexity:
The normal double-layer for loop is indeed O (n²) in any case, but you can make minor changes to the code to get O (n), and the modified code is as follows:

void Bubblesort (int arr[]) {
    Boolean didswap;
    for (int i = 0, len = arr.length; i < len-1; i++) {
        Didswap = false;
        for (int j = 0; J < len-i-1; j + +) {
            if (arr[j + 1] < Arr[j]) {
                swap (arr, J, j + 1);
                Didswap = true;
            }
        }
        if (Didswap = = false)
        return;}
}
Select sort:Selecting the smallest and unsorted first interchange in an unordered number each time, destroys the relative order (for example, the first unordered is 3, the second is 3, the third is the smallest of the 2 is also unsorted, at which point the third and the first one breaks the relative order between the first and the second), so it is not a stable sort Spatial complexity is constant O (1) (with bubble sort only temporary variable requires space when swapping) in either case, even if the original array is sorted, it will also cost close to N²/2 times to confirm the time complexity of O (n²) Insert Sort:Each exchange does not change the relative order of other elements, so it is a stable sorting algorithm spatial complexity is constant O (1) The worst case is the need to exchange each time, the total need to traverse and exchange will be near N²/2 times, time complexity of O (n²), the best case is already sorted well, as long as the traversal once, do not need to exchange Time Complexity of O (n) Hill Sort:The same elements will be divided into different sub-sequences, may break the relative order, so the instability of the sorting algorithm spatial complexity is constant O (1) (internal is the insertion sort) worst case and average time complexity are O (nlog2 N), the best case is uncertain Merge Sort:Segmentation and merging will not change the relative order, it is a stable sorting algorithm needs an auxiliary array, the space complexity is O (n), the space of the recursive need to save O (log2 N), so add up is still O (n) assuming that the array length is n, then the split fraction group needs log2 n Each step is a common process of merging sub-arrays, the time complexity is O (n), so its synthesis time complexity of O (nlog2 N)
With a top-down recursive implementation of the merge sort, there is a risk of stack overflow. Quick Sort:Similar to the selection sort, the fast sort elements of each interchange may not be contiguous, so it is possible to break the order between the original values and the same elements. Therefore, the fast sort is not stable first the space used by the quick Sort is O (1), and the real space is the recursive call, because each recursion is to maintain some data, the optimal case of the space complexity of O (log2 N), each time the array is divided evenly, the worst case of space complexity O (n), The case of degradation to bubble sort is the average Time complex O (nlog2 N), the worst time to reach O (n²) Heap Sort:is not adjacent to the exchange, so the unstable sort does not need the auxiliary array, the space complexity of O (1) to build the heap process, from N/2 to 0, time complexity is O (n), the process of adjusting the heap is the parent-child node of the heap adjustment, the number of executions is the heap depth log2 N, a total of n times, Add up time complexity not O (nlog2 N) Picture Summary

reference Eight sorting algorithms summary and Java implementation Seven common classic sorting algorithms summary (c + + implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.