# Insert sorting by JavaScript sorting algorithm

Source: Internet
Author: User
Insert sort is to take the first element from the unordered table every time and insert it into the proper position of the ordered table so that the ordered table is still ordered. The first step is to compare the first two numbers, and then insert the second number into an ordered table by size. The second step is to scan the third data and the first two numbers from the front to the back, insert the third number to the ordered table by size. perform the following operations in sequence and perform the (n-1) round scan.

Insert sort (straight insertion sort) is to take the first element from the unordered table every time and insert it into the proper position of the ordered table so that the ordered table is still ordered.

The first step is to compare the first two numbers, and then insert the second number into an ordered table by size. The second step is to scan the third data and the first two numbers from the front to the back, insert the third number to the ordered table by size. perform the following operations in sequence. after (n-1) scanning, the entire sorting process is completed.

Directly inserted sorting is a stable sorting. The time complexity is o (n ^ 2), and the space complexity is O (1 ).

Direct insertion sorting is composed of two nested loops. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. Direct insertion and sorting compare the value to be compared with the previous value, so the outer loop starts from the second value. When the current value is greater than the value to be compared, continue the loop comparison until a value smaller than the value to be compared is found and the value to be compared is placed in the next position to end the loop.

It is worth noting that we must use a bucket to save the value to be compared, because when the comparison is complete, we need to place the last digit of the value to be smaller than the value to sort the cards like playing cards. The basic way to insert sorting is: each step inserts a record to be sorted into the appropriate position in the previously sorted sequence according to the size of its keyword until all records are inserted.

Related Keywords:

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.