JS sorting method (sort, bubble, select, insert) code summary, bubbleinsert
Recently, I started to learn data structures.
I hope to stick to it.
Because the direction is the frontend, it is implemented using JavaScript.
// Sort var testArr1 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr2 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr3 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; testArr1.sort (); // sorting result: [15, 19, 2, 26, 27, 3, 36, 38, 4, 44, 46, 47, 48, 5, 50] testArr2.sort (function (a, B) {return a> B}); // sorting result: [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50] testArr3.sort (function (a, B) {return a-B}); // sorting result: [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
// Sort buckets
Var testArr1 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; var testArr2 = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function bubbleSort1 (array) {for (I = array. length-1; I> 0; I --) {for (j = 0; j <I; j ++) {if (array [j + 1] <array [j]) {var temp = array [j + 1]; array [j + 1] = array [j]; array [j] = temp ;}}} function bubbleSort2 (array) {for (I = array. length-1; I> 0; I --) {for (j = 0; j <I; j ++) {if (array [j + 1]-array [j]) <0) {var temp = array [j + 1]; array [j + 1] = array [j]; array [j] = temp ;}}} bubbleSort1 (testArr1); // sorting result: [2, 3, 4, 5, 15, 19,, 50] bubbleSort2 (testArr2); // sorting result: [, 50]
// Select sorting
Var testArr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function selectSort (array) {for (I = 0; I <array. length; I ++) {var slc = array [I]; // set the first unordered value to the selected value var tmpidx at the beginning; // record the index for (j = I; j <array. length; j ++) {if (array [j] <slc) {slc = array [j]; tmpidx = j ;}} if (slc! = Test [I]) {// if the last selected value is not the same as the initial slc value var temp = array [I]; array [I] = array [tmpidx]; array [Maid] = temp ;}} selectSort (testArr); // The sorting result is: [, 50]
// Insert sorting
Var testArr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; function insertSort (array) {for (var I = 0; I <array. length-1; I ++) {// note that I is smaller than the length of the array-1; otherwise, the array may be out of bounds, forming an endless loop var curElement = array [I + 1]; for (var j = I; j> = 0; j --) {if (curElement <array [j]) {array [j + 1] = array [j]; if (j = 0) {// when j = 0, it indicates that array [0] = curElement;} has been ranked at the beginning of the array ;}} else {array [j + 1] = curElement; break ;};};} insertSort (testArr); // The sorting result is: [2, 3, 4, 5, 15, 19, 26, 27,, 50]
Articles you may be interested in:
- Common js sorting implementation code
- Js object arrays are sorted by Attributes quickly
- Js sorting animation simulation-insert sorting
- Notes for sorting arrays (sort) in js
- Use sort and localeCompare in JS to implement Chinese sorting instance
- Js exchange sorting Bubble Sorting Algorithm (Javascript Version)
- Js uses Array. prototype. sort () to sort Array objects