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;
}
Related
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);
}
My problem statement is this -
Find the count of numbers in a sorted array that are less than a given number, and this should be done efficiently with respect to time. I wrote a program using binary search that gets the count but time complexity wise it's failing. Need help in achieving this.
import java.util.Arrays;
public class SortedSearch {
public static int countNumbers(int[] sortedArray, int lessThan) {
if(sortedArray.length ==1 || sortedArray.length == 0) {
return singleElement(sortedArray, lessThan);
}
else {
return binarySearch(sortedArray, lessThan);
}
}
public static int singleElement(int[] sortedArray, int searchVal) {
if(sortedArray.length == 0) {
return 0;
}
if(sortedArray[0] < searchVal) {
return 1;
}
return 0;
}
private static int binarySearch(int[] sortedArray, int searchVal) {
int low = 0;
int high = (sortedArray.length)-1;
int mid = (low + high)/2;
if((sortedArray.length == 0) || (sortedArray[0] > searchVal)) {
return 0;
}
if(sortedArray[high] < searchVal) {
return sortedArray.length;
}
if(sortedArray[high] == searchVal) {
return sortedArray.length-1;
}
if(sortedArray[mid] < searchVal) {
int newLow = low;
int newHigh = calculateNewHigh(sortedArray, newLow, 0, searchVal);
int[] newArray = Arrays.copyOfRange(sortedArray, newLow, newHigh+1);
return newArray.length;
}
else {
int newLow = low;
int newHigh = mid;
int[] newArray = Arrays.copyOfRange(sortedArray, newLow, newHigh+1);
return binarySearch(newArray, searchVal);
}
}
private static int calculateNewHigh(int[] sortedArray, int low, int previousHigh, int searchVal) {
int newHigh = previousHigh + (sortedArray.length-low)/2;
if(sortedArray[newHigh] < searchVal) {
newHigh = calculateNewHigh(sortedArray, newHigh, newHigh, searchVal);
}
if(sortedArray[newHigh] == searchVal) {
newHigh--;
}
if(sortedArray[newHigh] > searchVal) {
newHigh--;
}
return newHigh;
}
public static void main(String[] args) {
System.out.println(SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4));
}
}
Since you're using Arrays anyway, way not use the Arrays.binarySearch(int[] a, int key) method, instead of attempting to write your own?
public static int countNumbers(int[] sortedArray, int lessThan) {
int idx = Arrays.binarySearch(sortedArray, lessThan);
if (idx < 0)
return -idx - 1; // insertion point
while (idx > 0 && sortedArray[idx - 1] == lessThan)
idx--;
return idx; // index of first element with given value
}
The while loop1 is necessary because the javadoc says:
If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
1) This loop is not optimal if e.g. all values are the same, see e.g. Finding multiple entries with binary search
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;
}
I have this java code for QuickSort that works if there are no duplicates, however if there are any duplicates, the QuickSort fails. For example, if I want to QuickSort {5,3,3,1,7} my code will output {1,3,3,7,5}, and I can't seem to figure out why this is the case.
public static void quickSort(Integer[] nums) {
quickSort(nums, 0, nums.length-1);
}
private static void quickSort(Integer[] ary, int lo, int hi) {
//pick num # lo to be pivot
int pivot = lo;
int i = lo+1;
int j = hi;
if( lo==hi) {
return;
}
while(i <j) {
if(ary[i].compareTo(ary[pivot]) <=0 ) {
i++;
}
else if(ary[j].compareTo(ary[pivot]) >=0 ) {
j--;
}
else {
int temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
if(i == hi && j == hi) {
if(ary[pivot].compareTo(hi) > 0) {
int temp = ary[pivot];
ary[pivot] = ary[hi];
ary[hi] = temp;
pivot = hi;
}
else {
int temp1 = ary[pivot];
ary[pivot] = ary[i-1];
ary[i-1] = temp1;
pivot = i-1;
}
}
if(lo < pivot -1) {
quickSort(ary, lo, pivot-1);
}
if(pivot +1 < hi) {
quickSort(ary, pivot+1, hi);
}
}
If anyone could tell me what I'm doing wrong, that would be greatly appreciated!
Hi i have modified your code, please check corresponding comments
private static void quickSort(Integer[] ary, int lo, int hi) {
//pick num # lo to be pivot
int pivot = lo;
int i = lo+1;
int j = hi;
if( lo==hi) {
return;
}
//while(i <j) {
for(;;){//change from while to infinite for
while(ary[i].compareTo(ary[pivot]) <=0 && i<hi ) {//changed from if to while with boundary conditions
i++;
}
while(ary[j].compareTo(ary[pivot]) >0 && j>lo) { //change from if to while with boundary conditions and it is not >=0 only >
j--;
}
if(i<j){ //changed from else to if
int temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}else{//added else block
break;
}
}
//you didn't handled i>j condition properly i.e when i>j you need to swap pivot and i-1
int temp1 = ary[pivot];
ary[pivot] = ary[i-1];
ary[i-1] = temp1;
pivot = i-1;
//Not required
/*if(i == hi && j == hi) {
if(ary[pivot].compareTo(hi) > 0) {
int temp = ary[pivot];
ary[pivot] = ary[hi];
ary[hi] = temp;
pivot = hi;
}
else {
int temp1 = ary[pivot];
ary[pivot] = ary[i-1];
ary[i-1] = temp1;
pivot = i-1;
}
}*/
if(lo < pivot -1) {
quickSort(ary, lo, pivot-1);
}
if(pivot +1 < hi) {
quickSort(ary, pivot+1, hi);
}
}
Thanks
if you want to quicksort use the algorithm from this site.
Quicksort
It works for me and the explanation is quite good I think.
I'm reading in a text file of unsorted numbers
static void insertInOrder( int[] arr, int cnt, int newVal )
{
int belongs = -( bSearch( arr, 0, arr.length-1, newVal)) - 1;
{
for ( int i = cnt; i >= belongs+1 ; --i)
{
arr[i] = arr[i-1];
}
arr[belongs] = newVal;
}
}
// We do not need to pass in count. The incoming lo and hi define the range
public static int bSearch(int[] a, int lo, int hi, int key)
{
int mid = (lo+hi)/2;
if(lo>hi)
return -1;
else if (a[mid]==key)
return mid;
else if (a[mid]<key)
return bSearch(a, mid+1, hi, key);
else
return bSearch(a, lo, mid-1, key);
}
The binary search is working, but my insertinorder isn't putting the numbers in ascending order and I can't figure out why. It's just printing the list out backwards. And this has to be done using a recursive binary search in an insertinorder.
Have you copied the latest code in to the question ???.
One obvious problem is:
int belongs = -( bSearch( arr, 0, arr.length-1, newVal)) - 1;
the method bSearch returns -1 when a value is not found in the array. This will give an insertion point of -2 when newValue is not found. You need to change bSearch to return both wether the item was found and the position. Two options
Create (and return) a new class
private class FoundResult {
boolean found = true;
int position;
}
When not found; return -hi-1
i.e. in bSearch some thing like:
if(lo>hi)
return -hi-1;
For option 1, the code will be roughly (i have not tested this):
static void insertInOrder( int[] arr, int cnt, int newVal ) {
int belongs = bSearch( arr, 0, arr.length-1, newVal)).position ;
for ( int i = cnt; i >= belongs+1 ; --i) {
arr[i] = arr[i-1];
}
arr[belongs] = newVal;
}
// We do not need to pass in count. The incoming lo and hi define the range
public static FoundResult bSearch(int[] a, int lo, int hi, int key)
{
int mid = (lo+hi)/2;
FoundResult ret;
if(lo>hi) {
ret = new FoundResult();
ret.found = false;
ret.position = hi;
return ret;
} else if (a[mid]==key) {
ret = new FoundResult();
ret.position = hi;
return ret;
} else if (a[mid]<key) {
return bSearch(a, mid+1, hi, key);
} else {
return bSearch(a, lo, mid-1, key);
}
For option 2 the code is roughly:
static void insertInOrder( int[] arr, int cnt, int newVal ) {
int belongs = bSearch( arr, 0, arr.length-1, newVal)) ;
if (belongs < 0) {
belongs = -1 - belongs ;
}
for ( int i = cnt; i >= belongs+1 ; --i) {
arr[i] = arr[i-1];
}
arr[belongs] = newVal;
}
// We do not need to pass in count. The incoming lo and hi define the range
public static int bSearch(int[] a, int lo, int hi, int key)
{
int mid = (lo+hi)/2;
FoundResult ret;
if(lo>hi) {
return - hi - 1;
} else if (a[mid]==key) {
return hi;
} else if (a[mid]<key) {
return bSearch(a, mid+1, hi, key);
} else {
return bSearch(a, lo, mid-1, key);
}