Skip to content

Commit

Permalink
Merge pull request kennyledet#479 from darubramha89/java-branch
Browse files Browse the repository at this point in the history
Stooge Sort Java Implementation
  • Loading branch information
patrickyevsukov committed Aug 20, 2015
2 parents 0c5a250 + db7569e commit 4c9f3f8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Stooge_Sort/Java/darubramha89/StoogeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package stooge.sort;

public class StoogeSort {

public int[] sort(int input[])
{
return stoogesort(input, 0, input.length-1);
}

public int[] stoogesort(int input[], int i, int j)
{

if(input[j] < input[i])
{
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
if((j-i+1) > 2)
{
int temp = (j- i + 1)/3;
stoogesort(input, i, j-temp);
stoogesort(input, i+temp, j);
stoogesort(input, i, j-temp);
}
return input;
}
}
21 changes: 21 additions & 0 deletions Stooge_Sort/Java/darubramha89/StoogeSort_test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stooge.sort;

import static org.junit.Assert.*;

import org.junit.Test;

public class StoogeSort_test {

@Test
public void test() {

int expected[] = {1,1,2,2,3,3,4,5,5,7,88 };
int input[] = {2,4,1,5,3,88,5,3,7,2,1};

StoogeSort ss = new StoogeSort();
assertArrayEquals("Both arrays should match and give sorted results",expected, ss.sort(input));
assertArrayEquals("Both arrays should match and give sorted results",expected, ss.sort(expected));

}

}

0 comments on commit 4c9f3f8

Please sign in to comment.