Practice brain JavaScript Write direct insert sort and bubble sort

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.