## [RESOLVED] bubble sort problems

Hi guys,

I'm new to JavaScript and I've got some javascript code that I'm trying to solve for my first uni course. I've emailed my tutor but they have not got back to me and I'm running out of time. If anyone can point me in the right direction then I would appreciate your help. I have entered the code I have done so far. I'm trying to get the inner and outer loop to count each time it is used so I can measure the efficiency of the programme. I'm also trying to get the programme to stop once the array is sorted and there is no more swaps to be done but as yet have no idea how to do this without using a while loop condition. I know there are better ways of doing this but this is how they want it done.

function bubbleSort(arrayToSort)
{
// declare and initialise a variable to hold the length of the argument array
var length = arrayToSort.length;

//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = arrayToSort[i];
}

// PLACE YOUR CODE FOR THE FUNCTION HERE
/* */

var temp;
for (j = 0; j < returnArray.length - 1; j = j + 1)
{

for (k = 0; k < returnArray.length; k = k + 1)
{

if (returnArray[k] > returnArray[k + 1])
{

temp = returnArray[k + 1];

returnArray[k + 1] = returnArray[k];

returnArray[k] = temp;
}
}

}
total = j + k;
window.alert ('The number of loops to sort the array was: ' + total);
return returnArray;
}