博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 选择排序
阅读量:4594 次
发布时间:2019-06-09

本文共 1204 字,大约阅读时间需要 4 分钟。

class Program    {        static void Main(string[] args)        {            int[] arr = new int[] { 6, 9, 13, 2, 4, 64 };            int[] newArr = SelectionSort(arr);            Console.Read();        }        public static int[] SelectionSort(int[] a)        {            int item; //中间临时变量,用于交换元素位置            for (int i = 0; i < a.Length; i++)            {                int minIndex = i; //记录最小元素的下标                for (int j = i+1; j < a.Length; j++)                {                    if (a[minIndex]>a[j])                    {                        minIndex = j;                    }                }                if (a[minIndex]!=a[i])                {                    item = a[i];                    a[i] = a[minIndex];                    a[minIndex] = item;                }                StringBuilder sb = new StringBuilder();                sb.Append($"第{i+1}次排序后:");                foreach (var num in a)                {                    sb.Append($"{num},");                }                Console.WriteLine(sb.ToString());            }            return a;        }    }

 备注:选择排序的复杂度,用大O表示法为:O(n*n)

 效率上:   快速排序 > 选择排序 > 冒泡排序

转载于:https://www.cnblogs.com/25miao/p/10662471.html

你可能感兴趣的文章
VMWare12pro安装Centos 6.9教程
查看>>
Spark笔记之使用UDF(User Define Function)
查看>>
找规律 UVALive 6506 Padovan Sequence
查看>>
区间DP UVA 10453 Make Palindrome
查看>>
JavaScript系列教程(七):函数
查看>>
江中微型统计分析软件V1.0版本完成
查看>>
彻底搞懂CNN中的卷积和反卷积
查看>>
iOS中画各种图形
查看>>
javascript中的面向对象
查看>>
VirtualBox 下 CentOS6.5 网络配置(NAT) (公司内部用)
查看>>
XGBOOST/GBDT,RandomForest/Bagging的比较
查看>>
python迭代器
查看>>
世界大部分的变化变革是一直有人在问为什么不能做得更好。
查看>>
关于wav文件fft处理后x,y轴坐标数据的问题
查看>>
HDU 2546 0-1背包问题
查看>>
mysql show processlist 显示mysql查询进程
查看>>
centos系统安装后的基本配置
查看>>
遍历集合的Iterator删除其中的元素
查看>>
Linux
查看>>
hihocoder 1407 重复旋律2
查看>>