From 01e5b8099d70e22f8158b88d2b0380d3f1516497 Mon Sep 17 00:00:00 2001 From: Anand Girish Parthasarathy Date: Fri, 20 Sep 2024 17:11:25 -0700 Subject: [PATCH] Saving work in progress for srrted rotated array debug --- .../src/io/anand/play/SearchRotatedArray.java | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/play/src/io/anand/play/SearchRotatedArray.java b/play/src/io/anand/play/SearchRotatedArray.java index 2700d80..ceed91c 100644 --- a/play/src/io/anand/play/SearchRotatedArray.java +++ b/play/src/io/anand/play/SearchRotatedArray.java @@ -157,7 +157,7 @@ private static int binSearchIter(int[] a, int key) { searchFirstHalf = true; } else if (a[mid] <= a[end]) { // If the second half is sorted and our key value is out of range - if ((key > a[end]) || (key < a[mid])) + if ((key >= a[end]) || (key < a[mid])) searchFirstHalf = true; } else // Technically not valid since array is sorted at-least in one of the halves. @@ -177,6 +177,44 @@ private static int binSearchIter2(int[] a, int key) { int end = a.length - 1; int start = 0; + // Edge cases + if (1 == a.length) + if (a[0] == key) + return 0; + // Search for the value for the modpoint and if less than midpoint + // search within theat section, else the half after mid point + // + while (start <= end) { + int mid = start + (end - start) / 2; + System.out.println("start: " + start + ", end: " + end + ", mid: " + mid + ", value[mid]: " + a[mid]); + + // If the value is at any one of the locations we have access to, just check and be done with it. + if (a[mid] == key) + return mid; + + // Start with base assumption, Of course we will validate further + boolean searchFirstHalf = false; + if ( ((a[start] < a[mid]) && (key >= a[start]) && (key < a[mid])) + || ((a[mid] < a[end]) && (key < a[mid]) && (key > a[end])) ) + // If the first half is sorted and key value is in this range OR + // second half is sorted and key is out of that range + // pick the first half to search + searchFirstHalf = true; + + // Adjust the next search indices + if (searchFirstHalf) + end = mid - 1; + else + start = mid + 1; + } + return -1; + } + + + private static int binSearchIter3(int[] a, int key) { + int end = a.length - 1; + int start = 0; + // Edge cases if (1 == a.length) if (a[0] == key) @@ -198,8 +236,8 @@ private static int binSearchIter2(int[] a, int key) { // If the first half is sorted and key value is in this range searchFirstHalf = true; } else if ((a[mid] <= a[end]) && ((key < a[mid]) || (key > a[end]))) { - // If the second half is sorted and key value is out of this range - searchFirstHalf = true; + // If the second half is sorted and key value is out of this range + searchFirstHalf = true; } // Adjust the next search indices @@ -210,11 +248,12 @@ private static int binSearchIter2(int[] a, int key) { } return -1; } + private static boolean useRecursion = false; private static int binarySearchRotated (int [] a, int key) { if (useRecursion) return binSearchRecurse(a, key, 0, a.length - 1); - return binSearchIter(a, key); + return binSearchIter2(a, key); } public static void main(String []args){