算法是如何影响程序编码方式的 - 基本排序算法

我们的目标是从一个int的Array中,找出最小值。

作为demo程序,我们先增加一个类,用来模拟Array,如下定义:

class CArray

    {

        const int DefaultCapacity = 100;



        private int[] array;

        private int curPosition = -1;





        public CArray()

        { 

            this.array=new int[DefaultCapacity];

            curPosition = -1;

        }

        public CArray(int capacity)

        {

            this.array = new int[capacity];

            curPosition = -1;

        }

        public void Insert(int value)

        {

            if (curPosition + 1 >= array.Length)

                throw new Exception("数组已经达到最大长度,无法再插入了。");



            curPosition++;

            array[curPosition] = value;

        }

        public void Clear()

        {

            curPosition = -1;

            this.array=new int[this.array.Length];

        }



        public void Display()

        {

            Console.WriteLine();

            for (var i = 0; i < this.curPosition; i++)

                Console.Write(string.Format("{0}, ", this.array[i]));

        }



        #region 统计函数

        public int Max()

        { 

            int maxValue=this.array[0];

            for (var i = 1; i <= this.curPosition; i++)

                if (maxValue < this.array[i])

                    maxValue = this.array[i];

            return maxValue;

        }

        public int Min()

        {

            int minValue = this.array[0];

            for (var i = 1; i <= this.curPosition; i++)

                if (minValue > this.array[i])

                    minValue = this.array[i];

            return minValue;

        }

        #endregion

再加入一个计算所用时间的类,用来计算Min/Max函数所运行的时间

class Timing

    {

        Stopwatch tm = new Stopwatch();



        public void Start()

        { 

            GC.Collect();

            GC.WaitForPendingFinalizers();



            tm.Reset();

            tm.Start();

        }



        public void Stop()

        {

            tm.Stop();

        }



        public void Display(string msg)

        {

            Console.WriteLine(msg + ", 共花费了 " + tm.Elapsed.TotalMilliseconds + " 毫秒");

        }

    }

 

我们先来写个最简单的,没有排序功能的demo程序,来看看当没有排序时的性能:

int count = 19999;



CArray aryObj = new CArray(count);



Random rnd = new Random(DateTime.Now.Second);

for (var i = 0; i < count; i++)

        aryObj.Insert((int)(rnd.NextDouble() * 100000) + 1);



Timing t = new Timing();

t.Start();

aryObj.Min();

t.Stop();

t.Display("排序前Min值");

 

 运行结果:

算法是如何影响程序编码方式的 - 基本排序算法

 然后,我们再为这个CArray编写一个排序功能,如下:

public void BubbleSort()

        {

            int temp;

            for (var i = this.curPosition; i >= 1; i--)

            {

                for (var j = 0; j <= i-1; j++)

                {

                    if (this.array[j] > this.array[i])

                    { 

                        temp=this.array[i];

                        this.array[i] = this.array[j];

                        this.array[j]=temp;

                    }

                }

            }

            sorted = true;//新增加的一个私有成员

        }



public int Min()//修改

        {

            if (sorted)

                return this.array[0];



            int minValue = this.array[0];

            for (var i = 1; i <= this.curPosition; i++)

                if (minValue > this.array[i])

                    minValue = this.array[i];

            return minValue;

        }

 

 然后修改主程序如下

int count = 19999;



                CArray aryObj = new CArray(count);



                Random rnd = new Random(DateTime.Now.Second);

                for (var i = 0; i < count; i++)

                    aryObj.Insert((int)(rnd.NextDouble() * 100000) + 1);



                Timing t = new Timing();

                t.Start();

                aryObj.Min();

                t.Stop();

                t.Display("排序前Min值");



                t.Start();

                aryObj.BubbleSort();

                t.Stop();

                t.Display("冒泡排序");



                t.Start();

                aryObj.Min();

                t.Stop();

                t.Display("排序后Min值");

 

运行结果图:

算法是如何影响程序编码方式的 - 基本排序算法

Min函数的执行效率在排序前后相差了379倍。

其实到这里大家都懂了,但是又出现了另外一个话题:排序函数怎么花了这么长时间?

  • 这就牵涉到各种排序算法的比较了,以后再讲。

有人肯定会说,这些排序算法在C#中都已经被ms实现了,所以没用!

  • 对,是被实现了,但本篇是讲现实项目中,算法是如何影响程序的
  • ms实现的是一些常用算法,要是遇到了非常用,非标准的算法才能解决的问题呢?比如各种算法的变种,你不会要等ms出一个更新才用吧

 

 

你可能感兴趣的:(排序算法)