学习笔记,排序算法(选择排序)

目标:对数组(列表等任意有序容器)进行排序

方法:对列表进行遍历,选出最大的

    之后将这个数储存起来,对剩下的数再选择最大的,之后再对剩下数做同样的操作

   直至结束即可。

    代码如下:

public class Example02:IComMethod
    {
        public Example02()
        {

        }

        private void mainLogic(){
            List<int> list = new List<int> { 4, 2, 3, 5, 1, 8, 8, 0 };
            List<int> list2 = list;
            int li = list2.Count;
            List<int> newList = new List<int>();
            for (int i = 0; i < li; i++)
            {
                int tmp = list2[0];
                int index = 0;
                for (int j = 0; j < list2.Count - 1; j++)
                {
                    if (list2[j] < list2[j + 1])
                    {
                        tmp = list2[j + 1];
                        index = j + 1;
                    }
                }
                newList.Add(tmp);
                list2.Remove(tmp);
            }
            for (int i = 0; i < newList.Count; i++)
            {
                Console.WriteLine(newList[i] + "@");
            }
        }

        public void Run()
        {
            mainLogic();
        }

        public void Run(int key)
        {

        }
    }

算法时间复杂度0(n2)

猜你喜欢

转载自www.cnblogs.com/BXLH/p/10444228.html
今日推荐