File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
Algorithms.Tests/Sorters/Comparison
Algorithms/Sorters/Comparison Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 1+ using System ;
2+ using Algorithms . Sorters . Comparison ;
3+ using Algorithms . Tests . Helpers ;
4+ using NUnit . Framework ;
5+
6+ namespace Algorithms . Tests . Sorters . Comparison ;
7+
8+ public static class GnomeSorterTests
9+ {
10+ [ Test ]
11+ public static void ArraySorted (
12+ [ Random ( 0 , 1000 , 100 , Distinct = true ) ]
13+ int n )
14+ {
15+ // Arrange
16+ var sorter = new GnomeSorter < int > ( ) ;
17+ var intComparer = new IntComparer ( ) ;
18+ var ( correctArray , testArray ) = RandomHelper . GetArrays ( n ) ;
19+
20+ // Act
21+ sorter . Sort ( testArray , intComparer ) ;
22+ Array . Sort ( correctArray , intComparer ) ;
23+
24+ // Assert
25+ Assert . That ( correctArray , Is . EqualTo ( testArray ) ) ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ using System . Collections . Generic ;
2+
3+ namespace Algorithms . Sorters . Comparison ;
4+
5+ /// <summary>
6+ /// Class that implements gnome sort algorithm.
7+ /// </summary>
8+ /// <typeparam name="T">Type of array element.</typeparam>
9+ public class GnomeSorter < T > : IComparisonSorter < T >
10+ {
11+ /// <summary>
12+ /// Moves forward through the array until it founds two elements out of order,
13+ /// then swaps them and move back one position,
14+ /// internal, in-place, stable,
15+ /// time complexity: O(n2),
16+ /// space complexity: O(1).
17+ /// </summary>
18+ /// <param name="array">Array to sort.</param>
19+ /// <param name="comparer">Compares elements.</param>
20+ public void Sort ( T [ ] array , IComparer < T > comparer )
21+ {
22+ int index = 0 ;
23+
24+ while ( index < array . Length )
25+ {
26+ if ( index == 0 || comparer . Compare ( array [ index ] , array [ index - 1 ] ) >= 0 )
27+ {
28+ index ++ ;
29+ }
30+ else
31+ {
32+ Swap ( array , index , index - 1 ) ;
33+ index -- ;
34+ }
35+ }
36+ }
37+
38+ public void Swap ( T [ ] array , int index1 , int index2 )
39+ {
40+ ( array [ index1 ] , array [ index2 ] ) = ( array [ index2 ] , array [ index1 ] ) ;
41+ }
42+ }
Original file line number Diff line number Diff line change @@ -124,6 +124,7 @@ find more than one implementation for the same objective but using different alg
124124 * [ Comb Sort] ( ./Algorithms/Sorters/Comparison/CombSorter.cs )
125125 * [ Cycle Sort] ( ./Algorithms/Sorters/Comparison/CycleSorter.cs )
126126 * [ Exchange Sort] ( ./Algorithms/Sorters/Comparison/ExchangeSorter.cs )
127+ * [ Gnome Sort] ( ./Algorithms/Sorters/Comparison/GnomeSorter.cs )
127128 * [ Heap Sort] ( ./Algorithms/Sorters/Comparison/HeapSorter.cs )
128129 * [ Insertion Sort] ( ./Algorithms/Sorters/Comparison/InsertionSorter.cs )
129130 * [ Merge Sort] ( ./Algorithms/Sorters/Comparison/MergeSorter.cs )
You can’t perform that action at this time.
0 commit comments