Saturday 15 June 2013

c++ - k-rotated-shifted array and finding x? -


We say that a k-rotated-shifted array , for an array that is k Rotate can be sorted with the example: A = [10, 15, 20, 1, 7] , with k = 2 . I propose an algorithm to find a key similar to x in this horizontal-shifted array.

Can anyone help me confirm this?

Enter image details here

Edit 1: My The new code is:

  int S (int l, int r, int x) {int f = a [l], m = a [(l + r) / 2], la = A [l]; If (x == f or x == m or x == la) returns 1; Other (if & lt; f and x & gt; m) || (X> f and x & gt; m) || (X & lt; f & x & lt; m)) return s ((L + R) / 2, r, x); And return S (L, (L + R) / 2, X); }}  

Breaks 1 search in 3,4,5,1,2 Breaks in search of the key, which is not present, because the case of the base is triggered only when the keys are found, which has an infinite loop.

To fix, use the status

  before & lt; Middle & lt; X or Middle & lt; X & lt; First or X & lt; First & lt; Middle  

Also keep in mind that if we take the (i + j) / 2 roof, then there is no need to compare the previous is.

>

No comments:

Post a Comment