Code of heap sort (javascript)

must

#1

var a = [12,11,10,9,8,7,6,2];

var heap_size = a.length - 1;

var heapify = function (a,i) {

	var largest = 0;

	if(2*i + 1 <= heap_size && a[2*i + 1]> a[i])

	{
		largest = 2*i + 1;

	} 

	else
	{
		largest = i;
	}


	if( 2*i + 2 <= heap_size && a[2*i +  2] > a[largest])

	{
		largest = 2*i + 2;

	}


	if(largest != i)
	{
		var temp = a[i]
		a[i] = a[largest]
		a[largest] = temp
		heapify(a,largest)

	}

}

for(var i = a.length/2 - 1;i>=0;--i)
{

	heapify(a,i);
}



console.log("after heapify " + a);


for(var i = a.length-1;i>0;--i)
{
	var temp = a[heap_size]
	a[heap_size] = a[0]
	a[0] = temp;
	heap_size = heap_size - 1;
	heapify(a,0);

}




console.log("after final sort" + a);