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:
- In the range of subscript 0-4, the smallest number in the range is referred to subscript 0
- In the range of subscript 1-4, the smallest number in the range is referred to subscript 1
- In the range of subscript 2-4, the smallest number in the range is referred to subscript 2
- In the range of subscript 3-4, the smallest number in the range is referred to subscript 3
- 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:
- Compare position I and position i+1, if larger than i+1, then swap
- Compare position I and position i+2, if larger than i+2, then swap
- ......
- 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:
- In the range of subscript 0-4, the maximum number in the range is sunk to position 4
- In the range of subscript 0-3, the maximum number in the range is sunk to position 3
- In the range of subscript 0-2, the maximum number in the range is sunk to position 2
- In the range of subscript 0-1, the maximum number in the range is sunk to position 1
- 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:
- Compare position 0 and position 1, if the former is larger than the latter, then swap
- Compare position 1 and position 2, if the former is larger than the latter, then swap
- ......
- 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:
- 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
- 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 #