From 7f10d5814d929c5c7710deed0523420c4404d201 Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 25 Jul 2023 15:44:57 +0900 Subject: [PATCH] changed insert sort --- src/DotRecast.Core/CollectionExtensions.cs | 22 ++++++++++++++++++++++ src/DotRecast.Recast/RecastArea.cs | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) 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]; } }