调整目录结构
This commit is contained in:
53
Assets/Scripts/Model/Utils/AlgorithmUtils.cs
Normal file
53
Assets/Scripts/Model/Utils/AlgorithmUtils.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class AlgorithmUtils
|
||||
{
|
||||
#region 快速排序
|
||||
|
||||
public static void QuickSortRecursive<T>(IList<T> dataList, int left, int right, Comparison<T> comparer)
|
||||
{
|
||||
if (dataList == null)
|
||||
throw new ArgumentNullException();
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
if (left >= right)
|
||||
return;
|
||||
int mid = QuickSortPatition(dataList, left, right, comparer);
|
||||
QuickSortRecursive(dataList, left, mid - 1, comparer);
|
||||
QuickSortRecursive(dataList, mid + 1, right, comparer);
|
||||
}
|
||||
|
||||
private static int QuickSortPatition<T>(IList<T> dataList, int left, int right, Comparison<T> comparer)
|
||||
{
|
||||
int j = left;
|
||||
int i = j - 1;
|
||||
T equipKey = dataList[right]; //基准元素
|
||||
for (; j < right; ++j)
|
||||
{
|
||||
if (comparer(dataList[j], equipKey) < 0)
|
||||
{
|
||||
SwapData(dataList, j, ++i);
|
||||
}
|
||||
}
|
||||
|
||||
//把right交换到中间
|
||||
T t = dataList[right];
|
||||
dataList[right] = dataList[++i];
|
||||
dataList[i] = t;
|
||||
return i;
|
||||
}
|
||||
|
||||
private static void SwapData<T>(IList<T> dataList, int x, int y)
|
||||
{
|
||||
T tmp = dataList[x];
|
||||
dataList[x] = dataList[y];
|
||||
dataList[y] = tmp;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user