All files / algorithms/src sort.ts

100% Statements 65/65
100% Branches 15/15
100% Functions 8/8
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1125x   9x       5x 1x     1x 8x     8x   64x   35x 35x       8x 1x       1x     5x   1x   8x 8x     8x   35x 35x     8x     1x       5x 17x 8x 8x 8x   8x 8x   8x 8x 8x     8x   16x   1x 1x     15x 15x   16x       8x   12x 12x 12x       8x   1x 1x 1x       17x     5x 15x 8x   7x 29x 29x   7x      
export const by = <Element, Key extends keyof Element>(array: Element[], key: Key): Element[] => {
    // eslint-disable-next-line security/detect-object-injection
    return array.concat().sort((a: Element, b: Element) => (a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0));
};
 
// @src https://github.com/kutyel/typescript-algorithms/blob/master/src/bubbleSort/index.ts
export const bubble = <Element>(array: Element[]): Element[] => {
    array = Array.from(array);
 
    // eslint-disable-next-line no-loops/no-loops, no-constant-condition
    while (true) {
        let swapped = false;
 
        // eslint-disable-next-line no-loops/no-loops
        for (let i = 0; i < array.length - 1; i++) {
            // eslint-disable-next-line security/detect-object-injection
            if (array[i] > array[i + 1]) {
                // eslint-disable-next-line security/detect-object-injection
                [array[i], array[i + 1]] = [array[i + 1], array[i]];
                swapped = true;
            }
        }
 
        if (!swapped) {
            break;
        }
    }
 
    return array;
};
 
export const insertion = <Element>(array: Element[]): Element[] => {
    // eslint-disable-next-line no-loops/no-loops
    for (let index = 1; index < array.length; index++) {
        // eslint-disable-next-line security/detect-object-injection
        const currentItem: Element = array[index];
        let currentLeftIndex: number = index - 1;
 
        // eslint-disable-next-line no-loops/no-loops, security/detect-object-injection
        while (currentLeftIndex >= 0 && array[currentLeftIndex] > currentItem) {
            // eslint-disable-next-line security/detect-object-injection
            array[currentLeftIndex + 1] = array[currentLeftIndex];
            currentLeftIndex -= 1;
        }
 
        array[currentLeftIndex + 1] = currentItem;
    }
 
    return array;
};
 
// @see https://www.jesuisundev.com/comprendre-les-algorithmes-de-tri-en-7-minutes/
export const merge = <Element>(array: Element[]): Element[] => {
    if (array.length > 1) {
        const middleIndex = Math.floor(array.length / 2);
        const leftSide = array.slice(0, middleIndex);
        const rightSide = array.slice(middleIndex);
 
        merge(leftSide);
        merge(rightSide);
 
        let leftIndex = 0;
        let rightIndex = 0;
        let globalIndex = 0;
 
        // eslint-disable-next-line no-loops/no-loops
        while (leftIndex < leftSide.length && rightIndex < rightSide.length) {
            // eslint-disable-next-line security/detect-object-injection
            if (leftSide[leftIndex] < rightSide[rightIndex]) {
                // eslint-disable-next-line security/detect-object-injection
                array[globalIndex] = leftSide[leftIndex];
                leftIndex++;
            } else {
                // eslint-disable-next-line security/detect-object-injection
                array[globalIndex] = rightSide[rightIndex];
                rightIndex++;
            }
            globalIndex++;
        }
 
        // eslint-disable-next-line no-loops/no-loops
        while (leftIndex < leftSide.length) {
            // eslint-disable-next-line security/detect-object-injection
            array[globalIndex] = leftSide[leftIndex];
            leftIndex++;
            globalIndex++;
        }
 
        // eslint-disable-next-line no-loops/no-loops
        while (rightIndex < rightSide.length) {
            // eslint-disable-next-line security/detect-object-injection
            array[globalIndex] = rightSide[rightIndex];
            rightIndex++;
            globalIndex++;
        }
    }
 
    return array;
};
 
export const quick = <Element>(array: Element[]): Element[] => {
    if (array.length <= 1) {
        return array;
    } else {
        const pivot = array.pop() as Element;
        const smallerValues = array.filter((item) => item < pivot);
        const biggerValues = array.filter((item) => item > pivot);
 
        return [...quick(smallerValues), pivot, ...quick(biggerValues)];
    }
};