Py这道题感觉过不了。啥方法都用了。
查看原帖
Py这道题感觉过不了。啥方法都用了。
707862
s_a_b_e_ryyds楼主2022/4/26 00:02

sort()

n,m=map(int,input().strip().split())
ls=[int(i) for i in input().split()]
ls.sort()
print(ls[m])

快速排序

def MovePivot(arr,low,high):
    Pivot=arr[high]
    imove=low-1
    for i in range(low,high):
        if arr[i]<=Pivot:
            imove+=1
            arr[imove],arr[i]=arr[i],arr[imove]
    arr[imove+1],arr[high]=arr[high],arr[imove+1]
    return imove+1
def QuickSort(arr,low,high):
    if low<high:
        pivot=MovePivot(arr,low,high)
        QuickSort(arr,low,pivot-1)
        QuickSort(arr,pivot+1,high)

n,m=map(int,input().strip().split())
ls=[int(i) for i in input().split()]
QuickSort(ls,0,n-1)
print(ls[m])

归并排序

def MergeSort(arr):
    if len(arr) <= 1:
        return arr
    mid=len(arr)//2
    left=MergeSort(arr[:mid])
    right=MergeSort(arr[mid:])
    return Merge(left,right)
def Merge(left,right):
    r,l=0,0
    temp=[]
    lmax=len(left)
    rmax=len(right)
    while l<lmax and r<rmax:
        if left[l]<=right[r]:
            temp.append(left[l])
            l+=1
        else:
            temp.append(right[r])
            r+=1
    temp+=list(left[l:])
    temp+=list(right[r:])
    return temp
n,m=map(int,input().strip().split())
ls=[int(i) for i in input().strip().split()]
sl=MergeSort(ls)
print(sl[m])
2022/4/26 00:02
加载中...