Skip to content

Instantly share code, notes, and snippets.

@chirag64
Created June 30, 2016 18:44
Show Gist options
  • Select an option

  • Save chirag64/91d65934ea394e1399eea07afbbe119b to your computer and use it in GitHub Desktop.

Select an option

Save chirag64/91d65934ea394e1399eea07afbbe119b to your computer and use it in GitHub Desktop.
function GnomeSort(arr) {
for (var i = 0; i < (arr.length - 1);) {
// Compare next 2 elements, if they are in correct order, go ahead, else swap them and go back one step
if (arr[i] <= arr[i + 1]) {
i++;
} else {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
if (i > 0) {
i--;
}
else {
i++;
}
}
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment