How to implement sorting in O (n) time, and the spatial complexity is O (1)

Source: Internet
Author: User

For a common sorting algorithm, it is difficult to sort in O (n) time, and the space complexity is O (1), here is a way to achieve the requirements.

You can use the idea of hash sorting, which is to hash all the numbers into a hash table and implement sorting. The specific algorithm idea is to find out the maximum and minimum value of this set of data, in three kinds of situations to discuss:

1, if the minimum value is negative, at the time of the hash, each number is added to the inverse of the minimum value, as the subscript value of the array.

2. If the minimum value is 0, each number is directly used as the subscript value

3. If the minimum value is positive, subtract the minimum value from each number as the subscript value

A combination of three cases can be summed up as one method, which is to subtract the minimum value from each number.

This allows the creation of an array, the subscript as a key, the array value as value, the initialization of each value of 0, indicating that there is no corresponding key value, when the corresponding key value, the value of its value plus 1, if there is a duplicate value, the subscript corresponding value value is repeated and 1 counts, When the final printout is printed, the array is scanned from the beginning, and when value is not 0 o'clock, the corresponding key value is added or subtracted from the minimum value output, and if value is greater than 1, the output is repeated.

The Java code is implemented as follows:

1  Public classHeapsort {2     3      Public Static voidHeapsort (int[] Array) {4         intN=array.length;//find the original array length5         if(n<=0)return;//if the array length is 0, exit directly6         7         intMin=array[0],max=array[0];8System.out.print ("Before sorting:");9          for(inti=0;i<n;i++)//output the array before sorting, and find the maximum and minimum valuesTen         { One             if(Min>array[i]) min=Array[i]; A             if(Max<array[i]) max=Array[i]; -System.out.print (array[i]+ ","); -         } the System.out.println (); -          -         int[] h=New int[Max-min+1];//Hash Table -          for(inti=0;i//initializing a hash table +H[i]=0; -          +          for(inti=0;i<n;i++)//Hash Sort Ah[array[i]-min]++; at          -System.out.print ("After sorting:");//output sorted Array -          for(inti=0;i) -              for(intj=1;j<=h[i];j++) -System.out.print (i+min+ ",");  -     } in      -      Public Static voidMain (string[] args) { to           int[] array={-5,6,9,15,-3,9}; +Heapsort (array);//Call Hash Sort -     } the  *}
View Code

The program output results are:

Before sorting:-5, 6, 9, 15,-3, 9,

After sorting:-5,-3, 6, 9, 9, 15,

How to implement sorting in O (n) time, and the spatial complexity is O (1)

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.