sorted and shifted array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vols

    sorted and shifted array

    I found the following question, and couldn't get the best answer:

    if you have a sorted and shifted array such like {5,6,7,1,2,3,4} , how
    to find one element "2"?

    Thanks.
    Vol
  • Pete Becker

    #2
    Re: sorted and shifted array

    On 2008-05-31 23:03:13 -0400, Vols <volunteers@gma il.comsaid:
    I found the following question, and couldn't get the best answer:
    >
    if you have a sorted and shifted array such like {5,6,7,1,2,3,4} , how
    to find one element "2"?
    >
    It's the fourth element. Beyond that, the problem is seriously underspecified.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)

    Comment

    • Eric Pruneau

      #3
      Re: sorted and shifted array


      "Vols" <volunteers@gma il.coma écrit dans le message de news:
      d816aa69-6213-49a2-ad91-c977704e7e96...l egroups.com...
      >I found the following question, and couldn't get the best answer:
      >
      if you have a sorted and shifted array such like {5,6,7,1,2,3,4} , how
      to find one element "2"?
      >
      Thanks.
      Vol
      Check out algorithms like

      std::find, std::find_if

      int arr[5] = {1,2,3,4,5}
      int* it = std::find(arr, arr+5, 2); // it now point to arr[1]

      and you can use std::distance to obtain the position of the element


      Comment

      • kongweize@gmail.com

        #4
        Re: sorted and shifted array

        On Jun 1, 11:03 am, Vols <volunte...@gma il.comwrote:
        I found the following question, and couldn't get the best answer:
        >
        if you have a sorted and shifted array  such like {5,6,7,1,2,3,4} , how
        to find one element "2"?
        >
        Thanks.
        Vol
        the sorted and shifted array contains 2 sorted subsequence
        first, find the 2 subsequence
        then check which subsequence is the element in, and do binary search

        Comment

        • Jim Langston

          #5
          Re: sorted and shifted array

          Vols wrote:
          I found the following question, and couldn't get the best answer:
          >
          if you have a sorted and shifted array such like {5,6,7,1,2,3,4} , how
          to find one element "2"?
          Your requirements are not clear. Do you need to find the element that
          contains the value 2, or the element that was orignally the 2nd element
          before the shift?

          --
          Jim Langston
          tazmaster@rocke tmail.com


          Comment

          Working...