static void fallbackQSort3(UInt32 *fmap, UInt32 *eclass, Int32 loSt, Int32 hiSt) { Int32 unLo, unHi, ltLo, gtHi, n, m; Int32 sp, lo, hi; UInt32 med, r, r3; Int32 stackLo[FALLBACK_QSORT_STACK_SIZE]; Int32 stackHi[FALLBACK_QSORT_STACK_SIZE]; r = 0; sp = 0; fpush(loSt, hiSt); while ( sp > 0 ) { AssertH(sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004); fpop(lo, hi); if ( hi - lo < FALLBACK_QSORT_SMALL_THRESH ) { fallbackSimpleSort(fmap, eclass, lo, hi); continue; } /* Random partitioning. Median of 3 sometimes fails to * avoid bad cases. Median of 9 seems to help but * looks rather expensive. This too seems to work but * is cheaper. Guidance for the magic constants * 7621 and 32768 is taken from Sedgewick's algorithms * book, chapter 35. */ r = ( (r * 7621) + 1 ) % 32768; r3 = r % 3; if ( r3 == 0 ) { med = eclass[fmap[lo]]; } else { if ( r3 == 1 ) { med = eclass[fmap[(lo + hi) >> 1]]; } else { med = eclass[fmap[hi]]; } }
static void fallbackQSort3 ( UInt32* fmap, UInt32* eclass, Int32 loSt, Int32 hiSt ) { Int32 unLo, unHi, ltLo, gtHi, n, m; Int32 sp, lo, hi; UInt32 med, r, r3; Int32 stackLo[FALLBACK_QSORT_STACK_SIZE]; Int32 stackHi[FALLBACK_QSORT_STACK_SIZE]; r = 0; sp = 0; fpush ( loSt, hiSt ); while (sp > 0) { AssertH ( sp < FALLBACK_QSORT_STACK_SIZE, 1004 ); fpop ( lo, hi ); if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) { fallbackSimpleSort ( fmap, eclass, lo, hi ); continue; } /* Random partitioning. Median of 3 sometimes fails to avoid bad cases. Median of 9 seems to help but looks rather expensive. This too seems to work but is cheaper. Guidance for the magic constants 7621 and 32768 is taken from Sedgewick's algorithms book, chapter 35. */ r = ((r * 7621) + 1) % 32768; r3 = r % 3; if (r3 == 0) med = eclass[fmap[lo]]; else if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else med = eclass[fmap[hi]]; unLo = ltLo = lo; unHi = gtHi = hi; while (1) { while (1) { if (unLo > unHi) break; n = (Int32)eclass[fmap[unLo]] - (Int32)med; if (n == 0) { fswap(fmap[unLo], fmap[ltLo]); ltLo++; unLo++; continue; }; if (n > 0) break; unLo++; } while (1) { if (unLo > unHi) break; n = (Int32)eclass[fmap[unHi]] - (Int32)med; if (n == 0) { fswap(fmap[unHi], fmap[gtHi]); gtHi--; unHi--; continue; }; if (n < 0) break; unHi--; } if (unLo > unHi) break; fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--; } AssertD ( unHi == unLo-1, "fallbackQSort3(2)" ); if (gtHi < ltLo) continue; n = fmin(ltLo-lo, unLo-ltLo); fvswap(lo, unLo-n, n); m = fmin(hi-gtHi, gtHi-unHi); fvswap(unLo, hi-m+1, m); n = lo + unLo - ltLo - 1; m = hi - (gtHi - unHi) + 1; if (n - lo > hi - m) { fpush ( lo, n ); fpush ( m, hi ); } else { fpush ( m, hi ); fpush ( lo, n ); } }
static void fallbackQSort3(UInt32 *fmap,UInt32 *eclass,Int32 loSt,Int32 hiSt) { Int32 sp; UInt32 r; Int32 stackLo[100]; Int32 stackHi[100]; r = 0; sp = 0; while(sp > 0){ Int32 ltLo; Int32 gtHi; Int32 n; Int32 lo; Int32 hi; UInt32 med; if (hi - lo < 10) { fallbackSimpleSort(fmap,eclass,lo,hi); continue; } /* Random partitioning. Median of 3 sometimes fails to avoid bad cases. Median of 9 seems to help but looks rather expensive. This too seems to work but is cheaper. Guidance for the magic constants 7621 and 32768 is taken from Sedgewick's algorithms book, chapter 35. */ r = (r * 7621 + 1) % 32768; UInt32 r3 = r % ((unsigned int )3); if (r3 == 0) med = eclass[fmap[lo]]; else if (r3 == 1) med = eclass[fmap[lo + hi >> 1]]; else med = eclass[fmap[hi]]; Int32 unLo = ltLo = lo; Int32 unHi = gtHi = hi; while(1){ while(1){ if (unLo > unHi) break; n = ((Int32 )eclass[fmap[unLo]]) - ((Int32 )med); if (n == 0) { ltLo++; unLo++; continue; } ; if (n > 0) break; unLo++; } while(1){ if (unLo > unHi) break; n = ((Int32 )eclass[fmap[unHi]]) - ((Int32 )med); if (n == 0) { gtHi--; unHi--; continue; } ; if (n < 0) break; unHi--; } if (unLo > unHi) break; unLo++; unHi--; } if (gtHi < ltLo) continue; n = (ltLo - lo < unLo - ltLo?ltLo - lo : unLo - ltLo); { Int32 yyp1 = lo; Int32 yyp2 = unLo - n; Int32 yyn = n; while(yyn > 0){ yyp1++; yyp2++; yyn--; } } ; Int32 m = hi - gtHi < gtHi - unHi?hi - gtHi : gtHi - unHi; { Int32 yyp1 = unLo; Int32 yyp2 = hi - m + 1; Int32 yyn = m; while(yyn > 0){ yyp1++; yyp2++; yyn--; } } ; n = lo + unLo - ltLo - 1; m = hi - (gtHi - unHi) + 1; if (n - lo > hi - m) { } else { } }