Skip to content

Add Gnome sort algorithm #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Algorithms.Tests/Sorters/Comparison/GnomeSorterTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>();
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));
}
}
42 changes: 42 additions & 0 deletions Algorithms/Sorters/Comparison/GnomeSorter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;

namespace Algorithms.Sorters.Comparison;

/// <summary>
/// Class that implements gnome sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class GnomeSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// 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).
/// </summary>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> 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]);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down