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

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

实现:

ContractedBlock.gif ExpandedBlockStart.gif Code
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;
        }
    }
}
单元测试:

ContractedBlock.gif ExpandedBlockStart.gif Code

        
/// <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;
        }

转载于:https://www.cnblogs.com/netfuns/archive/2009/09/15/1567293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值