JavaScript Bubble Sorting Algorithm

Source: Internet
Author: User
Due to its simplicity and ease of understanding, Bubble Sorting is often the first sorting algorithm that people think of first. The basic idea is to compare two numbers at a time and make sure they have a correct order before moving to another project. At the end of each level, there is a valuable "sorting" to the correct position, and only other projects are finally sorted. Because of its simplicity and ease of understanding, via Bubble Sorting is often the first sorting algorithm that people think of first. The basic idea is to compare two numbers at a time and make sure they have a correct order before moving to another project. At the end of each level, there is a valuable "sorting" to the correct position, and only other projects are finally sorted. From: http://caibaojian.com/javascript-bubble-sort.html

Algorithm Implementation ideas

Compare the first and second items

If the first item should be behind the second item, exchange them

Comparison between item 2 and item 3

If the second item should be after the third item, exchange them

Continue until data ends

This process repeats several times until the data is fully sorted. In each loop, because the last item is correctly sorted, fewer and fewer items are sorted. For better understanding, let's compare an array: [3, 2, 4, 5, 1].

Comparison process

The first is positive sorting. Compare the first and second items. Because 2 is smaller than 3, 3 is ranked later. The result is [2, 3, 4, 5, 1].

The second and third items have the correct order and do not need to be exchanged. The third and fourth items are also correct and do not need to be exchanged. The fourth and fifth items are exchanged and the result is [2, 3, 4, 1, 5].

Recycle the first and second items, switch to the third and fourth items in sequence, for [2, 3, 4, 5]

In the third cycle, the second and third exchanges are [2, 1, 3, 4, 5].

In the fourth cycle, the first and second switches to [1, 2, 3, 4, 5].

The first step to Implement Bubble Sorting is to create a method to exchange the two items in the array. This method is common in many inefficient sorting methods. A simple javascript implementation code is:

function swap(items, firstIndex, secondIndex){    var temp = items[firstIndex];    items[firstIndex] = items[secondIndex];    items[secondIndex] = temp;}

As mentioned above, the efficiency of this sort algorithm is relatively low because it needs to be sorted multiple times. Assume that an array has n items, then the Npower of 2 is required to calculate, let's take a look at the original article from: http://caibaojian.com/javascript-bubble-sort.html

Positive bubble Algorithm

function bubbleSort(items){    var len = items.length,        i, j, stop;    for (i=0; i < len; i++){        for (j=0, stop=len-i; j < stop; j++){            if (items[j] > items[j+1]){                swap(items, j, j+1);            }        }    }    return items;}

The cycle outside via controls the number of cycles, and the cycle inside controls the sorting and comparison between items.

Reverse Bubble Sorting

function bubbleSort(items){    var len = items.length,        i, j;    for (i=len-1; i >= 0; i--){        for (j=len-i; j >= 0; j--){            if (items[j] < items[j-1]){                swap(items, j, j-1);            }        }    }    return items;}

The results of the above two codes are the same, all of which are sorted from small to large, but the order of the loop is slightly different, all of which are positive and bubble.

Reverse Bubble Sorting

In fact, it is to determine the size change. When the first item is smaller than the second item, the switching position, and so on.

Function bubbleSort2 (items) {var len = items. length, I, j, stop; for (I = 0; I
 
  

Summary

Again, via explains that Bubble Sorting may not be suitable for your actual work. It is just a simple tool to help us understand algorithms and lay the foundation for further knowledge acquisition. What we use most is the built-in Array. prototype. sort () prototype method, which is more efficient.



For more articles about the JavaScript Bubble Sorting Algorithm, refer to PHP!

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.