Graphic Insertion sorting algorithm

Source: Internet
Author: User
The basic idea of Insertion sorting is that after I-1 processing, L [1 .. i-1] has arranged the order. I-repeat only inserts L [I] into the proper position of L [1 .. i-1] so that L [1 .. I] is a sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L [I] and L [i-1], if L [i-1] ≤ L [I], then L [1 .. i] The order has been sorted, and the I-th processing is over; otherwise, the L

The basic idea of Insertion sorting is that after I-1 processing, L [1 .. i-1] has arranged the order. I-repeat only inserts L [I] into the proper position of L [1 .. i-1] so that L [1 .. I] is a sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L [I] and L [i-1], if L [i-1] ≤ L [I], then L [1 .. i] The order has been sorted, the I-th processing is over; otherwise, the position of switching L [I] and L [i-1] continues to compare L [i-1] and L [i-2], until a position j (1 ≤ j ≤ i-1) is found so that L [j] ≤ L [j + 1. Demonstrate the Insertion sorting process for the four elements, which requires three inserts: (a), (B), and (c.

Figure 4 insert and sort elements

In the insert sorting algorithm below, we can introduce a sentinel element L [0], which is smaller than any of the records in L [1. n] for the convenience of programming. Therefore, the element type ElementType has a constant-∞, which is smaller than any possible record. If the constant-∞ is not determined in advance, you must check whether the current position is 1 before deciding whether L [I] is to move forward, if the current position is already 1, the I-th processing should be completed. Another way is to put L [I] into L [0] at the beginning of the I-th processing, so that I-th processing can be completed as appropriate.

The complexity of the algorithm Insertion_Sort is considered below. For the definite I, the number of while loops in the body is O (I), so the whole body of the loop executes the Sigma O (I) = O (Sigma I ), I ranges from 2 to n. That is, the number of comparisons is O (n2 ). If the input sequence is arranged from large to small, then the number of while loops in the body is i-1, so the entire loop body executes a Σ (i-1) = n (n-1)/2 times. From this we can see that, in the worst case, Insertion_Sort should compare Ω (n2) times.

If the element type is a large record, the algorithm consumes a lot of time for the first row. Therefore, it is necessary to analyze the number of moving elements. According to the analysis, on average, n (n-1)/4 times are executed for 5th rows. the analysis method is the same as that for bubble sorting.

If moving elements consume a lot of time, you can use a linked list to implement linear tables.

The insert sorting method is an in-situ replacement sorting method and also a stable sorting method. Although the insertion method is complex as θ (n2) in the worst case, the insertion sorting method is a fast in-situ replacement sorting method for small-scale input. Many complex sorting methods use insert sorting methods for sorting when the scale is small, such as fast sorting and bucket sorting.

The following is an example of Insertion sorting in C language:

# Include
 
  
# Include
  
   
Void PrintHeap (const char * strMsg, int array [], int nLength); void InsertionSort1 (int * items, int count) void InsertionSort2 (int a [], int size ); void PrintArray (const char * strMsg, int array [], int nLength); int main (int argc, char * argv []) {int data [13] = {8, 5,, 2}; InsertionSort1 (data, 13); PrintArray ("Insertion Sort:", data, 13); system ("PAUSE "); return 0;}/* insert the sorting idea: divide the array into two areas: sorted area and unsorted area Domain. First, assume that the first element of the array is in the sorted area, and all elements after the first element are in the unordered area. Two layers of loops are used for sorting. the first loop is used to retrieve the elements to be sorted from the unsorted area and gradually narrow down the unsorted area, the second-level loop is used to find the insert position from the sorted area (that is, the elements that are larger than the elements to be sorted are constantly searched from the sorted area, then, move the elements in a large sorted area behind. The final result of the move is that the last element of the sorted area occupies the original position of the elements to be sorted, the position is left blank in the middle of the sorting area), and the space left after the elements to be sorted are inserted and moved. */Void InsertionSort1 (int * items, int count) {int x, y; int c; for (x = 1; x
   
    
= 0) & (c
    
     
= I for (I = 1; I
     
      
V) {a [j] = a [j-1]; j --; if (j <= 0) break;} // stopped when a [j-1] <= v, put v at position a [j] = v ;}} void PrintArray (const char * strMsg, int array [], int nLength) {int I; printf ("% s ", strMsg); for (I = 0; I
      
       

This article is available at http://www.nowamagic.net/librarys/veda/detail.

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.