Using JS to implement the insertion sort

Source: Internet
Author: User

Words don't say much, directly on the code

HTML Source:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

  

JS Source code

//JavaScript DocumentvarMyarr=[];$(function(){                  //Click the Sort button to trigger the event$ ("#click"). Click (function() {insert_mine (); }) Document.onkeydown=function(event) {vare=event| | window.event| | Arguments.callee.caller.arguments[0]; if(e&&e.keycode==13&&$ ("#content"). Is (": Focus") ) {insert_mine (); }                                         }           })varInsert_mine=function(){    varcontent=$ ("#content"); if(!content.val ()) {Alert ("Please enter what you want to sort!"} ");return false;}; varArr=content.val (). Split (",");  for(vari=0;i<arr.length;i++)    {        if(IsNaN (Arr[i])) {Alert ("The entry has an illegal number, the program has exited!") ");return false;} Myarr=arr_solve (Myarr,arr[i]); } alert (The sort ends. "); $("#conclude"). Val (myarr.tostring ()); Myarr=[];}varArr_solve=function(arr1,i) {Debugger; if(arr1.length==0) {arr1[0]=i; }        Else        {             for(vark=0;k<arr1.length;k++)            {            if(parseint (Arr1[k]) >=parseint (i) &&k<arr1.length) {//The previous index doesn't move, it's the index behind it .            //arr[k]=i;                         for(varj=arr1.length;j>k;j--) {Arr1[j]=arr1.slice (0) [j-1]; } Arr1[k]=i;                    Console.log (Arr1.tostring ()); returnarr1; }                    Else                    {                        if(arr1.length==k+1) {Arr1[arr1.length]=i;                        Console.log (Arr1.tostring ()); returnarr1;                        }                                                                        }                                }    } console.log (Arr1.tostring ()); returnarr1; }        

Input rule: Enter a comma-separated number in the input box

Output rule: Sort results in ascending order from small to large

Operation Result:

It is important to note that the exchange of elements in an array cannot be directly assigned to the slice method using the array's own.

Note: Running this program requires importing the jquery library

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.