Share and optimize the Bubble Sorting Code implemented by javascript, and bubble the javascript

Source: Internet
Author: User

Share and optimize the Bubble Sorting Code implemented by javascript, and bubble the javascript

Bubble Sorting: sorts the elements in an array in the ascending or ascending order.

var array=[9,8,7,6,5,4,3,2,1];

The first round of comparison: 8, 7, 6, 5, 4, 3, 2, 1, 9 exchanged 8 times I = 0 j = array. length-1-i

The second round of comparison: 7, 6, 5, 4, 3, 2, 9 exchanged 7 I = 1 j = array. length-1-i

Comparison of the third round: 6, 5, 4, 3, 2, 1, 7, 8, 9 exchanged 6 times I = 2 j = array. length-1-i

Comparison of the fourth round: 5, 4, 3, 2, 1, 6, 7, 8, 9 exchanged 5 times I = 3 j = array. length-1-i

Comparison of the Fifth Round: 4, 3, 2, 1, 5, 6, 7, 8, 9 exchanged 4 times I = 4 j = array. length-1-i

Comparison of the sixth round: 3, 2, 5, 6, 7, 8, 9 exchanged 3 times I = 5 j = array. length-1-i

The seventh round of comparison: 2, 1, 3, 4, 5, 6, 7, 8, 9 exchanged 2 I = 6 j = array. length-1-i

The eighth round of comparison: 1, 2, 3, 4, 5, 6, 7, 8, 9 exchanged 1 I = 7 j = array. length-1-i

Code implementation:

Var temp; var array = [9, 8, 7, 6, 5, 4, 3, 2, 1]; // Number of External Loop Control wheels for (var I = 0; I <array. length-1; I ++) {// Number of Inner Loop Control comparisons for (var j = 0; j <array. length-1-i; j ++) {if (array [j]> array [j + 1]) {// exchange two variables temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp ;}} console. log (array );

Code optimization:

Var temp, bool, m = 0; var array = [9, 8, 7, 6, 5, 4, 3, 2, 1]; for (var I = 0; I <array. length-1; I ++) {// switch bool = true in the open/closed principle; for (var j = 0; j <array. length-1-i; j ++) {if (array [j]> array [j + 1]) {// exchange two variables temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp; bool = false; // close the switch} // if the if in the inner loop is not executed (the switch is disabled, execute the following statement); if (bool) {break;} m ++ ;} console. log (array + ", compare" + m + "Wheel ");

Note: The best condition for comparing the number of rounds is 0 and the worst case is 8.

Let's look at a Bubble Sorting Algorithm.

// Javascript Bubble Sorting is directly added to the prototype of the basic type. // here, the code in the essence of the javascript language is used to add methods for the basic type prototype. // because of Array, string itself is also a constructor, they create objects through the new constructor line, so Array. prototype, String. prototype all points to Function. prototype // Array. when the method is used, the first access to the Array function object does not have the method, and then the Array. prototype still does not exist, and Function. prototype found Function. prototype. method = function (name, func) {if (! This. prototype [name]) {// You are advised to first determine whether this method is available in the prototype. If this method is not added. prototype [name] = func;} return this ;}; Array. method ('bucket', function () {// The bubble algorithm loops a total of the length of the array, that is, len times. Each time the smallest value is placed, var len = this. length; var I = 0, j = 0, tmp = 0; for (I = 0; I <len; I ++) {for (j = 0; (j + 1) <len-I; j ++) {console. log () if (this [j]> this [j + 1]) {tmp = this [j]; this [j] = this [j + 1]; this [j + 1] = tmp ;}}; return this ;}); alert ([,]. bubble ());

After reading the code of another front-end engineer, the code of the west wind thin horse, in the for loop at the first layer, is added to initialize an exchange flag, which is false. When an exchange occurs, the value becomes true, after the second-layer for loop ends, add a judgment. If the value is false, that is, there is no exchange between the previous and subsequent comparisons. If the order of size is correct, the break can jump out of the outer for loop.

// Array var list = Array (23, 45, 18, 37, 92, 13, 24); // Array length var n = list. length; // The temporary variable var tmp for the exchange Order; // The exchange flag var exchange; // a maximum of N-1 sort for (var time = 0; time <n-1; time ++) {exchange = false; for (var I = n-1; I> time; I --) {if (list [I] <list [I-1]) {exchange = true; tmp = list [I-1]; list [I-1] = list [I]; list [I] = tmp ;}// if this sort is not exchanged, terminate the algorithm in advance if (! Exchange) {break ;}} alert ('after the array is sorted, It is '+ list +', and n is ranked '+ time +' ');

I have also added a netizen's algorithm to my favorites, which is quite good. Let's take a look.

Function BubbleSort (array) {var length = array. length; var temp; var isSort = false; for (var I = 1; I <length; I ++) {isSort = false; for (var j = 0; j <length-I; j ++) {if (array [j]> array [j + 1]) {// exchange temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp; isSort = true ;}} if (! IsSort) break; // exit the loop if no exchange occurs} var array = [10,-, 34,-34,5,]; BubbleSort (array ); for (var I = 0; I <array. length; I ++) {document. write (array [I] + "");}

Now, let's summarize this for you today. I hope it will be helpful for you to learn JavaScript Bubble 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.