-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Studio-23-xyz/Bogoshort
Bogo
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters