function insertionsort(array) {if(Object.prototype.toString.call (array). Slice (8,-1) = = = ' array ') {//equivalent to (array instanceof array) to determine whether an array varLen =Array.Length; if(Len <= 1) {//array with only 0 or one element without sorting returnArray; } for(vari = 1; i < Len; i++) { varInsertionelement = Array[i];//Take a new element to insert as a sub-array for(varj = 0; J < I; J + +) {//Sub-array from 0 to I-1 if(Array[j] < insertionelement)//element is greater than, does not move Continue; Else if(Array[j] = = insertionelement) {//element equals, does not swap for(varmove = i; Move > J; move--) {//The sub-array moves backwards 1 bits (if there is a linked list can be directly linked, JS no linked list)Array[move] = array[move-1];//Note: The array moves to the order of attention } Break; } Else{//Exchange when element is less than for(varmove = i; Move > J; move--) {//Sub-array moves backwards 1 bitsArray[move] = array[move-1]; } Array[j]=insertionelement; Break; } } } returnArray; } Else{//not an array return' Not an array! '; } } function bubblesort(array) {if(Object.prototype.toString.call (array). Slice (8,-1) = = = ' array ') {//equivalent to (array instanceof array) to determine whether an array varLen =Array.Length; if(Len <= 1) {//array with only 0 or one element without sorting returnArray; } for(vari = 0; i < len-1; i++) { for(varj = i + 1; J < Len; J + +) { if(Array[j] < array[i]) {//the smallest value floats vartemp =Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; } } } returnArray; } Else{//not an array return' Not an array! '; }} console.log (Insertionsort (["A", "CD", "ee", "Hello", "This", "Hey", "e", "Go", "what", "Yo", "foo"])); Console.log (Bubblesort (["A", "CD", "ee", "Hello", "This", "Hey", "e", "Go", "what", "Yo", "foo"]);
Practice brain JavaScript Write direct insert sort and bubble sort