// $Id: heapsort.csharp,v 1.0 2002/09/28 10:21:00 dada Exp $
// http://dada.perl.it/shootout/
// contributed by Erik Saltwell
using System;
namespace HeapSort
{
class App
{
public const long IM = 139968;
public const long IA = 3877;
public const long IC = 29573;
public static long last = 42;
public static double gen_random(double max)
{
return( max * (last = (last * IA + IC) % IM) / IM );
}
public static int count =0;
[STAThread]
static void Main(string[] args)
{
count = int.Parse(args[0]);
double[] ary = new double[count+1];
unsafe
{
for(int i=0;i<=count;++i)
{
ary[i]=gen_random(1);
}
}
heapsort(ary);
Console.WriteLine(ary[count]);
}
public static void heapsort(double[] ra)
{
unsafe
{
int l, j, ir, i;
double rra;
l = (count >> 1) + 1;
ir = count;
for (;;)
{
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 << 1;
while (j <= ir)
{
if (j < ir && ra[j] < ra[j+1]) { ++j; }
if (rra < ra[j])
{
ra[i] = ra[j];
j += (i = j);
}
else
{
j = ir + 1;
}
}
ra[i] = rra;
}
}
}
}
}