// $Id: heapsort.elastic,v 1.0 2002/05/17 09:57:00 dada Exp $ package heapsort; import basic; import sys; import array; local IM = 139968; local IA = 3877; local IC = 29573; private function gen_random( n ) { static LAST = 42.0; LAST = (LAST * IA + IC) % IM; return(n * LAST / IM); } private function heapsort( n, ra ) { local rra; local i; local j; local l = (n / 2) + 1; local ir = n; while (1) { if (l > 1) { rra = ra[--l]; } else { rra = ra[ir]; ra[ir] = ra[1]; if (--ir == 1) { ra[1] = rra; return; } } i = l; j = l * 2; while (j <= ir) { if ( (j < ir) && (ra[j] < ra[j+1]) ) { j++; } if (rra < ra[j]) { ra[i] = ra[j]; i = j; j = j + i; } else { j = ir + 1; } } ra[i] = rra; } } private n = 1; private i; private result = 0; private ary = #[]; if(array.length(sys.args) > 0) { n = basic.int(sys.args[0]); } else { n = 1; } for(i=1; i<=n; i++) { ary[i] = gen_random(1.0); } heapsort(n, ary); basic.print(ary[-1]);