C + + implementation of the direct insertion algorithm

Source: Internet
Author: User

Direct insertion algorithm: Each trip inserts a keyword to be sorted into the appropriate position of the ordered sequence of parts by its value, until all the keywords to be sorted are inserted into the ordered sequence.

In theory, the second loop in the direct insert sort can end prematurely, where an element does not iterate through the front of the sequence when it looks for its proper position.

This is the maximum difference between the direct insertion sort and the simple selection sort. It is also straightforward to insert sort and simple to sort the same as time complexity O (N2), but the direct insertion sort is more efficient.

This advantage is especially noticeable when the data to be sorted is basically ordered. Even at this point, it is more efficient to have a direct-insert sort than the time-complexity O (NLOGN) sorting algorithm.

#include <iostream>#include<string>using namespacestd;template<typename t>voidInsertselectionsort (T arr[],intN) {    //The No. 0 element is not considered, because the No. 0 element itself is ordered in the initial case of the insertion sort     for(intI=1; i<n;i++){        //Find Arr[i] The right insertion position//Each comparison is a comparison of the current element and the previous element of the current element, so the condition is j>0 rather than j>=0         for(intJ=i;j>0&&arr[j-1]>arr[j];j--) Swap (Arr[j],arr[j-1]); }}intMain () {inta[Ten]={Ten,9,8,7,6,5,4,3,2,1}; Insertselectionsort (A,Ten);  for(intI=0;i<Ten; i++) cout<<a[i]<<" "; cout<<Endl; floatb[3]={3.3f,2.2f,1.1f}; Insertselectionsort (b,3);  for(intj=0;j<3; j + +) cout<<b[j]<<" "; cout<<Endl; stringc[4]={"D","C","B","A"}; Insertselectionsort (c,4);  for(intk=0;k<4; k++) cout<<c[k]<<" "; cout<<Endl; return 0;}

Output Result:

However, if we perform the algorithm performance test, we will find that the above code does not show the advantages of this high efficiency.

The reason is that there is a lot of numeric exchange in this code, and each value Exchange includes three assignments, and in this case, the time to access the index of the array, which is more expensive than simple.

So we can optimize the key code above.

Template <typename t>voidInsertselectionsort (T arr[],intN) {     for(intI=1; i<n;i++){        //Copy the keyword you want to sort, and compare it to the element in front of it .T e=Arr[i]; //I need to get the definition of J out of the For loop, because the last thing I want to do is insert the copied keyword in the position specified by index J (the target location after the comparison) e        intJ;  for(j=i;j>0&&arr[j-1]>e;j--)            //move the larger keyword than the keyword you want to sortarr[j]=arr[j-1]; ARR[J]=e; }}

Here we no longer call the swap function for the exchange of values, but all use the assignment statement to complete the corresponding operation.

The algorithm is greatly optimized.

It is important to note that for a direct insert sort, a trip to a sort does not ensure that a keyword reaches its final position.

C + + implementation of the direct insertion algorithm

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.