Created
August 16, 2014 10:40
-
-
Save AllenSH12/94bb1e943629a0f4ea3a to your computer and use it in GitHub Desktop.
// source http://jsbin.com/rusux/1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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