数据结构与算法(选择性插入排序)
2015-10-17 12:40
323 查看
//C#语言
namespace ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
IComparable[] agent;
Console.WriteLine("");
int cnumber=0;
int snumber = 0;
agent = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < agent.Length; i++)
{
int minpos = i;
for (int j = i + 1; j < agent.Length; j++)
{
if (agent[minpos].CompareTo(agent[j]) > 0)
minpos = j;
cnumber++;
}
if (minpos != i)
{
IComparable tmp = agent[i];
agent[i] = agent[minpos];
agent[minpos] = tmp;
snumber++;
}
}
string result = string.Empty;
foreach (var s in agent)
{
result += s + "";
}
result += "\n共对比" + cnumber + "次,交换" + snumber + "次";
Console.WriteLine(result);
Console.ReadLine();
}
}
}
namespace ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
IComparable[] agent;
Console.WriteLine("");
int cnumber=0;
int snumber = 0;
agent = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < agent.Length; i++)
{
int minpos = i;
for (int j = i + 1; j < agent.Length; j++)
{
if (agent[minpos].CompareTo(agent[j]) > 0)
minpos = j;
cnumber++;
}
if (minpos != i)
{
IComparable tmp = agent[i];
agent[i] = agent[minpos];
agent[minpos] = tmp;
snumber++;
}
}
string result = string.Empty;
foreach (var s in agent)
{
result += s + "";
}
result += "\n共对比" + cnumber + "次,交换" + snumber + "次";
Console.WriteLine(result);
Console.ReadLine();
}
}
}
相关文章推荐
- 数据结构考研复习--线性表1
- 1.数据结构和算法的基础笔记
- SDUT 数据结构实验之栈六:下一较大值(二)
- 数据结构与算法是什么
- 数据结构考试
- THU数据结构编程作业一:列车调度(Train)
- Language of FatMouse(trie树)
- Python基本数据结构 习题及源码
- 数据结构基础7_链队的实现
- 数据结构之链表(链接)
- 堆排序
- 数据结构实验之栈六:下一较大值(二)【OJ-3333】【自己写的封装的栈代码】
- hbase系统架构及数据结构
- 【数据结构与算法分析】1.1 找出N个数字中第K大的数
- 数据结构栈应用之进制转换
- What Are You Talking About(trie树或map)
- opencv学习笔记(十三)——数据结构
- 数据结构c字符串操作语言版本
- 《数据结构与算法分析-C语言描述》中的字谜问题
- spfa 模板