-
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. This topic can be tested on the AP Exam but it isnt commonly seen.
-
Binary search is one of the fastest searching algorithms. When compared to a linear search, it is much faster the bigger the array is
- Which of the following is a prerequisite for using binary search in Java?
- The array must be sorted in ascending order
- The array must be sorted in descending order
- The array can be in any order
- The array must have at least one element
- What is the worst-case scenario for binary search in Java?
- The element being searched for is at the beginning of the array
- The element being searched for is at the end of the array
- The element being searched for is not in the array
- The element being searched for is in the middle of the array
- Which of the following data structures is best suited for binary search?
- Linked List
- Array
- Stack
- Queue
- What is returned if the target is not present in the array?
- False
- 0
- -1
- The index of the element closest in value to the target
- What is returned if the target is present multiple times in the array?
- Binary Search does not work with duplicate values
- The index of the first occurrence will be returned
- The index of the middle occurrence will be returned
- The index of one of the occurrences will be returned
Answers: 1, 3, 2, 3, 4
public static void mergeSort(int[] array) {
if (array.length > 1) {
int mid = array.length / 2;
int[] leftArray = Arrays.copyOfRange(array, 0, mid);
int[] rightArray = Arrays.copyOfRange(array, mid, array.length);
mergeSort(leftArray);
mergeSort(rightArray);
int i = 0, j = 0, k = 0;
while (i < leftArray.length && j < rightArray.length) {
if (leftArray[i] < rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
while (i < leftArray.length) {
array[k] = leftArray[i];
i++;
k++;
}
while (j < rightArray.length) {
array[k] = rightArray[j];
j++;
k++;
}
}
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2};
mergeSort(array);
int index = binarySearch(array, 7);
if (index != -1) {
System.out.println("Index of 7 is: " + index);
} else {
System.out.println("7 is not found in the array");
}
}
public static void mergeSort(int[] array) {
if (array.length > 1) {
int mid = array.length / 2;
int[] leftArray = Arrays.copyOfRange(array, 0, mid);
int[] rightArray = Arrays.copyOfRange(array, mid, array.length);
mergeSort(leftArray);
mergeSort(rightArray);
int i = 0, j = 0, k = 0;
while (i < leftArray.length && j < rightArray.length) {
if (leftArray[i] < rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
while (i < leftArray.length) {
array[k] = leftArray[i];
i++;
k++;
}
while (j < rightArray.length) {
array[k] = rightArray[j];
j++;
k++;
}
}
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2};
mergeSort(array);
int index = binarySearch(array, 7);
if (index != -1) {
System.out.println("Index of 7 is: " + index);
} else {
System.out.println("7 is not found in the array");
}
}
class MergeSort {
void merge(String arr[], int l, int m, int r)
{
// Find the sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
String[] L = new String[n1];
String[] R = new String[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i].compareTo(R[j]) > 0) {
arr[k] = R[j];
j++;
}
else {
arr[k] = L[i];
i++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void sort(String arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void print(String arr[])
{
for (String word : arr){
System.out.print(word + " ");
}
}
//tester method
public static void main(String args[])
{
String[] arr = new String[]{"Cow", "Dog", "Bird", "Sheep", "Goat", "Pig", "Quail", "Crow" };
System.out.println("Given Array:");
print(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length - 1);
System.out.println("\nSorted Array:");
print(arr);
}
}
MergeSort.main(null);