Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var a, cocktail_sort, t;
a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
cocktail_sort = function(array) {
var i, j, k, swapped, tmp, tmp_two, _i, _j, _k, _ref, _ref1;
for (k = _i = _ref = array.length - 1; _ref <= 1 ? _i <= 1 : _i >= 1; k = _ref <= 1 ? ++_i : --_i) {
swapped = false;
for (i = _j = k; k <= 1 ? _j <= 1 : _j >= 1; i = k <= 1 ? ++_j : --_j) {
if (array[i] < array[i - 1]) {
tmp = array[i];
array[i] = array[i - 1];
array[i - 1] = tmp;
swapped = true;
}
}
for (j = _k = 0, _ref1 = k - 1; 0 <= _ref1 ? _k <= _ref1 : _k >= _ref1; j = 0 <= _ref1 ? ++_k : --_k) {
if (array[j] > array[j + 1]) {
tmp_two = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp_two;
swapped = true;
}
}
if (!swapped) {
return array;
}
}
};
t = cocktail_sort(a);
console.log(t);
</script>
<script id="jsbin-source-javascript" type="text/javascript">a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]
cocktail_sort = (array) ->
for k in [array.length - 1..1]
swapped = false
for i in [k..1]
if array[i] < array[i - 1]
tmp = array[i]
array[i] = array[i - 1]
array[i - 1] = tmp
swapped = true
for j in [0..k-1]
if array[j] > array[j + 1]
tmp_two = array[j]
array[j] = array[j + 1]
array[j + 1] = tmp_two
swapped = true
if !swapped
return array
t = cocktail_sort(a)
console.log t</script></body>
</html>
var a, cocktail_sort, t;
a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
cocktail_sort = function(array) {
var i, j, k, swapped, tmp, tmp_two, _i, _j, _k, _ref, _ref1;
for (k = _i = _ref = array.length - 1; _ref <= 1 ? _i <= 1 : _i >= 1; k = _ref <= 1 ? ++_i : --_i) {
swapped = false;
for (i = _j = k; k <= 1 ? _j <= 1 : _j >= 1; i = k <= 1 ? ++_j : --_j) {
if (array[i] < array[i - 1]) {
tmp = array[i];
array[i] = array[i - 1];
array[i - 1] = tmp;
swapped = true;
}
}
for (j = _k = 0, _ref1 = k - 1; 0 <= _ref1 ? _k <= _ref1 : _k >= _ref1; j = 0 <= _ref1 ? ++_k : --_k) {
if (array[j] > array[j + 1]) {
tmp_two = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp_two;
swapped = true;
}
}
if (!swapped) {
return array;
}
}
};
t = cocktail_sort(a);
console.log(t);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment