Skip to content

Commit bb5367f

Browse files
author
Tanimul Haque Khan
authored
Merge pull request #16 from Studio-23-xyz/Bogoshort
Bogo
2 parents 8a95f21 + af59aa3 commit bb5367f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using DTD.Sort.Net.Enums;
2+
using DTD.Sort.Net.Interfaces;
3+
using System;
4+
5+
6+
namespace DTD.Sort.Net.Algorithms.Uncommons
7+
{
8+
public class StoogeSort<T> : ISort<T> where T : IComparable<T>
9+
{
10+
public SortType Type => SortType.Stooge;
11+
12+
public T[] Sort(T[] input, SortOrder sortOrder = SortOrder.Default)
13+
{
14+
if (input.Length > 1)
15+
{
16+
Stooging(input, 0, input.Length - 1,sortOrder);
17+
}
18+
19+
return input;
20+
}
21+
22+
23+
private void Stooging(T[] L, int i, int j, SortOrder order)
24+
{
25+
if (Compare(L[j],L[i],order))
26+
{
27+
T tmp = L[i];
28+
L[i] = L[j];
29+
L[j] = tmp;
30+
}
31+
if (j - i > 1)
32+
{
33+
int t = (j - i + 1) / 3;
34+
Stooging(L, i, j - t,order);
35+
Stooging(L, i + t, j,order);
36+
Stooging(L, i, j - t,order);
37+
}
38+
}
39+
40+
private bool Compare(T left, T right, SortOrder order)
41+
{
42+
var difference = left.CompareTo(right);
43+
44+
return order == SortOrder.Ascending ? difference < 0 : difference > 0;
45+
}
46+
47+
48+
49+
}
50+
}

DTD.Sort.Net.Enums/SortType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ namespace DTD.Sort.Net.Enums
33
{
44
public enum SortType
55
{
6-
Quick,Bubble,Selection,Insertion,Merge,Cocktail,Heap,Shell,Gnome,Pancake
6+
Quick,Bubble,Selection,Insertion,Merge,Cocktail,Heap,Shell,Gnome,Pancake,Stooge
77
}
88
}

0 commit comments

Comments
 (0)