# 分治法思想-找出第k小元

```python
'''
思路：拆成多个lower,pi,high划分。lower中的比pi小，high中的比pi大
'''
def partition(seq):
    '''
    序列转成左，中，右
    '''
    pi=seq[0]
    lower=[x for x in seq[1:] if x<=pi]
    high=[x for x in seq[1:] if x>pi]
    print(lower,pi,high)
    return lower,pi,high

def search(seq,k):
    lower,pi,high=partition(seq)
    m=len(lower)
    if m==k-1:
        return pi
    elif m<k-1:
        return search(high,k-1-m)
    else:
        return search(lower,k)

data=[4,1,6,3,7,9,13,93,0,100,2]
print(search(data,8))
print(sorted(data))
```

```
[1, 3, 0, 2] 4 [6, 7, 9, 13, 93, 100]
[] 6 [7, 9, 13, 93, 100]
[] 7 [9, 13, 93, 100]
[] 9 [13, 93, 100]
9
[0, 1, 2, 3, 4, 6, 7, 9, 13, 93, 100]
```


---

# 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/si-xiang-lei/fen-zhi-fa/zhao-chu-di-k-xiao.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.
