nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
229 lines (228 loc) • 7.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../collections/index");
function heapSort(input, comparator) {
const computeHeap = (array, index, heapSize, cmp) => {
const left = 2 * index + 1;
const right = 2 * index + 2;
let largest = index;
if (left < heapSize && cmp(array[left], array[index]) > 0) {
largest = left;
}
if (right < heapSize && cmp(array[right], array[largest]) > 0) {
largest = right;
}
if (largest !== index) {
const temp1 = array[index];
array[index] = array[largest];
array[largest] = temp1;
computeHeap(array, largest, heapSize, cmp);
}
};
const buildMaxHeap = (array, cmp) => {
for (let i = Math.floor(array.length / 2); i >= 0; i -= 1) {
computeHeap(array, i, array.length, cmp);
}
return array;
};
let size = input.length;
let temp;
buildMaxHeap(input, comparator);
for (let i = input.length - 1; i > 0; i -= 1) {
temp = input[0];
input[0] = input[i];
input[i] = temp;
size -= 1;
computeHeap(input, 0, size, comparator);
}
return input;
}
exports.heapSort = heapSort;
function quickSort(input, comparator) {
const swap = (array, i, j) => {
const temp = array[i];
array[i] = array[j];
array[j] = temp;
return array;
};
const partition = (array, left, right, compare) => {
const cmp = array[right - 1];
let minEnd = left;
let maxEnd;
for (maxEnd = left; maxEnd < right - 1; maxEnd += 1) {
if (compare(array[maxEnd], cmp) < 0) {
swap(array, maxEnd, minEnd);
minEnd += 1;
}
}
swap(array, minEnd, right - 1);
return minEnd;
};
const quickSortImpl = (array, left, right, cmp) => {
if (left < right) {
const p = partition(array, left, right, cmp);
quickSortImpl(array, left, p, cmp);
quickSortImpl(array, p + 1, right, cmp);
}
return array;
};
return quickSortImpl(input, 0, input.length, comparator);
}
exports.quickSort = quickSort;
function insertionSort(input, comparator) {
let current;
let j;
for (let i = 1; i < input.length; i += 1) {
current = input[i];
j = i - 1;
while (j >= 0 && comparator(input[j], current) > 0) {
input[j + 1] = input[j];
j -= 1;
}
input[j + 1] = current;
}
return input;
}
exports.insertionSort = insertionSort;
function mergeSort(input, comparator) {
const mergeSortImpl = (array, cmp, start, end) => {
start = start || 0;
end = end || array.length;
if (Math.abs(end - start) <= 1) {
return [];
}
const middle = Math.ceil((start + end) / 2);
mergeSortImpl(array, cmp, start, middle);
mergeSortImpl(array, cmp, middle, end);
return mergeImpl(array, cmp, start, middle, end);
};
const mergeImpl = (array, cmp, start, middle, end) => {
const left = new index_1.LinkedList();
const right = new index_1.LinkedList();
const leftSize = middle - start;
const rightSize = end - middle;
const maxSize = Math.max(leftSize, rightSize);
const size = end - start;
let i;
for (i = 0; i < maxSize; i += 1) {
if (i < leftSize) {
left.add(array[start + i]);
}
if (i < rightSize) {
right.add(array[middle + i]);
}
}
i = 0;
while (i < size) {
if (left.first && right.first) {
if (cmp(left.first, right.first) > 0) {
array[start + i] = right.shift();
}
else {
array[start + i] = left.shift();
}
}
else if (left.first) {
array[start + i] = left.shift();
}
else {
array[start + i] = right.shift();
}
i += 1;
}
return array;
};
return mergeSortImpl(input, comparator, 0, input.length);
}
exports.mergeSort = mergeSort;
function bucketSort(input) {
const insertionSortImpl = (array) => {
let current;
let j;
for (let i = 1; i < array.length; i += 1) {
current = array[i];
j = i - 1;
while (j >= 0 && current < array[j]) {
array[j + 1] = array[j];
j -= 1;
}
array[j + 1] = current;
}
return array;
};
const createBuckets = (array) => {
const buckets = [];
let currentBucket;
let current;
for (let i = 0; i < array.length; i += 1) {
current = array[i];
currentBucket = Math.floor(current);
buckets[currentBucket] = buckets[currentBucket] || [];
buckets[currentBucket].push(current);
}
return buckets;
};
const sortBuckets = (buckets) => {
for (let i = 0; i < buckets.length; i += 1) {
if (buckets[i] !== undefined) {
insertionSortImpl(buckets[i]);
}
}
return buckets;
};
const unionBuckets = (buckets) => {
let result = [];
let currentBucket;
for (let i = 0; i < buckets.length; i += 1) {
currentBucket = buckets[i];
if (currentBucket !== undefined) {
result = result.concat(currentBucket);
}
}
return result;
};
const bucketsm = createBuckets(input);
sortBuckets(bucketsm);
return unionBuckets(bucketsm);
}
exports.bucketSort = bucketSort;
function countingSort(input) {
const getCount = (array) => {
const count = [];
let current;
for (let i = 0; i < array.length; i += 1) {
current = array[i];
count[current] = (count[current] || 0) + 1;
}
return count;
};
const getLessCount = (array) => {
const less = [];
let last;
less[0] = array[0] || 0;
for (let i = 1; i < array.length; i += 1) {
last = array[i - 1] || 0;
less[i] = last + less[i - 1];
}
return less;
};
const sortImpl = (array, less) => {
const result = [];
const currentPositions = [];
let current;
let position;
for (let i = 0; i < array.length; i += 1) {
current = array[i];
position = less[current];
if (currentPositions[current] === undefined) {
currentPositions[current] = position;
}
result[currentPositions[current]] = current;
currentPositions[current] += 1;
}
return result;
};
const lessy = getLessCount(getCount(input));
return sortImpl(input, lessy);
}
exports.countingSort = countingSort;