Insert sort-Direct insert sort (straight insertion sort)

Source: Internet
Author: User

Basic idea:

Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered to be an ordered subsequence, and then inserted from the 2nd record one after the other into the entry until the entire sequence is ordered.

Important: Set up Sentinel as a temporary storage and judgment array boundary.

Example of a direct insert sort:

If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

Direct Insert sort (straight insertion sort)is to remove the first element from the unordered table and insert it into the proper position of the ordered table so that the ordered table remains orderly. The first two numbers are compared, then the second number is inserted into the ordered table by size, and the second pass scans the third data with the first two numbers, inserts the third number into the ordered table by size, and then goes through the n-1 scan to complete the sorting process. A direct insert sort consists of a two-layer nested loop. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. A direct insert sort compares the value to be compared to its previous value, so the outer loop starts with the second value. The current value continues to loop compared with a value larger than what is to be compared until it is found to be smaller than the value to compare and the value to be compared is placed in a subsequent position, ending the cycle.
Additional records introduced in the function algorithm of Sentinel R[0] are called Sentinel or Sentinel. Sentinel has two functions: ① (insert position) loop before it saves a copy of R[i] so that it does not lose the contents of R[i] due to the post-movement of the record; ② its main function is to "monitor" the subscript variable J in the lookup loop. Once out of bounds (i.e. j=0), because r[0]. Can compare with oneself, the loop judgment condition is not established makes the search loop end, thus avoids in this cycle each time must detect whether J crosses the boundary (namely omitted the cyclic determination condition "j>=1"). Note: ① in fact, all additional nodes (elements) that are introduced to simplify boundary conditions can be called Sentinels. "Example" the head node in the single-linked list is actually a Sentinel ② after the introduction of Sentinel, so that the test to find the cycle conditions of about half of the time, so for the record number of files save time is considerable. For an algorithm that is similar to sorting in such a way that it can be used very frequently, reduce its run time as much as possible. Therefore, we should not consider the Sentinels in the above algorithm as tricks, but should understand and grasp this technique deeply.

  

The implementation of the algorithm:

#include<iostream> using namespace std; int main() {      int a[]={ 98 , 76 , 109 , 34 , 67 , 190 , 80 , 12 , 14 , 89 , 1 };      int k=sizeof(a)/sizeof(a[ 0 ]);      int j;      for ( int i= 1 ;i<k;i++) //循环从第2个元素开始      {          if (a[i]<a[i- 1 ])          {              intMove =a[i];              for (j=i- 1 ;j>= 0 && a[j]>move;j--)//a[j]若小于要挪动的数,则是循环终止的条件              {                  a[j+ 1 ]=a[j];//将a[i]前元素向后挪动一个              }              a[j+ 1 ]=move; //此处就是a[j+1]=move;          }      }      for ( int f= 0 ;f<k;f++)      {          cout<<a[f]<< "  " ;      }      return 0 ; }

Efficiency:

Time complexity: O (n^2).

The other insert sort has two-insert sort, 2-way insert sort.

Insert sort-Direct insert sort (straight insertion sort)

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.