Note: Time complexity: The time required to complete a program;
Space complexity: The amount of memory required to complete a program;
1. Bubble Sort: 22 comparison, if the previous one is larger than the latter, then the interchange position, after each cycle comparison, the last one is always the largest, the next round of comparison, it will not participate.
Eg:function Sort (Array) {
for (var i = 0; i < array.length-1; i++) {
for (var j = 0; j<array.length-i-1; j + +) {//The last one does not participate in sorting
if (Array[j] > array[j+1]) {
var SMAP = array[j]; Assign a large value to a variable
ARRAY[J] = array[j+1]; Move the small value forward
ARRAY[J+1] = SMAP; Move the big value back
}
}
}
}
Compare 1 + 2 + 3 + ... + (array.length-1) comparisons altogether.
Knowledge System second time review supplement--Bubbling algorithm