Cocktail sort an array - Node.js Algorithm

Node.js examples for Algorithm:Sort

Description

Cocktail sort an array

Demo Code

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;
}

Related Tutorials