Javascript Bubble sorting summary and javascript bubble Summary

Source: Internet
Author: User

Javascript Bubble sorting summary and javascript bubble Summary

Bubble Sorting example, two-way Bubble sorting and slightly improved visualization of two-way Bubble sorting.

The code is very simple. I don't know if there are any unknown bugs.
Please do not speak out

Bubble sort example

var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ]; for(var i=0;i<ls.length;i++){      for(var j=i+1;j<ls.length;j++){        if(ls[i]>ls[j]){          ls[i]=ls[i]+ls[j];          ls[j]=ls[i]-ls[j];          ls[i]=ls[i]-ls[j];        }       }    }

Bidirectional Bubble Sorting example

Var ls = [, 44, 24,48, 34,12, 88,80, 33,8, 80,45, 64,52, 79,77]; for (var I = 0; I <ls. length; I ++) {for (var j = I + 1; j <ls. length-I; j ++) {if (ls [lent-1-i] <ls [lent-j]) {ls [lent-1-i] = ls [lent-1-i] + ls [lent-j]; ls [lent-j] = ls [lent-1-i]-ls [lent-j]; ls [lent-1-i] = ls [lent-1-i]-ls [lent-j];} // compare if (ls [I]> ls [j]) {ls [I] = ls [I] + ls [j]; ls [j] = ls [I]-ls [j]; ls [I] = ls [I]-ls [j];} // The comparison above }}

Improved bidirectional bubble sorting

Var ls =, 24,48, 34,12, 88,80, 33,8, 80,45, 64,52, 79,77]; var lent = ls. length; for (var I = 0; I <ls. length; I ++) {for (var j = I * 2; j <ls. length-2 * I; j ++) {if (ls [I * 2]> ls [j + 1]) {ls [I * 2] = ls [I * 2] + ls [j + 1]; ls [j + 1] = ls [I * 2]-ls [j + 1]; ls [I * 2] = ls [I * 2]-ls [j + 1];} // keep the first number of inner layers as the minimum if (ls [lent-I * 2-1] <ls [lent-j-1]) {ls [lent-I * 2-1] = ls [lent-I * 2-1] + ls [lent-j-1]; ls [lent-j-1] = ls [lent-I * 2-1]-ls [lent-j-1]; ls [lent-I * 2-1] = ls [lent-I * 2-1]-ls [lent-j-1];} /// keep the first count of the inner layer as the maximum number of loops if (ls [lent-2-i * 2] <ls [lent-j-1]) {ls [lent-2-i * 2] = ls [lent-2-i * 2] + ls [lent-j-1]; ls [lent-j-1] = ls [lent-2-i * 2]-ls [lent-j-1]; ls [lent-2-i * 2] = ls [lent-2-i * 2]-ls [lent-j-1];} // The last if (ls [I * 2 + 1]> ls [j + 1]) {ls [I * 2 + 1] = ls [I * 2 + 1] + ls [j + 1]; ls [j + 1] = ls [I * 2 + 1]-ls [j + 1]; ls [I * 2 + 1] = ls [I * 2 + 1]-ls [j + 1];} // next }}

Articles you may be interested in:
  • Javascript bubble sort code for positive and inverted sorting
  • Js sorting animation simulates Bubble Sorting
  • Example of array Bubble Sorting in javascript
  • Js exchange sorting Bubble Sorting Algorithm (Javascript Version)
  • Detailed description of the Javascript Bubble Sorting Algorithm
  • JS implements Bubble sorting, insert sorting, fast sorting, and sorting output
  • How js implements array Bubble sorting and fast sorting

Related Article

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.