All files / algorithms/sort/src bubble.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 11/11

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  5x 1x     1x 8x   8x   64x   35x 35x       8x 1x       1x    
// @src https://github.com/kutyel/typescript-algorithms/blob/master/src/bubbleSort/index.ts
export const sortBubble = <Element>(array: Element[]): Element[] => {
    array = [...array];
 
    // eslint-disable-next-line no-constant-condition
    while (true) {
        let swapped = false;
 
        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;
};