C + + implementation of fast sorting algorithm

Source: Internet
Author: User

Quick Sort Basic Features

Complexity of Time: O (N*LGN)

Worst: O (n^2)

Space complexity: Best case: O (LGN), worst case: O (n), average condition: O (LGN)

Not stable.

About the space complexity of the quick sort, thank you @ fate his father. elaborate.

Quick sort because it takes up one space to return to the middle number each time recursion, the space complexity of one recursion is O (1).

The best case and worst-case recursive depth is O (LGN), and the corresponding space complexity is O (LGN)

At worst, the recursive depth is O (n), and the space complexity is O (n).

Algorithm

QUICKSORT (A, p, r)
    if p < R
       then Q←partition (A, p, r)   //Key
            QUICKSORT (A, p, q-1)
            QUICKSORT (A, q + 1, R)
     
PARTITION (A, P, R)
      X←a[r]
      i←p-1 for
      j←p to R-1 do if A[j]≤x then i←i
                 + 1
 exchange A[i] <-> a[j]
      exchange A[i + 1] <-> A[r] return
      i + 1

Example

Sorted Array: 7 3 5 9 8 5 1 10 4 6

A trip to the sequencing process analysis:

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.