C + +-insert Sort algorithm

Source: Internet
Author: User

Index:

Open Source Spring Solution--lm.solution

See Code GitHub:

Sort.cpp

Code Brief Analysis Description:

1. For(int i=1;i<nsize;i++)

This outer for loop, [0][1],[1][2],[2][3] ... This order lets the inner layer follow the bad start index decrement comparison, guarantee each pair start

The inner layers are sorted before the bad sort.

2. While(Tmp<ary[n]) ... n--...

This inner layer while loop, so that the outer layers of the beginning of each loop, if the size of the comparison after the true, then transposition, n--to ensure that the n+1 is the index corresponding

Values can always be in the correct position

3. Operating Environment

Visual C + + IDE environment, it is recommended to actually run and debug a bit, the results are as follows:

   

4. Code:

1#include <iostream>2 3 using namespacestd;4 5 //defining functions--inserting sorting algorithms6 voidSort (int* ary,intnSize)7 {8     /*9 * Cyclic elements, dynamic understanding:Ten * arr[1] and arr[0] comparison One * arr[2] and arr[1],arr[0] comparison A * arr[3] and arr[2],arr[1],arr[0] comparison - * and so on and so on -     */ the      for(intI=1; i<nsize;i++) -     { -         inttmp=Ary[i]; -         intn=i-1; +  -         //swap position If the latter element is less than the previous element +          while(tmp<Ary[n]) A         { atary[n+1]=Ary[n]; -  -             //element is dynamic decrement -n--; -  -             //after the first element, the element is not in             if(n==-1) -             { to                  Break; +             } -         } the  *ary[n+1]=tmp; $     }Panax Notoginseng } -  the intMainintargcChar*argv[]) + { A     //arrays that need to be sorted the     intnumarray[]={ A, to,5, the,1, About}; +     intNlength=sizeof(Numarray)/sizeof(int); -  $     //Sort $ Sort (numarray,nlength); -  -     //show the sorted results the      for(intk=0; k<nlength;k++) -     {Wuyicout<<numarray[k]<<","; the     } -cout<<Endl; Wu  -     //Exit About     return 0; $}

Mask

2018-06-12 21:50 Tuesday

C + +-insert Sort algorithm

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.