The common sort algorithm--direct insertion sort

Source: Internet
Author: User

has always been from the blog garden to absorb nutrition, I would like to have the opportunity to write some useful articles to Bo friends, to give back to everyone's selfless dedication. Bloggers are looking for a job recently, listen to the small partners that the data structure and algorithm is a must test. So the pain of the heart decided to write a sorting algorithm. For self-examination, at the same time with the small partners one another. Welcome to reprint, if there are errors, please correct me. will be grateful!

Learn to communicate qq:792911374, gossip do not say, the beginning of the text .....

Direct Insert Sort
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 an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered. If you encounter an equal to the insertion element, the insertion element places the element that needs to be inserted behind an equal element. The order of the equal elements is not changed, so the direct insert sort is a stable sorting algorithm.

The algorithm is suitable for ordering small amounts of data. The time complexity is O (n^2) and the space complexity is O (1).

The algorithm is C language code, the environment is QT.

1#include <QCoreApplication>2 3 //number of array elements4 Const intMAX =Ten;5 //number of character arrays6 Const intCHARNUM = -;7 //Information Structure8 structrecordinfo{9    intkey;Ten    Charname[ -]; One }; A //temporary variables for array elements - structRecordInfo Info[max]; -  the voidgenlist (); - voidInsertsort (); - voidPrintArray (); -  + intMainintargcChar*argv[]) - { +     //function Call A Insertsort (); at PrintArray (); - qcoreapplication A (argc, argv); -     returna.exec (); - } -  - /** in * @ Function Name: - * @ parameter: none to * @ return value: None + * @ Purpose: Generate content array - * @yangfy the   */ * voidgenlist () $ {Panax Notoginseng      for(inti =0; i < MAX; i++){ -         if((i%3==0) && i >0) theInfo[i].key = Info[i-3].key; +         Else AInfo[i].key =rand (); thesnprintf (Info[i].name, CHARNUM-1,"content is:%d", i); +     } - } $  $ /** - * @ Function Name: - * @ parameter: none the * @ return value: None - * Use: Direct insert sortWuyi * @yangfy the   */ - voidInsertsort () Wu { - genlist (); About     structRecordInfo Recinfo; $      for(inti =1; i < MAX; i++){ -          for(intj = i; J >0; j--){ -             //in an ordered array list, find the swap position that is larger than J. -             if(Info[j].key < Info[j-1].key) { ARecinfo =Info[j]; +INFO[J] = info[j-1]; theInfo[j-1] =Recinfo; -             } $         } the     } the } the  the /** - * @ Function Name: in * @ parameter: none the * @ return value: None the * @ Purpose: Print array contents About * @yangfy the   */ the voidPrintArray () the { +      for(inti =0; i < MAX; i++){ -printf"key is:%d, name:%s\r\n", Info[i].key, info[i].name); the     }Bayi}

Program:

The common sort algorithm--direct insertion sort

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.