From ee71316de8640383a64b05c0953a9384449fd578 Mon Sep 17 00:00:00 2001 From: Jc Calayag <138150245+Jccqt@users.noreply.github.com> Date: Fri, 11 Jul 2025 14:34:18 +0800 Subject: [PATCH] Add Gnome sort algorithm --- .../Sorters/Comparison/GnomeSorterTests.cs | 27 ++++++++++++ Algorithms/Sorters/Comparison/GnomeSorter.cs | 42 +++++++++++++++++++ README.md | 1 + 3 files changed, 70 insertions(+) create mode 100644 Algorithms.Tests/Sorters/Comparison/GnomeSorterTests.cs create mode 100644 Algorithms/Sorters/Comparison/GnomeSorter.cs diff --git a/Algorithms.Tests/Sorters/Comparison/GnomeSorterTests.cs b/Algorithms.Tests/Sorters/Comparison/GnomeSorterTests.cs new file mode 100644 index 00000000..fc9cb5a8 --- /dev/null +++ b/Algorithms.Tests/Sorters/Comparison/GnomeSorterTests.cs @@ -0,0 +1,27 @@ +using System; +using Algorithms.Sorters.Comparison; +using Algorithms.Tests.Helpers; +using NUnit.Framework; + +namespace Algorithms.Tests.Sorters.Comparison; + +public static class GnomeSorterTests +{ + [Test] + public static void ArraySorted( + [Random(0, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var sorter = new GnomeSorter(); + var intComparer = new IntComparer(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + sorter.Sort(testArray, intComparer); + Array.Sort(correctArray, intComparer); + + // Assert + Assert.That(correctArray, Is.EqualTo(testArray)); + } +} diff --git a/Algorithms/Sorters/Comparison/GnomeSorter.cs b/Algorithms/Sorters/Comparison/GnomeSorter.cs new file mode 100644 index 00000000..28bfeeb2 --- /dev/null +++ b/Algorithms/Sorters/Comparison/GnomeSorter.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; + +namespace Algorithms.Sorters.Comparison; + +/// +/// Class that implements gnome sort algorithm. +/// +/// Type of array element. +public class GnomeSorter : IComparisonSorter +{ + /// + /// Moves forward through the array until it founds two elements out of order, + /// then swaps them and move back one position, + /// internal, in-place, stable, + /// time complexity: O(n2), + /// space complexity: O(1). + /// + /// Array to sort. + /// Compares elements. + public void Sort(T[] array, IComparer comparer) + { + int index = 0; + + while (index < array.Length) + { + if (index == 0 || comparer.Compare(array[index], array[index - 1]) >= 0) + { + index++; + } + else + { + Swap(array, index, index - 1); + index--; + } + } + } + + public void Swap(T[] array, int index1, int index2) + { + (array[index1], array[index2]) = (array[index2], array[index1]); + } +} diff --git a/README.md b/README.md index c9fd190e..198d2bd7 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ find more than one implementation for the same objective but using different alg * [Comb Sort](./Algorithms/Sorters/Comparison/CombSorter.cs) * [Cycle Sort](./Algorithms/Sorters/Comparison/CycleSorter.cs) * [Exchange Sort](./Algorithms/Sorters/Comparison/ExchangeSorter.cs) + * [Gnome Sort](./Algorithms/Sorters/Comparison/GnomeSorter.cs) * [Heap Sort](./Algorithms/Sorters/Comparison/HeapSorter.cs) * [Insertion Sort](./Algorithms/Sorters/Comparison/InsertionSorter.cs) * [Merge Sort](./Algorithms/Sorters/Comparison/MergeSorter.cs)