JS array bubble sort


Bubble sort: Roulette, pairwise comparison, small front


1. Number of rounds: length – 1 times in total

2. Number of comparisons in each round: as the number of rounds increases, the number of comparisons decreases


Double loop represents the whole sorting process

1. Outer loop: control the number of rounds of comparison, from 1 to length-1

2. Inner loop: control the number of comparisons in each round, and also indicate the subscript of the elements participating in the comparison, starting from 0 to length – 1 – I (round number variable)


Bubble sort data within a set of data

function mao(){
                var arr = [1,99,58,6,3,7,6,102,66];
                console.log (before sorting: + ARR);
                //Outer circulation
                for(var i=1;i<=arr.length-1;i++){
                    //Inner circulation
                    for(var j=0;j<=arr.length-1-i;j++){
                        //Compare the size relationship between the current number arr [J] and the next number arr [J + 1]. If arr [J] is greater than arr [J + 1], the positions are exchanged
                            arr[j] ^= arr[j+1];
                            arr[j+1] ^= arr[j];
                            arr[j] ^= arr[j+1];
                console.log (after sorting) + ARR);

Results of operation: