Skip to content

Commit

Permalink
Split sorters into comparison and integer (TheAlgorithms#119)
Browse files Browse the repository at this point in the history
* Split sorters into comparison and integer

* Update README.md
  • Loading branch information
siriak authored Oct 19, 2019
1 parent 48bb3c7 commit 774ac35
Show file tree
Hide file tree
Showing 39 changed files with 147 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Algorithms.Tests/Compressors/HuffmanCompressorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Algorithms.DataCompression;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using NUnit.Framework;
using NUnit.Framework.Internal;

Expand Down
2 changes: 1 addition & 1 deletion Algorithms.Tests/Helpers/RandomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static (int[] correctArray, int[] testArray) GetArrays(int n)

for (var i = 0; i < n; i++)
{
var t = TestContext.CurrentContext.Random.Next(0, 1000);
var t = TestContext.CurrentContext.Random.Next(1_000_000);
testArr[i] = t;
correctArray[i] = t;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class BinaryInsertionSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class BogoSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;

using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class BubbleSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;

using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class CocktailSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class CycleSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class HeapSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class InsertionSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
/// <summary>
/// Class for testing merge sorter algorithm.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class PancakeSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class QuickSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class SelectionSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Comparison
{
public static class ShellSorterTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;

using Algorithms.Sorters;
using Algorithms.Sorters.Integer;
using Algorithms.Tests.Helpers;

using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Integer
{
public static class BucketSorterTests
{
Expand All @@ -14,11 +13,10 @@ public static void ArraySorted([Random(0, 1000, 1000, Distinct = true)]int n)
{
// Arrange
var sorter = new BucketSorter();
var intComparer = new IntComparer();
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
sorter.Sort(testArray, intComparer);
sorter.Sort(testArray);
Array.Sort(correctArray);

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.Integer;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Integer
{
public static class CountingSorterTests
{
[Test]
public static void SortsArray([Random(0, 10000, 100, Distinct = true)]int n)
{
// Arrange
var sorter = new CountingSorter();
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
CountingSorter.Sort(testArray);
sorter.Sort(testArray);
Array.Sort(correctArray);

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System;

using Algorithms.Sorters;
using Algorithms.Sorters.Integer;
using Algorithms.Tests.Helpers;

using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.Integer
{
public static class RadixSorterTests
{
[Test]
public static void SortsArray([Random(0, 1000, 100, Distinct = true)]int n)
{
// Arrange
const int bits = 4;
var sorter = new RadixSorter();
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
RadixSorter.Sort(testArray, bits);
sorter.Sort(testArray);
Array.Sort(correctArray);

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using System;
using Algorithms.Sorters;
using Algorithms.Sorters.String;
using Algorithms.Tests.Helpers;
using NUnit.Framework;

namespace Algorithms.Tests.Sorters
namespace Algorithms.Tests.Sorters.String
{
/// <summary>
/// Class for testing MSD radix sorter algorithm.
/// </summary>
public static class MsdRadixSorterTests
public static class MsdRadixStringSorterTests
{
[Test]
public static void ArraySorted([Random(2, 1000, 100, Distinct = true)]int n)
{
// Arrange
var sorter = new MsdRadixSorter();
var sorter = new MsdRadixStringSorter();
var (correctArray, testArray) = RandomHelper.GetStringArrays(n, 100, false);

// Act
Expand Down
6 changes: 3 additions & 3 deletions Algorithms/DataCompression/HuffmanCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Algorithms.Sorters;
using Algorithms.Sorters.Comparison;

namespace Algorithms.DataCompression
{
Expand All @@ -11,15 +11,15 @@ namespace Algorithms.DataCompression
public class HuffmanCompressor
{
// TODO: Use partial sorter
private readonly ISorter<ListNode> sorter;
private readonly IComparisonSorter<ListNode> sorter;
private readonly Translator translator;

/// <summary>
/// Initializes a new instance of the <see cref="HuffmanCompressor"/> class.
/// </summary>
/// <param name="sorter">Sorter to use for compression.</param>
/// <param name="translator">Translator.</param>
public HuffmanCompressor(ISorter<ListNode> sorter, Translator translator)
public HuffmanCompressor(IComparisonSorter<ListNode> sorter, Translator translator)
{
this.sorter = sorter;
this.translator = translator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// TODO.
/// </summary>
/// <typeparam name="T">TODO. 2.</typeparam>
public class BinaryInsertionSorter<T> : ISorter<T>
public class BinaryInsertionSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts array using specified comparer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Class that implements bogo sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class BogoSorter<T> : ISorter<T>
public class BogoSorter<T> : IComparisonSorter<T>
{
private readonly Random random = new Random();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Class that implements bubble sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class BubbleSorter<T> : ISorter<T>
public class BubbleSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts array using specified comparer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Cocktail Sort is a variation of Bubble sort, where Cocktail
/// Sort traverses through a given array in both directions alternatively.
/// </summary>
/// <typeparam name="T">Array input type.</typeparam>
public class CocktailSorter<T> : ISorter<T>
public class CocktailSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts array using Cocktail sort algorithm.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Cycle sort is an in-place, unstable sorting algorithm,
Expand All @@ -10,7 +10,7 @@ namespace Algorithms.Sorters
/// into cycles, which can individually be rotated to give a sorted result.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class CycleSorter<T> : ISorter<T>
public class CycleSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts input array using Cycle sort.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Collections.Generic;

namespace Algorithms.Sorters
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Heap sort is a comparison based sorting technique
/// based on Binary Heap data structure.
/// </summary>
/// <typeparam name="T">Input array type.</typeparam>
public class HeapSorter<T> : ISorter<T>
public class HeapSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts input array using heap sort algorithm.
Expand Down
Loading

0 comments on commit 774ac35

Please sign in to comment.