Skip to content

Commit

Permalink
Merge pull request #16 from Studio-23-xyz/Bogoshort
Browse files Browse the repository at this point in the history
Bogo
Tanimul Haque Khan authored Aug 31, 2021
2 parents 8a95f21 + af59aa3 commit bb5367f
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions DTD.Sort.Net.Algorithms/Uncommons/StoogeSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using DTD.Sort.Net.Enums;
using DTD.Sort.Net.Interfaces;
using System;


namespace DTD.Sort.Net.Algorithms.Uncommons
{
public class StoogeSort<T> : ISort<T> where T : IComparable<T>
{
public SortType Type => SortType.Stooge;

public T[] Sort(T[] input, SortOrder sortOrder = SortOrder.Default)
{
if (input.Length > 1)
{
Stooging(input, 0, input.Length - 1,sortOrder);
}

return input;
}


private void Stooging(T[] L, int i, int j, SortOrder order)
{
if (Compare(L[j],L[i],order))
{
T tmp = L[i];
L[i] = L[j];
L[j] = tmp;
}
if (j - i > 1)
{
int t = (j - i + 1) / 3;
Stooging(L, i, j - t,order);
Stooging(L, i + t, j,order);
Stooging(L, i, j - t,order);
}
}

private bool Compare(T left, T right, SortOrder order)
{
var difference = left.CompareTo(right);

return order == SortOrder.Ascending ? difference < 0 : difference > 0;
}



}
}
2 changes: 1 addition & 1 deletion DTD.Sort.Net.Enums/SortType.cs
Original file line number Diff line number Diff line change
@@ -3,6 +3,6 @@ namespace DTD.Sort.Net.Enums
{
public enum SortType
{
Quick,Bubble,Selection,Insertion,Merge,Cocktail,Heap,Shell,Gnome,Pancake
Quick,Bubble,Selection,Insertion,Merge,Cocktail,Heap,Shell,Gnome,Pancake,Stooge
}
}

0 comments on commit bb5367f

Please sign in to comment.