All files / algorithms/sort/src bubble.ts

100% Statements 15/15
100% Branches 4/4
100% Functions 1/1
100% Lines 14/14

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  5x 5x   5x 1x   1x 8x 8x 64x 35x 35x     8x 1x     1x   5x  
// @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++) {
            if (array[i] > array[i + 1]) {
                [array[i], array[i + 1]] = [array[i + 1], array[i]];
                swapped = true;
            }
        }
 
        if (!swapped) {
            break;
        }
    }
 
    return array;
};