Bubble sort refers to a sort of computer that has a time complexity of O (n^2), although it is less than the heap-sorted, fast-sorted O (Nlogn, base 2), but has two advantages: 1. " Programming complexity "is very low, it is easy to write code; 2. Stability, where the stability of the same elements in the original sequence is still maintained to the order of the sequence, and heap sorting, fast sorting are not stable. However, all the way, two merge sort, unbalanced binary tree sorting speed is faster than bubble sort, and has stability, but the speed is less than the heap sorting, fast sorting. The bubble sort is done by n-1, and the number of the I-trip is sorted from 1th to N-i, and if the number of I is larger than the latter (then ascending, small in descending order), exchange two numbers.
First, the simplest of all, is the bubbling sort of the individual. I don't have much to say.
/** @name Bubble Sort
* @lastmodify 2010/07/13
* @desc Comparison sort
Degree of Complexity O (n*n)
*/
function Bubblesort (list) {
var len = list.length;
var cl,temp;
while (len--) {
CL = list.length;
while (cl--) {
if (List[cl]>list[len] && cl < len) {
temp = List[len];
List[len] = List[cl];
LIST[CL] = temp;
}
}
}
return list;
}