Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 53 additions & 54 deletions Algorithms/Sorters/Comparison/BinaryInsertionSorter.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,71 @@
using System;
using System.Collections.Generic;

namespace Algorithms.Sorters.Comparison
namespace Algorithms.Sorters.Comparison;

/// <summary>
/// TODO.
/// </summary>
/// <typeparam name="T">TODO. 2.</typeparam>
public class BinaryInsertionSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// TODO.
/// Sorts array using specified comparer,
/// variant of insertion sort where binary search is used to find place for next element
/// internal, in-place, unstable,
/// time complexity: O(n^2),
/// space complexity: O(1),
/// where n - array length.
/// </summary>
/// <typeparam name="T">TODO. 2.</typeparam>
public class BinaryInsertionSorter<T> : IComparisonSorter<T>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer)
{
/// <summary>
/// Sorts array using specified comparer,
/// variant of insertion sort where binary search is used to find place for next element
/// internal, in-place, unstable,
/// time complexity: O(n^2),
/// space complexity: O(1),
/// where n - array length.
/// </summary>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer)
for (var i = 1; i < array.Length; i++)
{
for (var i = 1; i < array.Length; i++)
{
var target = array[i];
var moveIndex = i - 1;
var targetInsertLocation = BinarySearch(array, 0, moveIndex, target, comparer);
Array.Copy(array, targetInsertLocation, array, targetInsertLocation + 1, i - targetInsertLocation);
var target = array[i];
var moveIndex = i - 1;
var targetInsertLocation = BinarySearch(array, 0, moveIndex, target, comparer);
Array.Copy(array, targetInsertLocation, array, targetInsertLocation + 1, i - targetInsertLocation);

array[targetInsertLocation] = target;
}
array[targetInsertLocation] = target;
}
}

/// <summary>Implementation of Binary Search using an iterative approach.</summary>
/// <param name="array">
/// An array of values sorted in ascending order between the index values left and right to search
/// through.
/// </param>
/// <param name="from">Left index to search from (inclusive).</param>
/// <param name="to">Right index to search to (inclusive).</param>
/// <param name="target">The value to find placefor in the provided array.</param>
/// <param name="comparer">TODO.</param>
/// <returns>The index where to insert target value.</returns>
private static int BinarySearch(T[] array, int from, int to, T target, IComparer<T> comparer)
/// <summary>Implementation of Binary Search using an iterative approach.</summary>
/// <param name="array">
/// An array of values sorted in ascending order between the index values left and right to search
/// through.
/// </param>
/// <param name="from">Left index to search from (inclusive).</param>
/// <param name="to">Right index to search to (inclusive).</param>
/// <param name="target">The value to find placefor in the provided array.</param>
/// <param name="comparer">TODO.</param>
/// <returns>The index where to insert target value.</returns>
private static int BinarySearch(T[] array, int from, int to, T target, IComparer<T> comparer)
{
var left = from;
var right = to;
while (right > left)
{
var left = from;
var right = to;
while (right > left)
{
var middle = (left + right) / 2;
var comparisonResult = comparer.Compare(target, array[middle]);

if (comparisonResult == 0)
{
return middle + 1;
}
var middle = (left + right) / 2;
var comparisonResult = comparer.Compare(target, array[middle]);

if (comparisonResult > 0)
{
left = middle + 1;
}
else
{
right = middle - 1;
}
if (comparisonResult == 0)
{
return middle + 1;
}

return comparer.Compare(target, array[left]) < 0 ? left : left + 1;
if (comparisonResult > 0)
{
left = middle + 1;
}
else
{
right = middle - 1;
}
}

return comparer.Compare(target, array[left]) < 0 ? left : left + 1;
}
}
81 changes: 40 additions & 41 deletions Algorithms/Sorters/Comparison/BogoSorter.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
using System;
using System.Collections.Generic;

namespace Algorithms.Sorters.Comparison
namespace Algorithms.Sorters.Comparison;

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

/// <summary>
/// Class that implements bogo sort algorithm.
/// TODO.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class BogoSorter<T> : IComparisonSorter<T>
/// <param name="array">TODO. 2.</param>
/// <param name="comparer">TODO. 3.</param>
public void Sort(T[] array, IComparer<T> comparer)
{
private readonly Random random = new();

/// <summary>
/// TODO.
/// </summary>
/// <param name="array">TODO. 2.</param>
/// <param name="comparer">TODO. 3.</param>
public void Sort(T[] array, IComparer<T> comparer)
while (!IsSorted(array, comparer))
{
while (!IsSorted(array, comparer))
{
Shuffle(array);
}
Shuffle(array);
}
}

private bool IsSorted(T[] array, IComparer<T> comparer)
private bool IsSorted(T[] array, IComparer<T> comparer)
{
for (var i = 0; i < array.Length - 1; i++)
{
for (var i = 0; i < array.Length - 1; i++)
if (comparer.Compare(array[i], array[i + 1]) > 0)
{
if (comparer.Compare(array[i], array[i + 1]) > 0)
{
return false;
}
return false;
}

return true;
}

private void Shuffle(T[] array)
return true;
}

private void Shuffle(T[] array)
{
var taken = new bool[array.Length];
var newArray = new T[array.Length];
for (var i = 0; i < array.Length; i++)
{
var taken = new bool[array.Length];
var newArray = new T[array.Length];
for (var i = 0; i < array.Length; i++)
int nextPos;
do
{
int nextPos;
do
{
nextPos = random.Next(0, int.MaxValue) % array.Length;
}
while (taken[nextPos]);

taken[nextPos] = true;
newArray[nextPos] = array[i];
nextPos = random.Next(0, int.MaxValue) % array.Length;
}
while (taken[nextPos]);

for (var i = 0; i < array.Length; i++)
{
array[i] = newArray[i];
}
taken[nextPos] = true;
newArray[nextPos] = array[i];
}

for (var i = 0; i < array.Length; i++)
{
array[i] = newArray[i];
}
}
}
57 changes: 28 additions & 29 deletions Algorithms/Sorters/Comparison/BubbleSorter.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace Algorithms.Sorters.Comparison
namespace Algorithms.Sorters.Comparison;

/// <summary>
/// Class that implements bubble sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class BubbleSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Class that implements bubble sort algorithm.
/// Sorts array using specified comparer,
/// internal, in-place, stable,
/// time complexity: O(n^2),
/// space complexity: O(1),
/// where n - array length.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class BubbleSorter<T> : IComparisonSorter<T>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer)
{
/// <summary>
/// Sorts array using specified comparer,
/// internal, in-place, stable,
/// time complexity: O(n^2),
/// space complexity: O(1),
/// where n - array length.
/// </summary>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer)
for (var i = 0; i < array.Length - 1; i++)
{
for (var i = 0; i < array.Length - 1; i++)
var wasChanged = false;
for (var j = 0; j < array.Length - i - 1; j++)
{
var wasChanged = false;
for (var j = 0; j < array.Length - i - 1; j++)
if (comparer.Compare(array[j], array[j + 1]) > 0)
{
if (comparer.Compare(array[j], array[j + 1]) > 0)
{
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
wasChanged = true;
}
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
wasChanged = true;
}
}

if (!wasChanged)
{
break;
}
if (!wasChanged)
{
break;
}
}
}
Expand Down
Loading