forked from huskyii/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
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 kennyledet#479 from darubramha89/java-branch
Stooge Sort Java Implementation
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
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,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; | ||
} | ||
} |
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,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)); | ||
|
||
} | ||
|
||
} |