Inserting a sorted JavaScript implementation

Source: Internet
Author: User
Tags array sort

Thought

Each time a new array item is placed on the basis of an existing array that is already lined up.

    • Think of the first item as already lined up, and the second one before or after the first? Now that the two items are lined up, should the third be before or after the two that have been lined up? Now that the three items are lined up, what is the fourth item in this row of three??... And so on
    • When a new item is queued into an array item that is already lined up, compare the items that have been lined up from the back to the new one, and if they are larger than the new one, move backward in one place ... Until you find the position of an array item that is less than or equal to the new item, place the new item behind that position (because the following array item is moved back one position, so there is just one empty position).
Code
function insertionSort(arr) {  const len = arr.length;  for (let i = 1; i < len; i++) { //在arr[0,...,i-1]中插入arr[i]    const toInsertValue = arr[i];    let j;    for (j = i; j >0 && arr[j-1] > toInsertValue; j--) { //找到一个比arr[i]大的项,就把这个项往后挪一项。因为最后一项就是toInsertValue,所以该值一直可以通过toInsertValue访问,故也不必另做保存。      arr[j] = arr[j-1];    }    arr[j] = toInsertValue;//内循环结束得到的arr[j-1]是第一个比arr[i]小的值,那么就把arr[i]存储在此处的arr[j]上。而之前的arr[j]已经在上一轮循环中存储到了arr[j+1]中      }}
Performance analysis
    • Time complexity: Best O (N), average, worst O (n^2) (but small array sort, with better performance than bubbling and sorting)
    • Space complexity: O (1), stable
Extension: Comparing the insertion sort of C speech
#include<stdio.h>#include<stdlib.h>void insertion(int *list,int n){    int i,j,t;    for(i=1;i<n;i++)//待插入的是list[1]到list[n-1]    {        if(list[i]<list[i-1])        {            t=list[i];            list[i]=list[i-1];            j=i;            while(t<list[j-1]&&j>=1)            {                list[j]=list[j-1];                j--;            }            list[j]=t;          }        }}main(){    int list[10],n=10,i;    printf("请输入10个整数:\n");    for(i=0;i<10;i++)    {        scanf("%d",&list[i]);    }    insertion(list,10);    for(i=0;i<10;i++)    {        printf("%d\t",list[i]);    }    system("pause");}

Inserting a sorted JavaScript implementation

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.