Bubble sort principle and realization method

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.