圈排序

来源:互联网 发布:淘宝如何退保证金 编辑:程序博客网 时间:2024/05/16 00:51
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//圈排序://它是一个就地、不稳定的排序算法,根据原始的数组,一种理论上最优的比较,并且与其它就地排序算法不同。它的思想是把要排的数列分解为圈,即可以分别旋转得到排序结果。//不同于其它排序的是,元素不会被放入数组的中任意位置从而推动排序。每个值如果它已经在其正确的位置则不动,否则只需要写一次即可。也就是说仅仅最小覆盖就能完成排序。namespace _05圈排序{    class Program    {        static void Main(string[] args)        {            List<int> arrayToSort = new List<int>() { 12, 4654, 789, 6, 456, 174, 12, 54, 9 };            //int writes = 0;            for (int cycleStart = 0; cycleStart < arrayToSort.Count; cycleStart++)            {                //第一次循环,则取出第一个值放到item中,为12                int item = arrayToSort[cycleStart];                //这个表示第几次循环,第一次循环则pos为0                int pos = cycleStart;                do                {                    //给to赋初始值,假设当前数组中没有一个数比item小                    int to = 0;                    //循环遍历该数组,把每个数与该数比较,是否有比该数小的数                    for (int i = 0; i < arrayToSort.Count; i++)                    {                        //如果该数不是item 且 该数不比item小                        if (i != cycleStart && ((IComparable)arrayToSort[i]).CompareTo(item) < 0)                        {                            //记录该数组中比item小的个数                            to++;                        }                    }                    //首先,先知道pos表示是该数组中第几个数,to表示该数组中有几个数比当前取出item的值小                    //假设pos=0,to=3,则表示pos是该数组中第1个数,且该数组中还有3个数比它小                    //判断pos != to主要是为了避免pos==to,如果pos==to这表示item即将要移动的位置就是原来的位置                    if (pos != to)                    {                        //个人认为pos != to是多余的,因为前面if已经有这个判断了,没必要这要这个判断                        //而后面的判断是担心item要移动到的地方与该地方原来的值相等,如果相等的话就放到这个值的后面                        while (pos != to && ((IComparable)item).CompareTo(arrayToSort[to]) == 0)                        {                            to++;                        }                        int temp = arrayToSort[to];                        arrayToSort[to] = item;                        item = temp;                        //writes++;                        pos = to;                    }                } while (cycleStart != pos);//这里的while循环终止的条件是:假设是第一次循环,则要找到最小的一个数放在第一位,如果是第二次循环,则要找到第二小的数放到第二位。其中,中间不管循环了多少次,每次循环的时候都会把循环过的数放到该放的地方。            }            #region 遍历输出数组            for (int i = 0; i < arrayToSort.Count; i++)            {                Console.Write(arrayToSort[i].ToString() + ",");            }            Console.ReadKey();            #endregion        }        public IList CycleSort(IList arrayToSort)        {            int writes = 0;            for (int cycleStart = 0; cycleStart < arrayToSort.Count; cycleStart++)            {                object item = arrayToSort[cycleStart];                int pos = cycleStart;                do                {                    int to = 0;                    for (int i = 0; i < arrayToSort.Count; i++)                    {                        if (i != cycleStart && ((IComparable)arrayToSort[i]).CompareTo(item) < 0)                        {                            to++;                        }                    }                    if (pos != to)                    {                        while (pos != to && ((IComparable)item).CompareTo(arrayToSort[to]) == 0)                        {                            to++;                        }                        object temp = arrayToSort[to];                        arrayToSort[to] = item;                        //RedrawItem(to);                        item = temp;                        //RedrawItem(cycleStart);                        //pnlSamples.Refresh();                        //if (chkCreateAnimation.Checked)                        //    SavePicture();                        writes++;                        pos = to;                    }                } while (cycleStart != pos);            }            return arrayToSort;        }    }}

学习来源:http://blog.jobbole.com/72850/

0 0
原创粉丝点击