find the index in a sorted array - java

it's a basic problem that given a sorted array and a target value, find the index if the target is found. If not, return the index where it would be if it were inserted in order.
Here is my method:
public int searchInsert1(int[] A, int target) {
if (A == null || A.length == 0)
return 0;
return search(A, target, 0, A.length);
}
public int search(int[] a, int target, int s, int e) {
if (s == e)
return s;
int mid = (s + e) / 2;
if (a[mid] == target) {
return mid;
} else if (target < a[mid]) {
return search(a, target, 0, mid - 1);
} else {
return search(a, target, mid, e);
}
}
somehow, it always returns the wrong index value. However, if I change the if else part as the following, it works:
if (a[mid] == target) {
return mid;
} else if (target > a[mid]) {
return search(a, target, mid + 1, e);
} else {
return search(a, target, 0, mid);
}
can anyone help me with it in detail? Thanks so much in advance

Use this method
public int search(int[] a, int target, int s, int e) {
int low = s;
int high = e- 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < target)
low = mid + 1;
else if (midVal > target)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

Your code has errors in these parts:
1. you have set end index wrong, it should be :A.length - 1;
2. your return value in the condition part is wrong,the return value in your "else" part should be: search(a, target, 0, mid+1, e);
Correct code should be:
public int searchInsert1(int[] A, int target) {
if (A == null || A.length == 0)
return 0;
// check if target is larger than the largest value or smaller than the smallest value
if (target < A[0])
return 0;
else if (target > A[A.length-1])
return A.length-1;
return search(A, target, 0, A.length-1);
}
public int search(int[] a, int target, int s, int e) {
if (s == e)
return s;
int mid = (s + e) / 2;
if (a[mid] == target) {
return mid;
} else if (target < a[mid]) {
return search(a, target, 0, mid - 1);
} else {
return search(a, target, mid+1, e);
}
}

Here is my simplest solution in Java
public static int search(int[] a, int target) {
for (int i = 0; i < a.length; i++) {
if (a[i] == target) {
return i;
}
}
return -1;
}

Related

How can I show the value of array with the Binary Search Tree of Comparison?

I going to do searching the value in the array, did I need to create a method to handle it? For example, the array logged 32,21,13,44,22, and I going to find 22 of the comparison. How can I implement this?
public class binarySearch {
public static void main(String [] args) {
int i = binarySearch(0, new int[]{32,21,13,44,22});
System.out.println("Iterations: " + i);
}
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right) / 2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
} else if (comp > 0) {
left = mid + 1;
} else {
break; // success
}
}
return i;
}
}
My final answer is here. May help you all in the future.
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right) / 2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
} else if (comp > 0) {
left = mid + 1;
} else {
break; // success
}
}
return i;
}
If you have shuffled array, all you can do is go through an array and find your number.
BinarySearch works only with sorted array. I think your solution could look like this:
public static int binarySearch(int[] arr, int key) {
Arrays.sort(arr);
return Arrays.binarySearch(arr, key);
}

Searching in a Bitonic Array becomes infinite loop

I am trying to write a searching algorithm in a bitonic array. Please take a look in the ascending function, where I am printing 'lo'. When I run this code, it keeps print that 'lo' number. I don't understand why it becomes infinite loop there. Here is my code:
public class bitonicArray {
public int ascending(int[] a, int key, int lo, int hi) {
int mid = lo+(hi-lo)/2;
while(lo<=hi) {
if (key<a[mid]) hi = mid-1;
else if (key>a[mid]) {
lo=mid+1;
System.out.println(lo);
}
else return mid;
}
return -1;
}
public int descending(int[] a, int key, int lo, int hi) {
int mid=lo+(hi-lo)/2;
while(lo<=hi) {
if(key<a[mid]) lo = mid+1;
else if(key>a[mid]) {
hi = mid-1;
}
else return mid;
}
return -1;
}
public int bitonicPoint(int[] a) {
int hi = a.length-1;
int lo = 0;
int mid = (hi-lo)/2;
while(mid<=hi) {
if (a[mid-1] < a[mid] && a[mid+1]>a[mid]) mid = mid + 1;
else if (a[mid-1] > a[mid] && a[mid+1]<a[mid]) mid = mid - 1;
else if (a[mid-1]<a[mid] && a[mid+1]<a[mid]) return mid;
}
return -1;
}
public int ind(int[] a, int key) {
int lo = 0;
int hi = a.length-1;
int bit = bitonicPoint(a);
int asc = ascending(a, key, lo, bit-1);
System.out.println(asc);
int desc = descending(a, key, bit+1, hi);
if (asc != -1) {
System.out.println(asc);
return asc;
}
else if (desc != -1) return desc;
else return bit;
}
public static void main(String[] args) {
int n[]= {1,3,4,6,9,14,11,7,2,-4,-9};
bitonicArray ba = new bitonicArray();
System.out.println(ba.ind(n, 6));
}
}
Please help with this. I am pretty new in Java. I am a Python user. Trying to learn Java.
In this part of code fragment you are not updating your mid value
public int ascending(int[] a, int key, int lo, int hi) {
int mid = lo+(hi-lo)/2;
while(lo<=hi) {
if (key<a[mid]) hi = mid-1;
else if (key>a[mid]) {
lo=mid+1;
System.out.println(lo);
}
else return mid;
}
return -1;
}
Initially when this function is called from ind function , value of lo is 0 and hi is 4.
So, mid value is 2.
Because of this in while loop it goes in this part :
else if (key>a[mid]) {
lo=mid+1;
System.out.println(lo);
}
Hence you get your first lo value printed , but then you have updated lo value but not
mid.
Because of which every time mid is at 2 and in while loop each time it goes in same code fragment. You need to update value of mid because it is dependent on lo and hi and in the above else if part you have updated value of lo.
Corrected code is :
public int ascending(int[] a, int key, int lo, int hi) {
int mid = 0;
while(lo<=hi) {
mid = lo+(hi-lo)/2;
if (key<a[mid]) hi = mid-1;
else if (key>a[mid]) {
lo=mid+1;
System.out.println(lo);
}
else return mid;
}
return -1;
}

Binary Search Using Recusion

I am trying to create a binary search algorithm using recursion in Java, when debugging everything seems to be okay up until it finds the value and should return the index of the key needed. However, for some reason it skips the return statement and goes to the bottom return statement.
public int binSearch(int key, int L, int R) {
int mid =(R + L)/2;
if (R < L) {
return -1;
}
if (A[mid] == key) {
return mid;
}
if (key > A[mid]) {
binSearch(key, mid + 1, R);
}
if (key < A[mid]) {
binSearch(key, L, mid - 1);
}
return -1;
}
I was able to salvage this from an old post. I know it doesnt fix your problem, but it shows you another way to solving this problem.
public static int binarySearch(int[] a, int target) {
return binarySearch(a, 0, a.length-1, target);
}
public static int binarySearch(int[] a, int start, int end, int target) {
int middle = (start + end) / 2;
if(end < start) {
return -1;
}
if(target==a[middle]) {
return middle;
} else if(target<a[middle]) {
return binarySearch(a, start, middle - 1, target);
} else {
return binarySearch(a, middle + 1, end, target);
}
}
You are missing some return statements (when you are invoking binSearch recursively)
public int binSearch(int key, int L, int R) {
int mid =(R + L)/2;
if (R < L) {
return -1;
}
if (A[mid] == key) {
return mid;
}
if (key > A[mid]) {
return binSearch(key, mid + 1, R);
}
if (key < A[mid]) {
return binSearch(key, L, mid - 1);
}
return -1;
}

Why there are 2 'illegal start of type' error in my BinarySearch code?

public class Solution {
/*
* #param nums: An integer array sorted in ascending order
* #param target: An integer
* #return: An integer
*/
public int findPosition(int[] nums, int target) {
// write your code here
if (nums.length = 0) {
return -1;
}
int start = 0;
int end = num.length - 1;
int mid;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
} else if (nums[mid] > target) {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
}
return -1;
}
Your code seems to have errors like
if (nums.length = 0) {
return -1;
}
It should have benn
if (nums.length == 0) {
return -1;
}
Also
int end = num.length - 1;
Should have been
int end = nums.length - 1;
Your error less code would look like.
public int findPosition(int[] nums, int target) {
// write your code here
if (nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
int mid;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
} else if (nums[mid] > target) {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}

Java BinarySearch

Can I get some help please? I have tried many methods to get this to work i got the array sorted and to print but after that my binary search function doesnt want to run and give me right results. It always gives me -1. Any help?
public class BinarySearch {
public static final int NOT_FOUND = -1;
public static int binarySearch(double[] a, double key) {
int low = 0;
int high = a.length -1;
int mid;
while (low<=high) {
mid = (low+high) /2;
if (mid > key)
high = mid -1;
else if (mid < key)
low = mid +1;
else
return mid;
}
return NOT_FOUND;
}
public static void main(String[] args) {
double key = 10.5, index;
double a[] ={10,5,4,10.5,30.5};
int i;
int l = a.length;
int j;
System.out.println("The array currently looks like");
for (i=0; i<a.length; i++)
System.out.println(a[i]);
System.out.println("The array after sorting looks like");
for (j=1; j < l; j++) {
for (i=0; i < l-j; i++) {
if (a[i] > a[i+1]) {
double temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (i=0;i < l;i++) {
System.out.println(a[i]);
}
System.out.println("Found " + key + " at " + binarySearch(double a[], key));
}
}
you are not actually comparing with the array values. in
while (low <= high) {
mid = (low + high) / 2;
if (mid > key) {
high = mid - 1;
} else if (mid < key) {
low = mid + 1;
} else {
return mid;
}
}
Instead use this section
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] > key) {
high = mid - 1;
} else if (a[mid] < key) {
low = mid + 1;
} else {
return mid;
}
}
You were correct to find the indexes, but what you were doing is that you were just comparing index number with your key, which is obviously incorrect. When you write a[mid] you will actually compare your key with the number which is at index mid.
Also the last line of code is giving compile error, it should be
System.out.println("Found " + key + " at " + binarySearch(a, key));
Here
if (mid > key)
high = mid -1;
else if (mid < key)
low = mid +1;
else
return mid;
You're comparing index to a value (key) in array. You should instead compare it to a[mid]
And,
System.out.println("Found " + key + " at " + binarySearch(double a[], key));
Should be
System.out.println("Found " + key + " at " + binarySearch(a, key));
public static double binarySearch(double[] a, double key) {
if (a.length == 0) {
return -1;
}
int low = 0;
int high = a.length-1;
while(low <= high) {
int middle = (low+high) /2;
if (b> a[middle]){
low = middle +1;
} else if (b< a[middle]){
high = middle -1;
} else { // The element has been found
return a[middle];
}
}
return -1;
}
int binarySearch(int list[], int lowIndex, int highIndex, int find)
{
if (highIndex>=lowIndex)
{
int mid = lowIndex + (highIndex - lowIndex)/2;
// If the element is present at the
// middle itself
if (list[mid] == find)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (list[mid] > find)
return binarySearch(list, lowIndex, mid-1, find);
// Else the element can only be present
// in right subarray
return binarySearch(list, mid+1, highIndex, find);
}
// We reach here when element is not present
// in array
return -1;
}
I somehow find the iterative version not quite easy to read, recursion makes it nice and easy :-)
public class BinarySearch {
private static int binarySearchMain(int key, int[] arr, int start, int end) {
int middle = (end-start+1)/2 + start; //get index of the middle element of a particular array portion
if (arr[middle] == key) {
return middle;
}
if (key < arr[middle] && middle > 0) {
return binarySearchMain(key, arr, start, middle-1); //recurse lower half
}
if (key > arr[middle] && middle < arr.length-1) {
return binarySearchMain(key, arr, middle+1, end); //recurse higher half
}
return Integer.MAX_VALUE;
}
public static int binarySearch(int key, int[] arr) { //entry point here
return binarySearchMain(key, arr, 0, arr.length-1);
}
}
Here is a solution without heap. The same thing can be done in an array.
If we need to find 'k' largest numbers, we take an array of size 'k' populated with first k items from the main data source. Now, keep on reading an item, and place it in the result array, if it has a place.
public static void largestkNumbers() {
int k = 4; // find 4 largest numbers
int[] arr = {4,90,7,10,-5,34,98,1,2};
int[] result = new int[k];
//initial formation of elems
for (int i = 0; i < k; ++i) {
result[i] = arr[i];
}
Arrays.sort(result);
for ( int i = k; i < arr.length; ++i ) {
int index = binarySearch(result, arr[i]);
if (index > 0) {
// insert arr[i] at result[index] and remove result[0]
insertInBetweenArray(result, index, arr[i]);
}
}
}
public static void insertInBetweenArray(int[] arr, int index, int num) {
// insert num at arr[index] and remove arr[0]
for ( int i = 0 ; i < index; ++i ) {
arr[i] = arr[i+1];
}
arr[index-1] = num;
}
public static int binarySearch(int[] arr, int num) {
int lo = 0;
int hi = arr.length - 1;
int mid = -1;
while( lo <= hi ) {
mid = (lo+hi)/2;
if ( arr[mid] > num ) {
hi = mid-1;
} else if ( arr[mid] < num ) {
lo = mid+1;
} else {
return mid;
}
}
return mid;
}
int BinSearch(int[] array, int size, int value)
{
if(size == 0) return -1;
if(array[size-1] == value) return size-1;
if(array[0] == value) return 0;
if(size % 2 == 0) {
if(array[size-1] == value) return size-1;
BinSearch(array,size-1,value);
}
else
{
if(array[size/2] == value) return (size/2);
else if(array[size/2] > value) return BinSearch(array, (size/2)+1, value);
else if(array[size/2] < value) return (size/2)+BinSearch(array+size/2, size/2, value);
}
}
or
Binary Search in Array
/**
* Find whether 67 is a prime no
* Domain consists 25 of prime numbers
* Binary Search
*/
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
int min = 0,
mid,
max = primes.length,
key = 67,
count= 0;
boolean isFound = false;
while (!isFound) {
if (count < 6) {
mid = (min + max) / 2;
if (primes[mid] == key) {
isFound = true;
System.out.println("Found prime at: " + mid);
} else if (primes[mid] < key) {
min = mid + 1;
isFound = false;
} else if (primes[mid] > key) {
max = mid - 1;
isFound = false;
}
count++;
} else {
System.out.println("No such number");
isFound = true;
}
}
/**
HOPE YOU LIKE IT
A.K.A Binary Search
Take number array of 10 elements, input a number a check whether the number
is present:
**/
package array;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class BinaryS
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
int n=Integer.parseInt(br.readLine());
int a[]={10,20,30,40,50,60,70,80,90,100};
int upper=a.length-1,lower=0,mid;
boolean found=false;
int pos=0;
while(lower<=upper)
{
mid=(upper+lower)/2;
if(n<a[mid])upper=mid-1;
else if(n>a[mid])lower=mid+1;
else
{
found=true;
pos=mid;
break;
}
}
if(found)System.out.println(n+" found at index "+pos);
else System.out.println(n+" not found in array");
}
}
Well I know I am posting this answer much later.
But according to me its always better to check boundary condition at first.
That will make your algorithm more efficient.
public static int binarySearch(int[] array, int element){
if(array == null || array.length == 0){ // validate array
return -1;
}else if(element<array[0] || element > array[array.length-1]){ // validate value our of range that to be search
return -1;
}else if(element == array[0]){ // if element present at very first element of array
return 0;
}else if(element == array[array.length-1]){ // if element present at very last element of array
return array.length-1;
}
int start = 0;
int end = array.length-1;
while (start<=end){
int midIndex = start + ((end-start)/2); // calculate midIndex
if(element < array[midIndex]){ // focus on left side of midIndex
end = midIndex-1;
}else if(element > array[midIndex]){// focus on right side of midIndex
start = midIndex+1;
}else {
return midIndex; // You are in luck :)
}
}
return -1; // better luck next time :(
}
static int binarySearchAlgorithm() {
// Array should be in sorted order. Mandatory requirement
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int lowIndex = 0;
int valueToFind = 8;
int highIndex = a.length - 1;
while (lowIndex <= highIndex) {
//Finding the midIndex;
int midIndex = (highIndex + lowIndex) / 2;
// Checking if midIndex value of array contains the value to be find.
if (a[midIndex] == valueToFind) {
return midIndex;
}
// Checking the mid Index value is less than the value to be find.
else if (a[midIndex] < valueToFind) {
// If Yes, changing the lowIndex value to midIndex value + 1;
lowIndex = midIndex + 1;
} else if (a[midIndex] > valueToFind) {
// If Yes, changing the highIndex value to midIndex value - 1;
highIndex = midIndex - 1;
} else {
return -1;
}
}
return -1;
}

Categories