Implementation of various sorting and complexity analysis (continuous update)

Source: Internet
Author: User

Implementation of various sorting and complexity analysis (continuous update)

Stability:

Select a sorting algorithm that is not stable, such as sorting, fast sorting, Hill sorting, and heap sorting,
Bubble sorting, insert sorting, Merge Sorting, and base sorting are stable sorting algorithms.

Complexity

Bubble Method: the complexity is O (n * n ). When the data is in positive order, there will be no exchange. The complexity is O (0 ). No more.

Direct insertion and sorting: O (n * n)

Hill sorting: the complexity of the algorithm is the 1.2 power of n.

Select sorting: O (n * n)

Fast sorting: unstable, average time complexity O (nlogn), and worst case time complexity O (n2 ). The highest among all internal sorting methods is always the best in most cases. The space complexity is mainly the use of stack space caused by recursion. The best case is that the depth of the recursive tree is log2n, and the space complexity is O (logn). The worst case is, the n‐1 recursive call is required. The space complexity is O (n), the average condition, and the space complexity is O (logn)

Merge Sorting: log2 (n) * n

Heap sorting: log2 (n) * n

Algorithm Implementation:

First, implementation of quick sorting

Void quickSort (int A [], int low, int high) {if (low 


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.