Re: [algogeeks] Re: logic???????????

2011-08-13 Thread Kunal Patil
@Yasir: Yups...I also have the same algo...
Just to improvise your solution, you need not do binary search on both sides
of the pivot.
Just check End points (min-max) of the both sub-array to decide which side
to do a binary search..This works in the case of duplicate elements too.

for e.g.
2  5  8  12  45  78  91  100   101  104
k = 5
78  91  100   101  104  2  5  8  12  45
Pivot is at 2
You want to search for 12.
Check end points of both the sub-array (78,104  2,45)
Second range would contain 12.
So no need to call binSearch on first sub-array.

2  2  5  5  7
k = 3
So new arr is  5  7  2  2  5
Pivot is first occurrence of 2
You want to search for 5 (which is in both parts)
check end points to decide which sub-array to be searched.
While checking end-points itself you get the answer.

I hope you get it...

On Fri, Aug 12, 2011 at 11:37 PM, Yasir yasir@gmail.com wrote:

 how abt  http://ideone.com/lN2og ??





  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/pZdvFb_t2b4J.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: logic???????????

2011-08-13 Thread Yasir
Yeah got it. Thanks :)

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/NAqtuyQmehgJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: logic???????????

2011-08-12 Thread sagar pareek
nope it will not work
top kya lower part bhi sorted hai..

like 7 8 9 1 2 3 4 5 6
do it with this i/p... it will fail
ur algo will do same thing for 8 and 4

FAIL !!!

On Fri, Aug 12, 2011 at 9:27 PM, KK kunalkapadi...@gmail.com wrote:

 Check this out

 //But this and fails for input such as: 101 ie all nos
 must be unique..
 int search_element_in_rotated_array(int *a, int low, int high, int
 key)
 {
while(low = high)
{
  int mid = low + (high - low)/2;

  if(a[mid] == key)
  return mid;

  if(a[mid] = a[low])   // the top part is sorted
  {
if(a[low] = key  key  a[mid])
high = mid - 1;
else
low = mid + 1;
  }
  else
  {
if(key = a[high]  key  a[mid])
low = mid + 1;
else
high = mid - 1;
  }
}

return -1;
 }

 On Aug 12, 8:38 pm, sagar pareek sagarpar...@gmail.com wrote:
  oh common shashank...its not that easy u told.
  just do codingthen u will find the error in ur logic
 
  On Fri, Aug 12, 2011 at 6:25 PM, WgpShashank shashank7andr...@gmail.com
 wrote:
 
 
 
 
 
 
 
 
 
   Hi Guys Here is the algorithm for doing same in O(logn)
 
   Hint :  Find the pivot point, divide the array in two sub-arrays and
 call
   binary search. Thats It :)
 
   Algorithm
   1. A Pivot Point is point around which array is rotated so 1st we need
 to
   find out its location/index. lets say it pivot.
 a. to find pivot point we use same idea of binary search   check
 if
   arr[mid]a[mid+1] if true then return pivot=mid.
 b  else if arr[low]a[mid] search for pivot in left
 c. else search for pivot in right half of array.
Time Complexity O(logn)
 
   2.  Once we have found index of pivot point check if desired element is
   same value at pivot index or not e.g.
   a. ( if a[pivot]==value we are searching for ) the simply return
 true
   or 1 .
 Now call binary search for one of the two sub-arrays.
(ab) *If *element is greater than 0th element then  search in
   left array
(ac) *Else* Search in right array  *
(ad) If *element is found in selected sub-array then return
   index
   *Else *return -1.
 
Again Time Complexity O(logn)
 
   Hope You Can Make it in Running Code , Do Noify me if You Need More
   Explanation or if missed Something??
 
   *Regards
   Shashank Mani Computer Science Is Awesome So Why I Write Code
   Computer Science
   Birla institute of Technology Mesra
   *
 
--
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To view this discussion on the web visit
  https://groups.google.com/d/msg/algogeeks/-/QPCzNLNf_sEJ.
 
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
  --
  **Regards
  SAGAR PAREEK
  COMPUTER SCIENCE AND ENGINEERING
  NIT ALLAHABAD

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
**Regards
SAGAR PAREEK
COMPUTER SCIENCE AND ENGINEERING
NIT ALLAHABAD

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: logic???????????

2011-08-12 Thread Yasir
how abt  http://ideone.com/lN2og ??





-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/pZdvFb_t2b4J.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.