jsort2.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. //void qsortvec(void **a, size_t n, size_t es)
  14. {
  15. #ifdef PARANOID
  16. void **starta = a;
  17. size_t startn = n;
  18. #endif
  19. VECTOR pa;
  20. VECTOR pb;
  21. VECTOR pc;
  22. VECTOR pd;
  23. VECTOR pl;
  24. VECTOR pm;
  25. VECTOR pn;
  26. size_t d, swap_cnt;
  27. int r;
  28. reloop:
  29. swap_cnt = 0;
  30. if (n < 7) {
  31. for (pm = a + 1; pm < a + n; pm++)
  32. for (pl = pm; pl > a && CMP(pl-1, pl) > 0; pl--)
  33. SWAP(pl, pl-1);
  34. return;
  35. }
  36. pm = a + (n / 2);
  37. if (n > 7) {
  38. pl = a;
  39. pn = a + (n - 1) ;
  40. if (n > 40) {
  41. d = (n / 8);
  42. pl = MED3(pl, pl + d, pl + 2 * d);
  43. pm = MED3(pm - d, pm, pm + d);
  44. pn = MED3(pn - 2 * d, pn - d, pn);
  45. }
  46. pm = MED3(pl, pm, pn);
  47. }
  48. SWAP(a, pm);
  49. pa = pb = a + 1;
  50. pc = pd = a + (n - 1);
  51. for (;;) {
  52. while (pb <= pc && (r = CMP(pb, a)) <= 0) {
  53. if (r == 0) {
  54. swap_cnt = 1;
  55. SWAP(pa, pb);
  56. pa++;
  57. }
  58. pb++;
  59. }
  60. while (pb <= pc && (r = CMP(pc, a)) >= 0) {
  61. if (r == 0) {
  62. swap_cnt = 1;
  63. SWAP(pc, pd);
  64. pd--;
  65. }
  66. pc--;
  67. }
  68. if (pb > pc)
  69. break;
  70. SWAP(pb, pc);
  71. swap_cnt = 1;
  72. pb++;
  73. pc--;
  74. }
  75. if ((swap_cnt == 0)&&(n<1000)) { /* Switch to insertion sort */
  76. for (pm = a + 1; pm < a + n; pm++)
  77. for (pl = pm; pl > a && CMP(pl - 1, pl) > 0; pl --)
  78. SWAP(pl, pl-1);
  79. return;
  80. }
  81. pn = a + n;
  82. r = MIN(pa - a, pb - pa);
  83. VECTOR v1 = a;
  84. VECTOR v2 = pb-r;
  85. while (r) {
  86. SWAP(v1,v2); v1++; v2++; r--;
  87. };
  88. r = MIN(pd - pc, pn - pd - 1);
  89. v1 = pb;
  90. v2 = pn-r;
  91. while (r) {
  92. SWAP(v1,v2); v1++; v2++; r--;
  93. };
  94. // iterates longest sequence to save stack space
  95. int r1 = pb-pa;
  96. int r2 = pd-pc;
  97. if (r1<r2) {
  98. if (r1>1)
  99. RECURSE(a, r1);
  100. a = pn - r2;
  101. n = r2;
  102. }
  103. else {
  104. if (r2>1)
  105. RECURSE(pn-r2, r2);
  106. n = r1;
  107. }
  108. #ifndef PARANOID
  109. // if (n>1) // not needed as we know probably is already
  110. goto reloop;
  111. #else
  112. RECURSE(a, n);
  113. a = starta;
  114. n = startn;
  115. for (pa = a + 1; pa < a + n; pa++)
  116. if (CMP(pa-1, pa) > 0) {
  117. PrintLog("***qsortvec failed sorting");
  118. assertex("qsortvec failed");
  119. }
  120. #endif
  121. }