Home > Backend Development > C#.Net Tutorial > C# selection sort

C# selection sort

黄舟
Release: 2017-02-09 16:14:11
Original
1392 people have browsed it

C# Selection sorting

using System;  
  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace Sort  
{  
    class SelectSorter  
    {  
        public static int[] Sort(int[] a)  
        {  
            SelectSort(a);  
            return a;  
        }  
        private static void SelectSort(int[] myArray)  
        {  
            int i, j, smallest;  
            //数据起始位置,从0到倒数第二个数据  
  
       for (i = 0; i < myArray.Length - 1; i++)  
            {  
                smallest = i;//记录最小数据的下标  
                for (j = i + 1; j < myArray.Length; j++)  
                {  
                    //在剩下的数据中寻找最小数据  
  
            if (myArray[j] < myArray[smallest])  
                    {  
                        smallest = j;//如果有比它更小的,记录下标  
                    }  
                }  
        //将最小数据和未排序的第一个数据交换  
                Swap(ref myArray[i], ref myArray[smallest]);  
            }  
        }  
        private static void Swap(ref int left, ref int right)  
        {  
            int temp;  
            temp = left;  
            left = right;  
            right = temp;  
        }  
    }  
}
Copy after login

The idea of ​​selection sorting:

C# selection sort


Example:

C# selection sort

The above is the content of C# selection sorting. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template