其他分享
首页 > 其他分享> > 简单选择排序

简单选择排序

作者:互联网

 class Program
    {
        static void SelectSort(int[] dataArray) {
            for (int i = 0; i < dataArray.Length-1; i++)
            {
                int min = dataArray[i];
                int minIndex = i;//最小值的索引
                for (int j = i+1; j < dataArray.Length; j++)
                {
                    if (dataArray[j]<min)
                    {
                        min = dataArray[j];
                        minIndex = j;
                    }
                }
                if (minIndex!=i)//如果最小值不是i  交换
                {
                    int temp = dataArray[i];
                    dataArray[i] = dataArray[minIndex];
                    dataArray[minIndex] = temp;
                }
            }


        }

        static void Main(string[] args)
        {
            int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
            SelectSort(data);
            foreach (var item in data)
            {
                Console.Write(item + "  ");
            }
            Console.ReadKey();
        }
    }

标签:minIndex,SelectSort,int,选择,++,Length,简单,排序,dataArray
来源: https://blog.csdn.net/qq258456qq/article/details/100098596