Time complexity O (N), space complexity O (1) sorting

Source: Internet
Author: User

Description:

How to sort n numbers requires time complexity O (N) and space complexity O (1)

 

Resolution:

Use the Count sorting method to set an int array of 65536 in the range of a [0] ~ A [65535], and the initial value is 0, and then the number of N is traversed. Assume that the number of N is in the array [0... in n-1], the I value ranges from 0 to n-1 and executes a [array [I] ++ at the same time, and then reads array a in sequence. When the value is not 0, read the corresponding subscript back to the array, count is read several times, overwrite the original number, the obtained array is the sorting request

The space complexity is known as 65536, and the number of execution cycles is about N + 65536. Therefore, the space complexity is O (n), and the space complexity is O (1 ),CodeOmitted

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.