# 78-在旋转数组中查找元素

有序数组被旋转了，查找其中元素

```python
class Solution:
    def search(self, A, target):
        if A is None:
            return -1
        start = 0
        end = len(A) - 1
        mid = 0
        while start + 1 < end:
            # 取中点
            mid = start + int((end - start) / 2)
            # 如果找到直接返回
            if target == A[mid]:
                return mid
            # 这一步很关键，可以区分mid在旋转后两段有序数组中的哪一段
            if A[start] < A[mid]:
                # 关键，显然此时target在mid左边，那么缩小区间为end = mid
                if A[start] <= target and target < A[mid]:
                    end = mid
                else:  # 显然此时target在mid右，那么缩小区间为 start = mid
                    start = mid
            else:
                # 关键
                if A[mid] < target and target <= A[end]:
                    start = mid
                else:
                    end = mid
        # 此时就剩start、end两个点有可能了
        if A[start] == target:
            return start
        if A[end] == target:
            return end
        return -1


s = Solution()
print(s.search([8, 9, 10, 11, 12, 13, 14, 4, 5, 6,7], 6))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/sou-suo-cha-zhao-lei/78-zai-xuan-zhuan-shu-zu-zhong-cha-zhao-yuan-su.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
