Basic sorting Algorithm C #

Source: Internet
Author: User

The algorithm knowledge lacks, learns several basic sorts algorithm, 1. Select Sort 2. bubble sort 3. Insert Sort

In ascending order, for example, the two values in the program are more than the latter, then the position is swapped.

1. Choose the sort algorithm that is most easily thought of for sorting, but don't know this is called the Select Sort method

1         /// <summary>2         ///Select Sort3         /// </summary>4         /// <param name= "sourcevalues" ></param>5         /// <returns></returns>6          Public Static int[] Selectionsortmin2max (int[] sourcevalues)7         {8             int[] ret =sourcevalues;9 Ten             if(Sourcevalues = =NULL) One                 returnret; A  -             //The 1th one followed the 2,3,4 ... comparison, the resulting smaller value is placed in the 1th result 1th is the minimum value -             //The 2nd one, in turn, with the 3,4 ... comparison, the lower value of the comparison is placed in the 2nd result 2nd is the second small value ... the             inttemp; -              for(inti =0; I < Ret. Length-1; i++) -             { -                  for(intj = i +1; J < Ret. Length; J + +) +                 { -                     if(Ret[j] <Ret[i]) +                     { Atemp =Ret[i]; atRet[i] =Ret[j]; -RET[J] =temp; -                     } -                 } -             } -  in             returnret; -}

2. Bubble sorting algorithm adjacent to two value comparisons (1 and 2,2 and 3 ... N-1 and N) result nth is the maximum value

Adjacent two value comparisons (1 and 2,2 and 3 ... N-2 and N-1) results N-1 is the second largest value ...

1 /// <summary>2         ///Bubble Sort3         /// </summary>4         /// <param name= "sourcevalues" ></param>5         /// <returns></returns>6          Public Static int[] Bubblesortmin2max (int[] sourcevalues)7         {8             int[] ret =sourcevalues;9 Ten             if(Sourcevalues = =NULL) One                 returnret; A  -             //adjacent comparison -             //inner complete loop for the first time (1th vs. 2nd, 2nd vs. 3rd ...) N-1 and Nth comparisons), bubbling result ret[ret.length-1] is the maximum value the             //Inner Complete Loop second (1th vs. 2nd comparison, 2nd vs. 3rd ...) N-2 and N-1 comparisons), bubbling result ret[ret.length-2] is a secondary large value -             // ...  -             inttemp; -              for(inti =0; I < Ret. length-1; i++) +             { -                  for(intj =0; J < Ret. Length-i-1; J + +) +                 { A                     if(Sourcevalues[j] > sourcevalues[j +1]) at                     { -temp =Sourcevalues[j]; -SOURCEVALUES[J] = sourcevalues[j +1]; -Sourcevalues[j +1] =temp; -                     } -                 } in             } -  to             returnret; +}

3. Insert Sort

1         /// <summary>2         ///Insert Sort Method3         /// </summary>4         /// <param name= "Sourcevalue" ></param>5         /// <returns></returns>6          Public Static int[] Insertionsort (int[] sourcevalue)7         {8             int[] ret =Sourcevalue;9             if(Sourcevalue = =NULL)Ten                 returnret; One  A             //idea: Assuming that the order of the first n values is correct, the first n+1 value is added, then the n+1 value is bubbling forward (contrasting position) until the appropriate position is found -             //1th value is bubbling forward, then No. 0, 1 values are sequentially -             //after the 2nd value is bubbling forward, the first 0,1,2 value is sequentially the             // ... When the nth value is bubbling forward, the first 0,1...N value is sequentially -             inttemp; -              for(inti =1; I < Ret. Length; i++) -             { +                  for(intj = i; J >=1; j--) -                 { +                     if(Ret[j] >= ret[j-1]) A                     { at                          Break; -                     } -temp =Ret[j]; -RET[J] = ret[j-1]; -Ret[j-1] =temp; -                 } in             } -             returnret; to}

Basic sorting Algorithm C #

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.