JavaScript for bubbling sorting

Source: Internet
Author: User

About the algorithm, should not be given to JavaScript to do, because the understanding of algorithms and data structures, and not because of their familiar language to achieve common data structures and algorithms to get much improvement, but this process is a few benefits, for some non-trained people.

Let's start by explaining the bubble sort mechanism: traverse the sequence to sort, compare two adjacent elements, and swap them out if their order is inconsistent with what we want. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The procedure of the bubbling sort has a fractional ascent or a large number sinking of two kinds, here refers only to the realization of the large number sinking.

The outer loop acts by extracting the largest number in the currently unsorted array and placing it on the left side of the sorted data. That is, our first outer loop is to swap the position of the largest number to the far right of the array, and the second outer loop is to swap the second large number to the right of the array, and so on. And the length of an array, we only need to propose length-1 a large number, then the first of the array must be the smallest number that has not been extracted, then the condition of the outer for loop to determine the range of the value I can be understood.

The function of the inner loop is to achieve the large number of sinks we want to process. Each comparison is adjacent to two data, so an array of length, we only need to do length-1 next to the comparison, we can achieve a large number of sinks, and the previous cycle has been precipitated by the large number does not need to be sorted, so the inner loop condition to determine the range of J is easy to understand.

Next we want to do data exchange, we want to do is a large number of precipitation, that is, when the next two data, the left side is larger than the right, swap position, here are three ways to provide.

Let's send a piece of code about bubble sort.

1 functionBubblesort (arr) {2      for(vari = 0; i < arr.length-1; i++) {3          for(varj = 0; J < Arr.length-1-i; J + +) {4             if(Arr[j] > arr[j+1]) {5                 /*set a third-party variable to use for data exchange, and this variable is better on the outside of the loop6 var temp = arr[j];7 Arr[j] = arr[j+1];8 arr[j+1] = temp;9                 */Ten  One              //using addition to realize the exchange of two data A                 //Arr[j] = Arr[j] + arr[j+1]; -                 //arr[j+1] = arr[j]-arr[j+1]; -                 //Arr[j] = arr[j]-arr[j+1]; the  -              //using bitwise arithmetic to realize the exchange of two data -ARR[J] = arr[j]^arr[j+1]; -ARR[J+1] = arr[j]^arr[j+1]; +ARR[J] = arr[j]^arr[j+1];  -             } +         }; A     }; at     returnarr; -}

As we can see, the first method needs to open up new memory space, so this third-party variable is a global variable when the performance is good, this method is the most used method, and the most easy to understand.

The second method is to use the addition to achieve two of data exchange, it is not difficult to understand, and addition can do, subtraction can certainly do, after all, subtraction in a sense, is also an addition.

The third method uses bit arithmetic, bitwise XOR or XOR, represented by the symbol (^), which is the direct operation of the binary form of the data in memory. Here, a bitwise XOR feature is used to exchange data: One data bitwise XOR or another number two times equals itself. It is more efficient than the two methods above, but using JavaScript to efficiently manipulate the data itself is a joke.

At this point our bubble is over, if there are errors and omissions in the place also please correct me.

JavaScript for bubbling sorting

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.