C#选择排序算法实现

来源:互联网 发布:hadoop2.9.0 windows 编辑:程序博客网 时间:2024/06/01 08:45

 using System;
using System.Collections.Generic;
using System.Text;

namespace SortAlgorithms
{
public  class SelectionSorter
    {
        private int min;
     

        public void Sort(int[] arr)
        {
            for (int i = 0; i < arr.Length-1 ; i++)
            {
                min = i;
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (j - 1 > 0)
                    {
                        if (arr[j - 1] < arr[min])
                        {
                            min = j - 1;
                        }
                        int t = arr[min];
                        arr[min] = arr[i];
                        arr[i] = t;
                    }
                }
            }
        }
        //static void Main(string[] args)
        //{

        //    int[] arry = new int[] { 1,2,54,3,65};

        //    SelectionSorter s = new SelectionSorter();
        //    s.Sort(arry);
        //    foreach (int m in arry)
        //    {
        //        Console.WriteLine("{0}",m);
        //    }
          
        //    Console.ReadLine();
        //}
    }
}