Internal sorting-directly insert sorting

Source: Internet
Author: User

A sorting middleware has been developed recently, so we need to study sorting.AlgorithmAnd focuses on implementation.

The algorithm language is C language, and the Environment is vs2010 integrated environment.

Data type to be record:
 //  Data  # Define Maxsize 20 Typedef  Int  Keytype; typedef  Int  Infotype; typedef  Struct  {Keytype key;  // Keyword Infotype otherinfo; //  Other data items } Redtype; //  Record type Typedef Struct  {Redtype R [maxsize + 1 ]; //  R [0] idle or used as a sentry Unit      Int Length; //  Sequence table length } Sqlist;//  Sequence Table type 

 

 

Simplest insert sorting

Sort by insert directly:Insert a record to an ordered table that has already been sorted to obtain a new ordered table with the number of records increasing by 1..

The theory of direct insertion sorting is relatively simple, and the algorithm is also relatively simple.

 //  Insert sort  Void Insertsort (sqlist & L ){  //  Directly insert and sort the sequence table L      For ( Int I = 2 ; I <= L. length; I ++ ){  If (LT (L. R [I]. Key, L. R [I- 1  ]. Key) {L. R [  0 ] = L. R [I]; L. R [I] = L. R [I- 1  ];}  Int J = I- 2  ;  For (; LT (L. R [ 0 ]. Key, L. R [J]. Key); j -- ) {L. R [J + 1 ] = L. R [J];} l. R [J + 1 ] = L. R [ 0  ] ;}} 

Function LT (INT, INT) is the content of comparison. Here is an example of integer size comparison based on data types.

BoolLT (IntX,IntY ){If(X <Y)Return True;ElseReturn False;}
Test

In order to be tested, the following is a complete testCode

Insert Sorting Algorithm instance

 // Testinsersort. cpp: defines console applications.Program.  //  # Include  "  Stdafx. h  "  # Include  "  Stdio. h  "  //  Data  # Define Maxsize 20 Typedef  Int  Keytype; typedef Int  Infotype; typedef  Struct  {Keytype key;  //  Keyword Infotype otherinfo; //  Other data items } Redtype; //  Record type Typedef Struct  {Redtype R [maxsize + 1 ]; // R [0] idle or used as a sentry Unit      Int Length; //  Sequence table length } Sqlist; //  Sequence Table type  Bool LT ( Int X, Int  Y ){  If (X < Y)  Return   True  ; Else          Return   False  ;}  //  Insert sort  Void Insertsort (sqlist & L ){  //  Directly insert and sort the sequence table L      For ( Int I = 2 ; I <= L. length; I ++ ){  If (LT (L. R [I]. Key, L. R [I-1  ]. Key) {L. R [  0 ] = L. R [I]; L. R [I] = L. R [I- 1  ];}  Int J = I- 2  ;  For (; LT (L. R [ 0 ]. Key, L. R [J]. Key); j -- ) {L. R [J + 1 ] =L. R [J];} l. R [J + 1 ] = L. R [ 0  ] ;}}  Int _ Tmain ( Int Argc, _ tchar * Argv []) {sqlist L; scanf (  "  % D  " ,& L. Length );  For ( Int I = 1 ; I <= L. length; I ++) {Scanf (  "  % D  " ,& L. R [I]. Key);} insertsort (L );  For ( Int I = 1 ; I <= L. length; I ++ ) {Printf (  "  % D \ t  "  , L. R [I]. Key );}  Return   0 ;} 
Efficiency of insert sorting

Space:S (1)Insert and sort directly requires only one auxiliary space.

Time: the basic job of sorting, 1. Compare the size of the keyword; 2. Move the record

Best case: when the record is already ordered and the sequence is the desired order, the second for loop will not be run, and the overall space complexity is O (n)

Worst case: when the record is the opposite of the sequence to be obtained, the second for loop is executed most frequently, compared to (2, 3 ,..., n) times, that is, (n + 2) (n-1)/2. The number of moves is (2, 3 ,..., n, N + 1), that is, (n + 4) (n-1)/2.

The average number of times is about N2/4, that is, the time complexity isO (n2)

 

 

References[1] Yan Weimin Wu Weimin data structure (C Language) Beijing: Tsinghua University Press, 1997.4

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.