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