zoukankan      html  css  js  c++  java
  • 15,选择排序

    1,假设第一项值最小

    2,每轮找出最小值,和对应下标

    3,如果最小值比第一项目小交换位置

    C#代码实现:

    using System;
    
    namespace 数据结构
    {
        public class SelectSort
        {
            static int[] sortArray = { 2, 501, 403, 708, 900, 1 };
            public static void Main(string[] args)
            {
                Console.WriteLine("排序之前");
                foreach (var m in sortArray)
                {
                    Console.Write(m+" ");
                }
                Select(sortArray);
                Console.WriteLine();
                Console.WriteLine("排序之后");
                foreach (var m in sortArray)
                {
                    Console.Write(m+" ");
                }
            }
    
            //选择排序,从小到大
            public static void Select(int[] array)
            {
                for (int i = 0; i < array.Length - 1; i++)
                {
                    //最小值下标
                    int minIndex = i;
                    //最小值
                    int min = array[minIndex];
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (min > array[j])
                        {
                            minIndex = j;
                            min = array[j];
                        }
                    }
                    //交换
                    if (min < array[i])
                    {
                        array[minIndex] = array[i];
                        array[i] = min;
                    }
                }
            }
        }
    }
    

     

  • 相关阅读:
    python工具类 md5
    python 线程池, 进程池
    scrapydweb 安装部署
    python 协程
    jquery
    scrapyd 设置访问密码
    pat 乙级1033 旧键盘打字(20)
    1459 迷宫游戏(51NOD)
    python之禅
    Jzzhu and Cities ----CodeForces
  • 原文地址:https://www.cnblogs.com/xiaojvhuang/p/13360121.html
Copyright © 2011-2022 走看看