选择排序
来源:互联网 发布:mac返回桌面 编辑:程序博客网 时间:2024/05/16 14:02
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//选择排序说明//原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。//算法过程如下://找到列表中的最小值,//把它和第一个位置的元素交换,//列表其余部分重复上面的步骤(从第二个位置开始,且每次加1).//列表被有效地分为两个部分:从左到右的有序部分,和余下待排序部分。namespace _07选择排序{ class Program { static void Main(string[] args) { List<int> arrayToSort = new List<int>() { 12, 4654, 789, 1, 456, 174, 6, 54, 9 }; int min; for (int i = 0; i < arrayToSort.Count; i++) { //假设最小值的索引为i min = i; //for循环找到剩余数的最小值索引 for (int j = i + 1; j < arrayToSort.Count; j++) { if (((IComparable)arrayToSort[j]).CompareTo(arrayToSort[min]) < 0) { //替换最小值索引 min = j; } } int temp = arrayToSort[i]; arrayToSort[i] = arrayToSort[min]; arrayToSort[min] = temp; } #region 遍历输出数组 for (int i = 0; i < arrayToSort.Count; i++) { Console.Write(arrayToSort[i].ToString() + ","); } Console.ReadKey(); #endregion } //上面的实现基于该算法实现 public IList SelectionSort(IList arrayToSort) { int min; for (int i = 0; i < arrayToSort.Count; i++) { min = i; for (int j = i + 1; j < arrayToSort.Count; j++) { if (((IComparable)arrayToSort[j]).CompareTo(arrayToSort[min]) < 0) { min = j; } } object temp = arrayToSort[i]; arrayToSort[i] = arrayToSort[min]; arrayToSort[min] = temp; //RedrawItem(i); //RedrawItem(min); //pnlSamples.Refresh(); //if (chkCreateAnimation.Checked) // SavePicture(); } return arrayToSort; } }}
学习来源:http://blog.jobbole.com/72850/
0 0
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序:
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 重温HBaes部署与java客户端访问
- ORA-00918 column ambiguously defined
- 自我简绍
- C++中生成任意范围内随机数的方法
- java中自带的对于自定义的类的排序
- 选择排序
- Lua 与C/C++ 交互系列: Lua调用C/C++函数(3)
- C#中序列化的应用
- ubuntu arm软件开发环境配置
- leetcode--Add and Search Word - Data structure design
- C# 闭包
- LeetCode Course Schedule II
- 安装Ubuntu
- mysql测试工具详细介绍