This sorting is a pairwise comparison.
Take this array and sort it in descending order var a = [3, 1, 5, 6, 4, 2];
First round of comparison: compare the first value with other elements of this array
3 to 1
3 to 5 //5 is larger, so the exchange results in a = [5, 1, 3, 6, 4, 2];
5 to 6 //The exchange results in a = [6 , 1, 3, 5, 4, 2];
6 to 4
6 to 2
The final result of the first round a = [6, 1, 3, 5, 4, 2];
Second round of comparison: Compare the second value with the elements after this value
1 to 3 //Exchange to get a = [6, 3, 1, 5, 4, 2];
3 to 5 //Exchange a = [6, 5, 1, 3, 4, 2];
5 to 4
5 to 2
The final result of the second round is a = [6, 5, 1, 3, 4, 2];
Exchange in this way
The final result of the third round a = [6, 5, 4, 1, 3, 2];
The final result of the fourth round a = [ 6, 5, 4, 3, 1, 2];
The final result of the fifth round a = [6, 5, 4, 3, 2, 1];
The following is the reconstruction method: