Bubble sort examination I solved bubble sort in an exam, but the examiner wasn't satisfied with the solution. Please tell me why my solution is unsatisfactory.

function bubbleSort(myArray){
  var temp = undefined,isSwap = false;

  for(var x = 0; x &lt; myArray.length; x++){
   if(((x+1) &lt;= (myArray.length - 1)) &amp;&amp; myArray[x] &gt; myArray[x+1]){
     temp = myArray[x];
     myArray[x] = myArray[x+1];
     myArray[x+1] = temp;
     isSwap = true;           
   }else if(isSwap === true &amp;&amp; x === (myArray.length - 1)){
     x = -1;
     isSwap = false;
   }else{
     continue;
   }
 }
 return myArray;
}


Just to see the performance I added a print statement i.e. console.log
in the start of the loop like below

for(var x = 0; x &lt; myArray.length; x++){
    console.log(myArray);


This is what I am able to see for the specific input:

console.log(bubbleSort([5,1,6,2,0]));


Output:



rahul@rahul:~/myPractise/Algo$ node sorting.js 
[ 5, 1, 6, 2, 0 ]
[ 1, 5, 6, 2, 0 ]
[ 1, 5, 6, 2, 0 ]
[ 1, 5, 2, 6, 0 ]
[ 1, 5, 2, 0, 6 ]
[ 1, 5, 2, 0, 6 ]
[ 1, 5, 2, 0, 6 ]
[ 1, 2, 5, 0, 6 ]
[ 1, 2, 0, 5, 6 ]
[ 1, 2, 0, 5, 6 ]
[ 1, 2, 0, 5, 6 ]
[ 1, 2, 0, 5, 6 ]
[ 1, 0, 2, 5, 6 ]
[ 1, 0, 2, 5, 6 ]
[ 1, 0, 2, 5, 6 ]
[ 1, 0, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
[ 0, 1, 2, 5, 6 ]
As @Zeta said, only your examiner can tell you why he wasn't satisfied.
But here are a couple of suggestions anyway:


Variable names are important and myArray doesn't mean much. You can try inputArray for example.
Changing the index of a for loop is usually not a good idea, because it makes the code a bit more difficult to understand (not by a lot, but why make it harder?).


This is another go at the same algorithm, which I think is more readable:

function bubbleSort(inputArray)
{
  var index;
  var done = false;
  var temp;

  while ( !done )
  {
    done = true;
    for(var index = 0; index &lt; inputArray.length; index++)
    {
      if ( inputArray[index] &gt; inputArray[index+1] )
      {
        temp = inputArray[ index ];
        inputArray[ index ] = inputArray[ index+1 ];
        inputArray[ index+1 ] = temp;
        done = false;
      }
    }
  }

  return inputArray;
}