快速排序方法及其单元测试

发表于:2009-9-30 16:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:51Testing会员    来源:51Testing博客

  快速排序是经典的排序方法。

  实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortPractice
{
    class MyQuickSorter
    {
        public int[] Sort(int[] src,int leftIndex,int rightIndex)
        {

            int left = leftIndex;
            int right =rightIndex;

          
            if (left < right)
            {
            int i = left;
            int j = right + 1;
                int guard = src[left];
                int temp = 0;

                do
                {
                    do
                    {
                        i++;
                    }
                    while (  i < right &&src[i] < guard);

                    do
                    {
                        j--;
                    }
                    while (src[j] > guard && j > left);
                    if (i < j)
                    {
                      temp = src[i];
                        src[i] = src[j];
                        src[j] = temp;
                    }

                } while (i < j);

 

                    temp = src[j];
                    src[j] = guard;
                    //guard = temp;
                    src[left] = temp;


                Sort(src, left, j - 1);
                Sort(src, j+1, rightIndex);

 

            }


            return null;
        }
    }
}

  单元测试

        /// <summary>
        ///Sort 的测试
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SortPractice.exe")]
        public void SortTest()
        {
            MyQuickSorter_Accessor target = new MyQuickSorter_Accessor(); // TODO: 初始化为适当的值

            int TestCount=10000;
            int ArraySize=300;

            Random r = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < TestCount; i++)
            {
                List<int> arr = new List<int>();
                for (int j = 0; j < ArraySize; j++)
                {
                    arr.Add(r.Next());
                }

                int[] src = arr.ToArray();
                int[] after =(int[]) src.Clone();

                target.Sort(after, 0, after.Length - 1);
                Array.Sort(src);


                bool equal=IntEqualInArray(src,after);
                Assert.IsTrue(equal);


            }

        }

        bool IntEqualInArray(int[] src, int[] after)
        {
            if (src.Length != after.Length)
            {
                return false;
            }
            for (int i = 0; i < src.Length; i++)
            {
                if (src[i] != after[i])
                {
                    return false;
                }
            }

            return true;
        }

版权声明:本文欢迎转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号