Skip to content

Commit 4c9f3f8

Browse files
Merge pull request kennyledet#479 from darubramha89/java-branch
Stooge Sort Java Implementation
2 parents 0c5a250 + db7569e commit 4c9f3f8

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package stooge.sort;
2+
3+
public class StoogeSort {
4+
5+
public int[] sort(int input[])
6+
{
7+
return stoogesort(input, 0, input.length-1);
8+
}
9+
10+
public int[] stoogesort(int input[], int i, int j)
11+
{
12+
13+
if(input[j] < input[i])
14+
{
15+
int temp = input[i];
16+
input[i] = input[j];
17+
input[j] = temp;
18+
}
19+
if((j-i+1) > 2)
20+
{
21+
int temp = (j- i + 1)/3;
22+
stoogesort(input, i, j-temp);
23+
stoogesort(input, i+temp, j);
24+
stoogesort(input, i, j-temp);
25+
}
26+
return input;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package stooge.sort;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class StoogeSort_test {
8+
9+
@Test
10+
public void test() {
11+
12+
int expected[] = {1,1,2,2,3,3,4,5,5,7,88 };
13+
int input[] = {2,4,1,5,3,88,5,3,7,2,1};
14+
15+
StoogeSort ss = new StoogeSort();
16+
assertArrayEquals("Both arrays should match and give sorted results",expected, ss.sort(input));
17+
assertArrayEquals("Both arrays should match and give sorted results",expected, ss.sort(expected));
18+
19+
}
20+
21+
}

0 commit comments

Comments
 (0)