Array ordering for C #

Source: Internet
Author: User

In my knowledge, C # has three kinds of common sorting methods, different, I have a brief summary of them, as follows:

1. Exchange Sort

Suppose there is an array of nums, with a length of 5, to sort it in ascending order, the general idea of an exchange sort is:

    1. In the range of subscript 0-4, the smallest number in the range is referred to subscript 0
    2. In the range of subscript 1-4, the smallest number in the range is referred to subscript 1
    3. In the range of subscript 2-4, the smallest number in the range is referred to subscript 2
    4. In the range of subscript 3-4, the smallest number in the range is referred to subscript 3
    5. Sort done!

Written code should be:

 for (int04; i++) {    // within the i-4 range, the smallest number in the range is referred to I}

regardless of the length of the nums, you can use this code format :

 for (int01; i++) {    // in I-(nums. LENGTH-1) within the range, refer to the smallest number in the range I}

So how to put I-(nums. LENGTH-1) The smallest number in the range refers to position I?

In this issue, the interchange sort is done in the following ways:

    1. Compare position I and position i+1, if larger than i+1, then swap
    2. Compare position I and position i+2, if larger than i+2, then swap
    3. ......
    4. Position I and position nums. Length-1 to compare if compared to nums. Length-1 large, then Exchange

Therefore, the code is as follows:

 for (int01; i++) {// in I-(nums. LENGTH-1) within the range, the smallest number within the range is referred to I for (int1; j < Nums. Length; J + +) {if (Nums[i] > nums[j]) {// interchange int temp =  == temp; }}}

2. Bubble sort

There are two ways to bubble sort:

    • Sink the largest number to the bottom
    • Take the smallest number to the top

Here is the idea of sinking the largest number to the bottom.

Assuming that there is an array of nums, with a length of 5, to sort it in ascending order, the overall idea of a bubbling sort is:

    1. In the range of subscript 0-4, the maximum number in the range is sunk to position 4
    2. In the range of subscript 0-3, the maximum number in the range is sunk to position 3
    3. In the range of subscript 0-2, the maximum number in the range is sunk to position 2
    4. In the range of subscript 0-1, the maximum number in the range is sunk to position 1
    5. Sort done!

Written code should be:

 for (int40; i--) {    // within the 0-i range, sink the largest number in the range to I}

regardless of the length of the nums, you can use this code format :

 for (int10; i--) {    // within the 0-i range, sink the largest number in the range to I}

So how do you sink the largest number in the 0-i range to position I?

On this issue, the bubbling sort is done in the following ways:

    1. Compare position 0 and position 1, if the former is larger than the latter, then swap
    2. Compare position 1 and position 2, if the former is larger than the latter, then swap
    3. ......
    4. The position i-1 and position I are compared, if the former is larger than the latter, then the Exchange

Therefore, the code is as follows:

 for  (int  i = nums. Length-1 ; i > 0 ; I--) { //  at 0 -I sinks the largest number in the range to the I  for  (int  j = Span style= "COLOR: #800080" >0 ; J < I; J++ if  (Nums[j] > Nums[j+1  ") { //  interchange  int  temp = Nums[j]; NUMS[J]  = Nums[j+1  ];nums[j  +1 ] = temp;}}  

3. Select Sort

The general idea of selecting sort and the general idea of the exchange sort is that the minimum number in a range refers to the first bit in the range, and that its code structure is identical to the Exchange sort:

 for (int01; i++) {    // in I-(nums. LENGTH-1) within the range, refer to the smallest number in the range I}

Knowledge has a difference in the implementation of the annotation section, the idea of choosing a sort is:

    1. First find I-(nums. LENGTH-1) The subscript of the minimum number within the range, assuming that the found subscript is saved to the variable index
    2. Then exchange the values of nums[i] and Nums[index]

Therefore, the code that uses the Select Sort Implementation comment section is as follows:

 for(inti =0; I < Nums. Length-1; i++){//in the I-(nums. LENGTH-1) within the range, the smallest number in the range is referred to I//1. First find I-(nums. LENGTH-1) The lowest number within the range is the subscriptintindex = i;//first assume that the subscript of the minimum number is I for(intj = i +1; J < Nums. Length; J + +){if(Nums[j] <Nums[index]) {//found a smaller numberindex = j;//Record the subscript}}//2. Then swap the values of nums[i] and Nums[index]inttemp =Nums[i];nums[i]=Nums[index];nums[index]=temp;}

Array ordering for 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.