js algorithm, sort the array

Bubble Sort. Press the number to the array sequentially ordered from small to large arr = [1, 6, 7, 8, 9, 5, 18];

// outermost loop pointer i pointing from left to right, when the cycle of the innermost circle of the pointer, the pointer points to the next position before i

// pointer j innermost point from right to left, not beyond the position of the pointer i, and regardless of the value of the size of the current point and the next value, the size press exchange position. As shown below

 

       let arr = [1, 6, 7, 8, 9, 5, 18];
        for (i = 0; i < arr.length - 1; i++) {
            for (j = arr.length - 1; j > i; j--) {
                if (arr[j - 1] > arr[j]) {
                    [Arr [j - 1], arr [j]] = [arr [j], arr [j - 1]];
                }
            }
        }
        console.log(arr);// [1, 5, 6, 7, 8, 9, 18]

Guess you like

Origin www.cnblogs.com/yt0817/p/11965957.html