Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork312
jUnit Test on Selection Sort#20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
homnay1234 wants to merge6 commits intorampatra:masterChoose a base branch fromhomnay1234:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
318cced
jUnit Test on Selection Sort
homnay1234eb9c094
Merge pull request #1 from homnay1234/homnay1234-patch-1
homnay12344b1b50a
jUnit Test on BinarySearch Algorithm
homnay123421ed288
Make a small Refactor for BinarySearch Algorithm
homnay12340eba744
jUnit Test on Binary Search Algorithm
homnay12343c82d58
Merge pull request #2 from homnay1234/homnay1234-patch-2
homnay1234File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletionsRefactor BinarySearch Algorithm
This file contains hidden or 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,73 @@ | ||
public class BinarySearch { | ||
/** | ||
* Searches an element {@param n} in a sorted array {@param a} | ||
* and returns its index in O(log n) time. The Index may not | ||
* correspond to the first occurrence of the element. | ||
* | ||
* @param a sorted array to be searched | ||
* @param n number to be searched in the array | ||
* @return index of {@param n} or {@code -1} if not present | ||
*/ | ||
public static int binarySearch(int[] a, int n) { | ||
return binarySearch(a, n, 0, a.length - 1); | ||
} | ||
public static int binarySearch(int[] a, int n, int low, int high) { | ||
if (low <= high) { | ||
int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 | ||
if (n == a[mid]) { | ||
return mid; | ||
} else if (n < a[mid]) { | ||
return binarySearch(a, n, 0, mid - 1); | ||
} else { | ||
return binarySearch(a, n, mid + 1, high); | ||
} | ||
} else { | ||
return -1; | ||
} | ||
} | ||
/** | ||
* Non-recursive version of binary search. | ||
* | ||
* @param a sorted array to be searched | ||
* @param n number to be searched in the array | ||
* @return index of {@param n} or {@code -1} if not present | ||
*/ | ||
public static int binarySearchNonRecursive(int[] a, int n) { | ||
int low = 0, high = a.length, mid; | ||
while (low <= high) { | ||
mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 | ||
if (n == a[mid]) { | ||
return mid; | ||
} else if (n < a[mid]) { | ||
high = mid - 1; | ||
} else { | ||
low = mid + 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
/** | ||
* Driver for testing. | ||
* | ||
* @param a | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println(binarySearch(new int[]{0, 2}, 2)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 2)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 3)); | ||
System.out.println(binarySearch(new int[]{0, 2}, 0)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); // doesn't return index of first occurrence | ||
System.out.println("---------"); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 2)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 2)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 3)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 0)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletionsTestBinarySearch
This file contains hidden or 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,43 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
class TestSearch { | ||
BinarySearch binarySearch = new BinarySearch() ; | ||
@Test | ||
public void tesInsertIntoSorted() { | ||
int[] inputArr = new int[] {0,1,2,3,4}; | ||
assertTrue( binarySearch.binarySearch(inputArr, 2) ==2 ); | ||
} | ||
BinarySearch binarySearch1 = new BinarySearch() ; | ||
@Test | ||
public void tesInsertIntoSorted1() { | ||
int[] inputArr = new int[] {0,1,2,3,4}; | ||
assertTrue( binarySearch1.binarySearch(inputArr, 1)==1 ); | ||
} | ||
BinarySearch binarySearch2 = new BinarySearch() ; | ||
@Test | ||
public void tesInsertIntoSorted2() { | ||
int[] inputArr = new int[] {0,1,2,3,4}; | ||
assertTrue( binarySearch2.binarySearch(inputArr, 0)==0 ); | ||
} | ||
BinarySearch binarySearch3 = new BinarySearch() ; | ||
@Test | ||
public void tesInsertIntoSorted3() { | ||
int[] inputArr = new int[] {0,1,2,3,4}; | ||
assertTrue( binarySearch3.binarySearch(inputArr, 1)==1 ); | ||
} | ||
BinarySearch binarySearch4 = new BinarySearch() ; | ||
@Test | ||
public void tesInsertIntoSorted4() { | ||
int[] inputArr = new int[] {0,1,2,3,4}; | ||
assertTrue( binarySearch4.binarySearch(inputArr, 1)==1 ); | ||
} | ||
} |
73 changes: 73 additions & 0 deletionsTestBinarySearch2
This file contains hidden or 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,73 @@ | ||
public class BinarySearch { | ||
/** | ||
* Searches an element {@param n} in a sorted array {@param a} | ||
* and returns its index in O(log n) time. The Index may not | ||
* correspond to the first occurrence of the element. | ||
* | ||
* @param a sorted array to be searched | ||
* @param n number to be searched in the array | ||
* @return index of {@param n} or {@code -1} if not present | ||
*/ | ||
public static int binarySearch(int[] a, int n) { | ||
return binarySearch(a, n, 0, a.length - 1); | ||
} | ||
public static int binarySearch(int[] a, int n, int low, int high) { | ||
if (low <= high) { | ||
int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 | ||
if (n == a[mid]) { | ||
return mid; | ||
} else if (n < a[mid]) { | ||
return binarySearch(a, n, 0, mid - 1); | ||
} else { | ||
return binarySearch(a, n, mid + 1, high); | ||
} | ||
} else { | ||
return -1; | ||
} | ||
} | ||
/** | ||
* Non-recursive version of binary search. | ||
* | ||
* @param a sorted array to be searched | ||
* @param n number to be searched in the array | ||
* @return index of {@param n} or {@code -1} if not present | ||
*/ | ||
public static int binarySearchNonRecursive(int[] a, int n) { | ||
int low = 0, high = a.length, mid; | ||
while (low <= high) { | ||
mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 | ||
if (n == a[mid]) { | ||
return mid; | ||
} else if (n < a[mid]) { | ||
high = mid - 1; | ||
} else { | ||
low = mid + 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
/** | ||
* Driver for testing. | ||
* | ||
* @param a | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println(binarySearch(new int[]{0, 2}, 2)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 2)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 3)); | ||
System.out.println(binarySearch(new int[]{0, 2}, 0)); | ||
System.out.println(binarySearch(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); // doesn't return index of first occurrence | ||
System.out.println("---------"); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 2)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 2)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 3)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 0)); | ||
System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletionsTestSelectionSort
This file contains hidden or 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,36 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
class TestSort { | ||
SelectionSort selectionSort = new SelectionSort(); | ||
@Test | ||
public void tesInsertIntoSorted() { | ||
int[] inputArr = new int[] {1,3,5}; | ||
int[] inputResult = new int[] {1,3,5}; | ||
selectionSort.selectionSort(inputArr); | ||
assertArrayEquals(inputResult, inputArr); | ||
} | ||
SelectionSort selectionSort2 = new SelectionSort(); | ||
@Test | ||
public void tesInsertIntoSorted2() { | ||
int[] inputArr = new int[] {5,3,1}; | ||
int[] inputResult = new int[] {1,3,5}; | ||
selectionSort.selectionSort(inputArr); | ||
assertArrayEquals(inputResult, inputArr); | ||
} | ||
SelectionSort selectionSort3 = new SelectionSort(); | ||
@Test | ||
public void tesInsertIntoSorted3() { | ||
int[] inputArr = new int[] {4,5,3,2}; | ||
int[] inputResult = new int[] {2,3,4,5}; | ||
selectionSort.selectionSort(inputArr); | ||
assertArrayEquals(inputResult, inputArr); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.