排序算法-简单选择排序

实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _007_简单选择排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
            SimpleSelectSort(data);
            for (int i = 0; i < data.Length; i++)
            {
                Console.Write(data[i] + " ");
            }
        }
        static void SimpleSelectSort(int[] dataArray)
        {
            for (int i = 0; i < dataArray.Length-1; i++)
            {
                int minIndex = i; //最小值索引
                for (int j = i+1; j < dataArray.Length; j++)
                {
                    if (dataArray[j] < dataArray[minIndex])//找到最小的数值
                        minIndex = j;
                }
                if (minIndex != i)//然后交换元素
                {
                    int temp = dataArray[i];
                    dataArray[i] = dataArray[minIndex];
                    dataArray[minIndex] = temp;
                }
            }
        }
    }
}

 

posted @ 2017-12-27 15:38  RONGWEIJUN  阅读(173)  评论(0编辑  收藏  举报