diff --git a/src/DotRecast.Core/CollectionExtensions.cs b/src/DotRecast.Core/CollectionExtensions.cs index 1dd0173..cb6e1e7 100644 --- a/src/DotRecast.Core/CollectionExtensions.cs +++ b/src/DotRecast.Core/CollectionExtensions.cs @@ -5,6 +5,28 @@ namespace DotRecast.Core { public static class CollectionExtensions { + /// Sorts the given data in-place using insertion sort. + + /// + /// @param data The data to sort + /// @param dataLength The number of elements in @p data + public static void InsertSort(this int[] data) + { + for (int valueIndex = 1; valueIndex < data.Length; valueIndex++) + { + int value = data[valueIndex]; + int insertionIndex; + for (insertionIndex = valueIndex - 1; insertionIndex >= 0 && data[insertionIndex] > value; insertionIndex--) + { + // Shift over values + data[insertionIndex + 1] = data[insertionIndex]; + } + + // Insert the value in sorted order. + data[insertionIndex + 1] = value; + } + } + public static void ForEach(this IEnumerable collection, Action action) { foreach (var item in collection) diff --git a/src/DotRecast.Recast/RecastArea.cs b/src/DotRecast.Recast/RecastArea.cs index a9d1daf..4751bee 100644 --- a/src/DotRecast.Recast/RecastArea.cs +++ b/src/DotRecast.Recast/RecastArea.cs @@ -319,7 +319,8 @@ namespace DotRecast.Recast } } - Array.Sort(neighborAreas); + //Array.Sort(neighborAreas); + neighborAreas.InsertSort(); areas[spanIndex] = neighborAreas[4]; } }