-
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.
- Loading branch information
1 parent
665442d
commit 8eef431
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
public class BinaryDSA { | ||
public static int rank(int key, int[] array) { | ||
return rank(key, array, 0, array.length - 1); | ||
} | ||
|
||
public static int rank(int key, int[] array, int lo, int hi) { | ||
if (lo > hi) return -1; | ||
int mid = lo + (hi - lo) / 2; | ||
if (key < array[mid]) return rank(key, array, lo, mid - 1); | ||
else if (key > array[mid]) return rank(key, array, mid + 1, hi); | ||
else return mid; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] sortedArray = {10, 20, 30, 40, 50}; | ||
int keyToFind = 40; | ||
|
||
System.out.println(rank(keyToFind,sortedArray)); | ||
} | ||
} |
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,32 @@ | ||
public class BinarySearch { | ||
public static int binarySearch(int[] array, int key) { | ||
int low = 0; | ||
int high = array.length - 1; | ||
|
||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
if (array[mid] == key) { | ||
return mid; | ||
} else if (array[mid] < key) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] sortedArray = {10, 20, 30, 40, 50}; | ||
int keyToFind = 40; | ||
|
||
int result = binarySearch(sortedArray,keyToFind); | ||
if (result != -1) { | ||
System.out.println(keyToFind + " found at index " + result); | ||
}else { | ||
System.out.println(keyToFind + " not found in the array"); | ||
} | ||
} | ||
} |
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,49 @@ | ||
public class UnknownOrder { | ||
public static int binarySearch(int[] array, int key) { | ||
int low = 0; | ||
int high = array.length - 1; | ||
|
||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
if (array[mid] == key) { | ||
return mid; | ||
} else if (array[mid] > array[mid + 1]) { | ||
if (array[mid] > key) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} else { | ||
if (array[mid] < key) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] ascArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; | ||
int keyToFind1 = 40; | ||
|
||
int result1 = binarySearch(ascArray, keyToFind1); | ||
if (result1 != -1) { | ||
System.out.println(keyToFind1 + " found at index " + result1); | ||
} else { | ||
System.out.println(keyToFind1 + " not found in the array"); | ||
} | ||
int[] desArray = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10}; | ||
int keyToFind2 = 50; | ||
|
||
int result2 = binarySearch(desArray, keyToFind2); | ||
if (result2 != -1) { | ||
System.out.println(keyToFind2 + " found at index " + result2); | ||
} else { | ||
System.out.println(keyToFind2 + " not found in the array"); | ||
} | ||
|
||
} | ||
} |