一天一个小算法的学习之选择排序

选择排序:

其思想就是,将每个数据元素,分成有序数列和无序数列..在每次排序中找出最小的那一个数据元素并放到有序数列后面,一直到排序结束.. 

using System;

namespace SortPra
{
    public class SelectSort
    {
        public SelectSort ()
        {
            
        }

        public static int[] SelectWithSort (int[]arr)
        {
            SelectSortTo (arr);
            for (int i = 0; i < arr.Length; i++) {
                Console.WriteLine ("选择排序" + arr [i]);
            }
            return arr;
        }
        //选择排序
        static void SelectSortTo (int[]arr)
        {
            int i, j, small;
            //记录起始位置,从0到倒数第二个数据
            for (i = 0; i < arr.Length; i++) {
                //记录最小的数据
                small = i;
                for (j = i + 1; j < arr.Length; j++) {
                    if (arr [j] < arr [small]) {
                        //有比他小的数就进行交换.
                        small = j;
                    }
                }
                //数据交换
                SelectSwap (ref arr [i], ref arr [small]);
            }
        }
        //进行交换的方法
        static void SelectSwap (ref int left, ref int small)
        {
            int temp;
            temp = left;
            left = small;
            small = temp;
        }

    }
}
原文地址:https://www.cnblogs.com/jbw752746541/p/8710039.html