Node.js examples for Algorithm:Sort
Cocktail sort an array
function cocktailSort(arr) { var start = 0;//from www .ja va 2 s .c om var end = arr.length - 2; var i; while (start <= end) { for (i = start; i <= end; i++) { if (arr[i] > arr[i + 1]) { swap(arr, i, i + 1); } } start++ for (i = end; i >= start; i--) { if (arr[i] < arr[i - 1]) { swap(arr, i, i - 1); } } end--; } return arr; }