算法 - 选择排序(C#)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
// --------------------------------------------------------------------------------------------------------------------// <copyright file="Program.cs" company="Chimomo's Company">//// Respect the work.//// </copyright>// <summary>//// The selection sort.//// 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完为止。选择排序是不稳定的排序算法。//// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// The program.    /// </summary>    public static class Program    {        /// <summary>        /// The main.        /// </summary>        public static void Main()        {            int[] a = { 1, 6, 4, 2, 8, 7, 9, 3, 10, 5 };            Console.WriteLine("Before Selection Sort:");            foreach (int i in a)            {                Console.Write(i + " ");            }            Console.WriteLine("\r\n\r\nIn Selection Sort:");            SelectionSort(a);            Console.WriteLine("\r\nAfter Selection Sort:");            foreach (int i in a)            {                Console.Write(i + " ");            }            Console.WriteLine(string.Empty);        }        /// <summary>        /// The selection sort.        /// </summary>        /// <param name="a">        /// The a.        /// </param>        private static void SelectionSort(int[] a)        {            for (int i = 0; i < a.Length - 1; i++)            {                int min = i; // 存储最小元素的index。                // 寻找最小元素的index。                for (int j = i + 1; j < a.Length; j++)                {                    if (a[j] < a[min])                    {                        min = j;                    }                }                int tmp = a[min];                a[min] = a[i];                a[i] = tmp;                Console.Write("Round {0}: ", i + 1);                // 打印数组。                foreach (int k in a)                {                    Console.Write(k + " ");                }                Console.WriteLine(string.Empty);            }        }    }}// Output:/*Before Selection Sort:1 6 4 2 8 7 9 3 10 5In Selection Sort:Round 1: 1 6 4 2 8 7 9 3 10 5Round 2: 1 2 4 6 8 7 9 3 10 5Round 3: 1 2 3 6 8 7 9 4 10 5Round 4: 1 2 3 4 8 7 9 6 10 5Round 5: 1 2 3 4 5 7 9 6 10 8Round 6: 1 2 3 4 5 6 9 7 10 8Round 7: 1 2 3 4 5 6 7 9 10 8Round 8: 1 2 3 4 5 6 7 8 10 9Round 9: 1 2 3 4 5 6 7 8 9 10After Selection Sort:1 2 3 4 5 6 7 8 9 10*/
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值