Direct insertion sorting algorithm (JavaScript version)

Source: Internet
Author: User

Divides the sequence of n elements into two parts that are ordered and unordered.

Sequence: {A1,a2,a3,a4,...,an}

The first element of the sequence is treated as an ordered sequence, followed by an unordered sequence:

{{A1},{a2,a3,a4,...,an}}

The elements in the unordered series are inserted into the corresponding positions of the ordered series, and the corresponding positions in the ordered series are found by the way of the size before inserting.

Code:

The following code executes through in the NODEJS environment.

functionsort (elements) {//Suppose that the No. 0 element is an ordered sequence, and the 1th one is an unordered series,  //so, starting with the 1th element, the elements of the unordered sequence are inserted into the ordered sequence.   for(vari = 1; i < elements.length; i++){    //Ascending    if(Elements[i] < elements[i-1]){      //Take the first I in the unordered sequence as the inserted element      varGuard =Elements[i]; //remember the last position of the ordered series and expand the position of the ordered series      varj = I-1; Elements[i]=Elements[j]; //than the size of the inserted element where it is found       while(J >= 0 && guard <Elements[j]) {Elements[j+1] =Elements[j]; J--; }      //InsertELEMENTS[J+1] =Guard; }  }}varelements = [10, 9, 8, 7, 6, 5];console.log (' Before: ' +elements); sort (elements); Console.log (' After: ' + elements);

Efficiency:

Best: N

Worst: O (n^2)

If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. The best case is that the sequence is already in ascending order, in which case the comparison operation needs to be done (n-1). The worst case scenario is that the sequence is sorted in descending order, then there is a total of n (n-1)/2 times to be performed at this point. The assignment operation to insert a sort is the number of times the comparison operation is added (n-1). On average, the time complexity of inserting the sorting algorithm is O (n^2). Thus, the insertion sort is not suitable for applications with a large amount of data for sorting. However, if the amount of data that needs to be sorted is small, for example, if the magnitude is less than thousand, then inserting the sort is a good choice.

Direct insertion sorting algorithm (JavaScript version)

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.