Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mySort1(iterable) {
- let p = 0,
- i = 0,
- arr = [...iterable],
- e = arr.length - 1;
- while (p < e) {
- if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; }
- if (i > 0 && arr[i - 1] > arr[i]) { i--; continue; }
- i = ++p;
- }
- return arr;
- }
- function mySort2(arr, p = 0, i = 0, e = arr.length - 1) {
- if (p >= e) { return arr; }
- if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; }
- if (i > 0 && arr[i - 1] > arr[i]) { return mySort2(arr, p, --i, e); }
- return mySort2(arr, ++p, p, e);
- }
- const mySort3 = (() => {
- let p = 0,
- i,
- arr,
- e;
- return function innerFn(iterable) {
- if (p === 0) { i = p; e = (arr = [...iterable]).length - 1; }
- if (p >= e) { p = 0; return arr; }
- if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; }
- if (i > 0 && arr[i - 1] > arr[i]) { i--; return innerFn(); }
- i = ++p;
- return innerFn();
- };
- })();
- const mySort4 = (function outerFn(arr, p, i, e) {
- return (iterable) => {
- if (p === undefined) { p = i = 0; e = (arr = [...iterable]).length - 1; }
- if (p >= e) { return arr; }
- if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; }
- if (i > 0 && arr[i - 1] > arr[i]) { return outerFn(arr, p, --i, e)(); }
- return outerFn(arr, ++p, p, e)();
- };
- })();
Add Comment
Please, Sign In to add comment