Introduction to algorithms: orders the N numbers between [0 .. n ^-1] In 8.3-4 O (n) time.

Source: Internet
Author: User
I. Question

How to sort the N integers between 0 and N ^ 2-1 in O (n) Time

 

Ii. Train of Thought to convert the integer into N in hexadecimal order and then sort the number. Each number has two digits. The value range of each digit is [0 .. n-1], and then sort the base number.

 

Iii. Code [CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. # Include <cmath>
  3. Using namespace STD;
  4. Int N, Radix, length_a, digit = 2;
  5. Void print (int * a, int start, int end)
  6. {
  7. Int I;
  8. For (I = start; I <= end; I ++)
  9. {
  10. If (I = Start) cout <'{';
  11. Else cout <'';
  12. Cout <A [I];
  13. }
  14. Cout <'}' <Endl;
  15. }
  16. // Stable sorting of base sort calls
  17. Void stable_sort (int * a, int * B, int K, int D)
  18. {
  19. Int I, J;
  20. // Initialize the C array to 0 for counting
  21. Int * c = new int [k + 1];
  22. For (I = 0; I <= K; I ++)
  23. C [I] = 0;
  24. Int * D = new int [length_a + 1];
  25. For (j = 1; j <= length_a; j ++)
  26. {
  27. // D [J] indicates the I-digit number of the [J] Element
  28. D [J] = A [J] % (INT) Pow (Radix * 1.0, d)/(INT) Pow (Radix * 1.0, D-1 );
  29. // C [J] indicates the number of times the number d [J] appears in array.
  30. C [d [J] ++;
  31. }
  32. // C [I] indicates the number of times that the <= I number has appeared.
  33. For (I = 1; I <= K; I ++)
  34. C [I] = C [I] + C [I-1];
  35. // Initialize B to 0 and B to output the sorting result
  36. For (I = 1; I <= length_a; I ++)
  37. B [I] = 0;
  38. For (j = length_a; j> = 1; j --)
  39. {
  40. // If the number of numbers <= d [J] is X, after sorting, a [J] should appear at position X, that is, B [x] = A [J]
  41. B [C [d [J] = A [J];
  42. C [d [J] --;
  43. }
  44. Delete [] C;
  45. Delete [] D;
  46. }
  47. // Base sorting
  48. Void radix_sort (int * a, int * B)
  49. {
  50. Int I, J;
  51. // Sort each bit in sequence, from low to high
  52. For (I = 1; I <= digit; I ++)
  53. {
  54. Stable_sort (a, B, Radix-1, I );
  55. // The input is A, the output is B, and the output data should be put into the output data when sorting again
  56. For (j = 1; j <= length_a; j ++)
  57. A [J] = B [J];
  58. }
  59. }
  60. Int main ()
  61. {
  62. Cin> N;
  63. Length_a = N;
  64. Int * A = new int [n + 1];
  65. Int * B = new int [n + 1];
  66. Bool flag [1000] = {0 };
  67. Int I;
  68. // Generate n random data ranges from 0 to N ^-1.
  69. For (I = 1; I <= N; I ++)
  70. {
  71. Do
  72. {
  73. A [I] = rand () % (n * n );
  74. } While (flag [A [I]);
  75. Flag [A [I] = 1;
  76. }
  77. Print (A, 1, n );
  78. Radix = N;
  79. Radix_sort (A, B );
  80. Print (A, 1, n );
  81. Return 0;
  82. }

Reprinted from: http://blog.csdn.net/mishifangxiangdefeng/article/details/7685839

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.