Classical 4-lecture C ++ sorting: insert sorting (1)

Source: Internet
Author: User

We all knowC ++ sortingThere are four common methods: insert sorting, Hill sorting, exchange sorting, and select sorting. This article introducesInsert sort. Before introducing insertion, introduce the test programs in our entire series of articles.

Test procedure

The subsequent routines are sorting arrays. Static linked lists are also applicable to the sorting of linked lists. For the sake of simplicity, only the single key code is sorted, and the final results are sorted from start to end in ascending order. The following is a unified test procedure:

 
 
  1. # Include  
  2. # Include  
  3. Using NamespaceStd;
  4. # Include  
  5. # Include  
  6. # Include  
  7. # Include "InsertSort. h" 
  8. # Define random (num) (rand () % (num )) 
  9. # Define randomize () srand (unsigned) time (NULL )) 
  10. # Define N 10000 // Number of sorting Elements 
  11. # Define SORT InsertSort // sorting method 
  12. ClassTimer// Unit: ms 
  13. {
  14. Public:
  15. VoidStart () {start_t = clock ();}
  16. Clock_tTime (){Return(Clock ()-start_t );}
  17. Private:
  18. Clock_tStart_t;
  19. };
  20. IntKCN, RMN; timer TIMER;
  21. VoidTest (IntA [])
  22. {
  23. TIMER. start ();
  24. SORT <Int> (A, N, KCN, RMN );
  25. Cout <"\ TTimeSpared :"<TIMER. time () <"Ms"<Endl;
  26. Cout <"KCN ="<Left <setw (11) <KCN;
  27. Cout <"KCN/N ="<Left <setw (11) <(Double) KCN/N;
  28. Cout <"KCN/N ^ 2 ="<Left <setw (11) <(Double) KCN/N;
  29. Cout <"KCN/NlogN ="<Left <setw (11) <(Double) KCN/N/log ((Double) N) * log (2.0) <endl;
  30. Cout <"RMN ="<Left <setw (11) <RMN;
  31. Cout <"RMN/N ="<Left <setw (11) <(Double) RMN/N;
  32. Cout <"RMN/N ^ 2 ="<Left <setw (11) <(Double) RMN/N;
  33. Cout <"RMN/NlogN ="<Left <setw (11) <(Double) RMN/N/log ((Double) N) * log (2.0) <endl;
  34. }
  35. IntMain ()
  36. {
  37. IntI;
  38. // Randomize (); this sentence is not added to compare different sorting algorithms under the same circumstances. 
  39. Int* Ascending =New Int[N];// Ascending sequence 
  40. Int* Descending =New Int[N];// Descending sequence 
  41. Int* Randomness =New Int[N];// Random sequence 
  42. For(I = 0; I <N; I ++) {ascending [I] = I; randomness [I] = I; descending [I] = N-I-1 ;}
  43. For(I = 0; I <N; I ++) swap (randomness [I], randomness [random (N)]);
  44. Cout <"Sort ascending N ="<N; test (ascending );
  45. Cout <"Sort randomness N ="<N; test (randomness );
  46. Cout <"Sort descending N ="<N; test (descending );
  47. Return0;
  48. }

Note that KCN and RMN are not required by the algorithm, this is to provide an intuitive evaluation of the algorithm's performance (without the calculation of those formulas ). Sorting 10000 integers should be the most efficient test method. We recommend that you do not increase the number of records. First, you do not have to wait too long in the worst case, the second is to avoid overflow of KCN and RMN. In addition, some recursive algorithms may overflow when the number of records is too large, resulting in program crash.


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.