Use sort() to bubbling sort:
var arr = [5,39,8,1,2,13,55];arr = arr.sort(function(a,b){return ab});console.log(arr);//1,2,5,8,13,39,55The third variable bubble sort is not declared:
The number of arrays in the first layer (how many times do you need to go through), and the second traversal (how many times do you need to loop)
a = 10; //The first element b = 5; //The next element if(a>b){ a = a+b; // a(15) = 10 +5; b = ab; // b(10) = 15 - 5; a = ab; // a(5) = 15 - 10;}var arr = [5,39,8,1,2,13,55];function jssort(ele){for (var i=0;i<ele.length;i++){ //How many times do you want to loop for (var j=0;j<ele.length-i-1;j++){ //How many times do you want to move if(ele[j]>ele[j+1]){ele[j]=ele[j]+ele[j+1]; //a = a+b ele[j+1]=ele[j]-ele[j+1];//b = a-bele[j]=ele[j]-ele[j+1]; //a = ab}}}return ele;}console.log(jssort(arr));//1,2,5,8,13,39,55I saw such a colon sorting online today
An array contains element numbers, "110 King Kong No. 3", "200 King Kong No. 1", "50 King Kong No. 2", "30 King Kong No. 6", "30 King Kong No. 5", "30 King Kong No. 4". How to sort the array according to the number of numbers in the string.
Answer: Use sort() sort to use regular expressions in callback functions
arr.sort(function(a,b){return parseInt(a.match(/(/d+)(?= number)/)[1])-parseInt(b.match(/(/d+)(?= number)/)[1])});The match() method can retrieve the specified value within a string, or find a match for one or more regular expressions.
This method is similar to indexOf() and lastIndexOf(), but it returns the specified value instead of the position of the string.
a.match(/(/d+)(?= sign)/ //Number + zero times or once matches the preceding character or subexpression.
Let's take a look at the example of js bubble sorting
Example, an example of js implementing bubble sorting.
<html><head><script type="text/javascript">function sort (arr) {for (var i = 0;i<arr.length;i++) {for (var j = 0; j < arr.length-i-1; j++) {if (arr[j]<arr[j+1]) {var temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}} return arr;}var arr=[2,5,3,1,7,8,78,89];sort(arr);</script></head><body></body></html>The above is the bubble sorting method in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!