Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
QuickSorter.cs 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
Nicholas Rodine 提交于 2018-02-08 02:38 +08:00 . Removed unused usings (#61)
using System.Collections.Generic;
using Algorithms.Common;
namespace Algorithms.Sorting
{
public static class QuickSorter
{
//
// The public APIs for the quick sort algorithm.
public static void QuickSort<T>(this IList<T> collection, Comparer<T> comparer = null)
{
int startIndex = 0;
int endIndex = collection.Count - 1;
//
// If the comparer is Null, then initialize it using a default typed comparer
comparer = comparer ?? Comparer<T>.Default;
collection.InternalQuickSort(startIndex, endIndex, comparer);
}
//
// Private static method
// The recursive quick sort algorithm
private static void InternalQuickSort<T>(this IList<T> collection, int leftmostIndex, int rightmostIndex, Comparer<T> comparer)
{
//
// Recursive call check
if (leftmostIndex < rightmostIndex)
{
int wallIndex = collection.InternalPartition(leftmostIndex, rightmostIndex, comparer);
collection.InternalQuickSort(leftmostIndex, wallIndex - 1, comparer);
collection.InternalQuickSort(wallIndex + 1, rightmostIndex, comparer);
}
}
//
// Private static method
// The partition function, used in the quick sort algorithm
private static int InternalPartition<T>(this IList<T> collection, int leftmostIndex, int rightmostIndex, Comparer<T> comparer)
{
int wallIndex, pivotIndex;
// Choose the pivot
pivotIndex = rightmostIndex;
T pivotValue = collection[pivotIndex];
// Compare remaining array elements against pivotValue
wallIndex = leftmostIndex;
// Loop until pivot: exclusive!
for (int i = leftmostIndex; i <= (rightmostIndex - 1); i++)
{
// check if collection[i] <= pivotValue
if (comparer.Compare(collection[i], pivotValue) <= 0)
{
collection.Swap(i, wallIndex);
wallIndex++;
}
}
collection.Swap(wallIndex, pivotIndex);
return wallIndex;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/strongandyzhang/C-Sharp-Algorithms.git
git@gitee.com:strongandyzhang/C-Sharp-Algorithms.git
strongandyzhang
C-Sharp-Algorithms
C-Sharp-Algorithms
master

搜索帮助