zoukankan      html  css  js  c++  java
  • 排序算法之选择排序

    选择排序定义:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。

    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new[] { 234, 632, 23, 643, 2, 6, -2, 423, 2342,43 };
            Console.WriteLine("排序前:");
            Console.WriteLine(string.Join(",", array));
    
            SelectSort(array);
    
            Console.WriteLine("排序后:");
            Console.WriteLine(string.Join(",", array));
            Console.ReadKey();
        }
        
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="sources">目标数组</param>
        private static void SelectSort(int[] sources)
        {
            for (int i = 0, len = sources.Length - 1; i <= len; i++)
            {
                // 假设最小值索引
                int minIndex = i;
    
                // 循环遍历一遍找到最小值的索引
                for (int j = i + 1; j <= len; j++)
                {
                    // 如果最小值比其他元素大,重新设置最小值的索引
                    if (sources[minIndex] > sources[j])
                    {
                        minIndex = j;
                    }
                }
    
                // 临时变量交换最小值的位置;
                int temp = sources[i];
                sources[i] = sources[minIndex];
                sources[minIndex] = temp;
            }
        }
    }
  • 相关阅读:
    请求参数的中文乱码问题
    MySql索引与优化
    Android 兼容包
    Mysql 主从(转)
    解决tomcat一闪而过(转)
    log4j
    支付相关
    通过maven添加quartz
    linux命令学习之:chmod
    Nginx特点及其配置
  • 原文地址:https://www.cnblogs.com/GodX/p/4063570.html
Copyright © 2011-2022 走看看