Skip to content

Instantly share code, notes, and snippets.

@EvgenyOrekhov
Last active May 17, 2020 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvgenyOrekhov/9d5ce3cded4b7c4f66cd to your computer and use it in GitHub Desktop.
Save EvgenyOrekhov/9d5ce3cded4b7c4f66cd to your computer and use it in GitHub Desktop.
JavaScript test tasks
function bubbleSort(array) {
"use strict";
function compareAndSwap(item, index) {
const nextItem = array[index + 1];
if (item > nextItem) {
array[index + 1] = item;
array[index] = nextItem;
}
}
array.forEach(() => array.forEach(compareAndSwap));
return array;
}
function combSort(array) {
"use strict";
let gap = array.length;
const shrinkFactor = 1.3;
function compareAndSwap(item, index) {
const nextItem = array[index + gap];
if (item > nextItem) {
array[index + gap] = item;
array[index] = nextItem;
}
return index + 1 + gap < array.length;
}
array.every(function () {
gap = Math.floor(gap / shrinkFactor) || 1;
array.every(compareAndSwap);
return gap > 1;
});
return array;
}
function quicksort([pivot, ...rest]) {
return pivot === undefined
? rest
: [
...quicksort(rest.filter((item) => item <= pivot)),
pivot,
...quicksort(rest.filter((item) => item > pivot))
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment