I know of no way to check if a bubble sort is finished early

other than to go through the entire array at least once

to check the current order and determine if swaps are required.

You can create a function to do this easily, but it will add

to the length of time that is required to perform the 'bubbleSort'

function. At the end of each 2nd loop swap check, you can do

something like check the status of the current array for any

additional swaps that might be required.

Code:

function isSortFinished() {
var needSwaps = 0;
for (var i=0; i<BSortedArray.length-1; i++) {
if (BSortedArray[i] > BSortedArray[i+1]) { needSwaps++; }
}
return needSwaps;
}

Then modify your sort code like:

Code:

var temp;
for (j = 0; j < returnArray.length - 1; j = j + 1) {
outerCnt++;
for (k = 0; k < returnArray.length; k = k + 1) {
innerCnt++;
if (returnArray[k] > returnArray[k + 1]) {
temp = returnArray[k + 1];
returnArray[k + 1] = returnArray[k];
returnArray[k] = temp;
}
}
if (isSortFinished() == 0) { break; }
}

Could also put check before the first outer loop to see if 'BSortedArray'

is already in sorted order and not go through the sort loop to begin with.

All code above is untested, so make sure you understand my comments before you do the modifications.

Post back if you don't understand my comments.

Good Luck!