/* Same as qsort1, but the structure definition is merged with the stack definition. */ #include #include double smallest(double *x,int xlen) { int i; double result; if (xlen < 1) return 0; result = x[0]; for (i = 1;i < xlen;++i) if (x[i] < result) result = x[i]; return result; } double largest(double *x,int xlen) { int i; double result; if (xlen < 1) return 0; result = x[0]; for (i = 1;i < xlen;++i) if (x[i] > result) result = x[i]; return result; } void quicksort(double *x,int xlen) { struct doublechunk { double *x; int xlen; } stack[100]; int stacklen = 0; int i; int j; double split; double t; for (;;) { if (xlen < 2) { if (!stacklen) return; --stacklen; x = stack[stacklen].x; xlen = stack[stacklen].xlen; continue; } split = smallest(x,xlen); split += 0.5 * (largest(x,xlen) - split); i = 0; j = xlen; for (;;) { if (i == j) break; if (x[i] <= split) { ++i; continue; } if (x[j - 1] > split) { --j; continue; } t = x[i]; x[i] = x[j - 1]; x[j - 1] = t; } while (i && (x[i - 1] == split)) --i; if (stacklen == 100) return; /* out of space! this will never happen */ if (i > xlen - j) { stack[stacklen].x = x; stack[stacklen].xlen = i; x += j; xlen -= j; } else { stack[stacklen].x = x + j; stack[stacklen].xlen = xlen - j; xlen = i; } ++stacklen; } } int main(void) { double *x = 0; int xspace = 0; int xlen = 0; int i; double r; while (scanf("%lf",&r) == 1) { if (xlen == xspace) if (!(x = realloc(x,(xspace = xspace * 2 + 1) * sizeof(double)))) { fprintf(stderr,"out of memory\n"); return 111; } x[xlen++] = r; } quicksort(x,xlen); for (i = 0;i < xlen;++i) printf("%f\n",x[i]); return 0; }