Problem:
Lets say there is a sorted array, but it is rotated by some positions.
Now we need to search for an element with O(log n) time complexity.
Solution:
If the array is completely sorted, then we can apply binary search on it.
But here it is not the case, but still we can use the rotation information and apply modified binary search.
Divide the array into two parts wrt middle element.
Now at this point, one of the left/right sub arrays, one sub array should be sorted and other is not.
We can use this information to solve the problem.
Algorithm:
Find the Middle element
If the data is equal to middle then return
else if (ar[left] < ar[mid]) { //means left array is sorted
if(data >= ar[left] and data < ar[mid])
high = mid - 1;
else //search in the right sub array
low = mid + 1;
} else { //right array is sorted
if(data > ar[mid] && data <= ar[high])
low = mid + 1;
else
high = mid - 1;
}
Code:
import java.util.Arrays;
class RotatedSortedAr {
static public int rotatedBinSearch(int[] ar, int data) {
int low = 0, high = ar.length - 1;
int mid = -1;
while(low <= high) {
mid = low + ((high-low)/2);
if(ar[mid] == data)
return mid;
if(ar[low] < ar[mid-1]) {
//left array is sorted
if(data >= ar[low] && data < ar[mid])
high = mid - 1;
else
low = mid + 1;
} else {
//right array is sorted
if(data > ar[mid] && data <= ar[high])
low = mid + 1;
else
high = mid - 1;
}
}
return mid;
}
static public void main(String[] args) {
int[] ar = {4, 5, 6, 7, 8, 9, 1, 2, 3};
int key = 3;
int pos = rotatedBinSearch(ar, key);
System.out.println(Arrays.toString(ar));
System.out.println("Position of " + key + " is: " + pos);
}
}
Time Complexity:
O (log n)
Lets say there is a sorted array, but it is rotated by some positions.
Now we need to search for an element with O(log n) time complexity.
Solution:
If the array is completely sorted, then we can apply binary search on it.
But here it is not the case, but still we can use the rotation information and apply modified binary search.
Divide the array into two parts wrt middle element.
Now at this point, one of the left/right sub arrays, one sub array should be sorted and other is not.
We can use this information to solve the problem.
Algorithm:
Find the Middle element
If the data is equal to middle then return
else if (ar[left] < ar[mid]) { //means left array is sorted
if(data >= ar[left] and data < ar[mid])
high = mid - 1;
else //search in the right sub array
low = mid + 1;
} else { //right array is sorted
if(data > ar[mid] && data <= ar[high])
low = mid + 1;
else
high = mid - 1;
}
Code:
import java.util.Arrays;
class RotatedSortedAr {
static public int rotatedBinSearch(int[] ar, int data) {
int low = 0, high = ar.length - 1;
int mid = -1;
while(low <= high) {
mid = low + ((high-low)/2);
if(ar[mid] == data)
return mid;
if(ar[low] < ar[mid-1]) {
//left array is sorted
if(data >= ar[low] && data < ar[mid])
high = mid - 1;
else
low = mid + 1;
} else {
//right array is sorted
if(data > ar[mid] && data <= ar[high])
low = mid + 1;
else
high = mid - 1;
}
}
return mid;
}
static public void main(String[] args) {
int[] ar = {4, 5, 6, 7, 8, 9, 1, 2, 3};
int key = 3;
int pos = rotatedBinSearch(ar, key);
System.out.println(Arrays.toString(ar));
System.out.println("Position of " + key + " is: " + pos);
}
}
Time Complexity:
O (log n)
No comments:
Post a Comment