public class HeapSort
{

    private long[] theArray;
    private long[] theHeap;
    private int heapSize;


    public static void main(String[] args)
    {
	int num = Integer.parseInt(args[0]);
	int reps = Integer.parseInt(args[1]);
	long[] arr = new long[num];
	for (int j=0; j<reps; j++)
	    {
		HeapSort m = new HeapSort(num);
		m.sort();
		//m.show();
	    }
    }

    public HeapSort(int num)
    {
	theArray = new long[num];
	for (int i=0; i<num; i++)
	    {
		theArray[i] = (int)(Math.random()*1000000000);
	    }
    }

    public void show()
    {
	for (int i=0; i<theArray.length; i++)
	    System.out.println(theArray[i]);
    }

    public void sort() {
	theHeap = new long[theArray.length];
	for (int i=0; i<theArray.length; i++)
	    heapInsert(theArray[i]);
	for (int i=0; i<theArray.length; i++)
	    theArray[i] = heapDelete();
    }


    private void heapInsert(long item)
    {
	theHeap[heapSize] = item;
	int att = heapSize;
	int parentloc = (att-1)/2;
	while (theHeap[att] > theHeap[parentloc])
	    {
		long tmp = theHeap[att];
		theHeap[att]=theHeap[parentloc];
		theHeap[parentloc]=tmp;
		att=parentloc;
		parentloc = (att-1)/2;
	    }
	heapSize++;
    }

    private long heapDelete()
    {
	heapSize--;
	long tmp = theHeap[0];
	theHeap[0]=theHeap[heapSize];
	int p = 0;
	int c1 = 1;
	int c2 = 2;
	while (c1<heapSize && c2<heapSize && (theHeap[p]<theHeap[c1] || theHeap[p]<theHeap[c2]))
	    {
		if (theHeap[c1]>theHeap[c2])
		    {
			long stmp = theHeap[p];
			theHeap[p]=theHeap[c1];
			theHeap[c1]=stmp;
			p=c1;
			c1 = 2*p+1;
			c2=2*p+2;
		    }
		else
		    {
			long stmp = theHeap[p];
			theHeap[p]=theHeap[c2];
			theHeap[c2]=stmp;
			p=c2;
			c1=2*p+1;
			c2=2*p+2;
		    }
	    }	       
	return tmp;
    }
}
