var arr = [1,4,2,9,7,6,5,4,7,5]; //Bubble Sort (popularly speaking is J and J+1 Dozen, who wins who goes back)Forvar i = 1;i<arr.length; i++) {for (var j = 0;j<arr.length-i; j + +) {if (arr[j]>arr[j+1]) {var temp = arr[j]; ARR[J] = arr[j+1]; ARR[J+1] = temp; }}} console.log (arr);
//Select sort (popularly speaking is J and I dozen, who wins who goes ahead)Forvar i = 0;i<arr.length-1; i++) {for (var j = i+1;j<arr.length; j + +) {if (Arr[i]>arr[j]) {var temp = arr[j]; ARR[J] = Arr[i]; Arr[i] = temp; }}} console.log (arr);
//FastQuick Sort:Take the value of the middle of the array, and then compare the other values with the value, greater than the join right array, less than the left array, recursive call function Quick (arr) {//recursive export conditionsif (arr.length<=1) {return arr;}//Get intermediate subscriptvar centerindex = Arr.length%2==0?arr.length/2: (arr.length+1)/2;//Get the value corresponding to the intermediate subscriptvar centerelement = Arr[centerindex];//Create left and right arraysvar left = []; var right = [];//cyclic judgment, if the element is greater than the middle of the table corresponding to the value of adding the left array, and vice versa to join the right arrayfor (var i = 0;i<arr.length;i++) {if ((I!=centerindex) && (arr[i]>=centerelement)) {Right.push (arr[i]); } if ((I!=centerindex) && (arr[i]<centerelement)) {Left.push (arr[i]); } }//Recursive call to this functionreturn Quick (left). Concat (centerelement). Concat (quick);} console.log (Quick (arr));
JS in bubble sort, select sort, quick sort