K’th Smallest/Largest Element in Unsorted Array

quick select   avg O(n), worst O(n^2)

Kth smallest element in Unsorted Array

#quickselect
import random
random.seed(5)
def partition(a):
    """
      Usage: (left,pivot,right) = Partition(array)
      Partitions an array around a randomly chosen pivot such that
      left elements <= pivot <= right elements.
      Running time: O(n)
      """
    if len(a)==1:
        return ([],a[0],[])
    if len(a)==2:
        if a[0]<=a[1]:
            return ([],a[0],a[1])
        else:
            return ([],a[1],a[0])
    #choose a random pivot
    p=random.randint(0,len(a)-1)#the pivot index
    pivot=a[p]
    right=[]
    left=[]
    for i in range(len(a)):
        if not i==p:
            if a[i]>pivot:
                right.append(a[i])
            else:
                left.append(a[i])
    return (left,pivot,right)
def quickselect(a,k):
    """
     Usage: kth_smallest_element = QuickSelect(array,k)
     Finds the kth smallest element of an array in linear time.
     """
    (left,pivot,right)=partition(a)
    print((left,pivot,right))
    if len(left)==k-1:
        result=pivot
    elif len(left)>k-1:
        result=quickselect(left,k)
    else:
        result=quickselect(right,k-len(left)-1)
    return result

def mainqs():
    N=10
    k=4
    a=[random.randint(1,100) for i in range(N)]
    print('input array:',a)
    print('k=',k)
    b=quickselect(a,k)
    print('kth smallest element:',b)

mainqs()

kth largest 

def partition1(a):
    """
      Usage: (left,pivot,right) = Partition(array)
      Partitions an array around a randomly chosen pivot such that
      left elements <= pivot <= right elements.
      Running time: O(n)
      """
    if len(a)==1:
        return ([],a[0],[])
    if len(a)==2:
        if a[0]>=a[1]:
            return ([],a[0],a[1])
        else:
            return ([],a[1],a[0])
    #choose a random pivot
    p=random.randint(0,len(a)-1)#the pivot index
    pivot=a[p]
    right=[]
    left=[]
    for i in range(len(a)):
        if not i==p:
            if a[i]<=pivot:
                right.append(a[i])
            else:
                left.append(a[i])
    return (left,pivot,right)

def quickselect1(a,k):
    """
     Usage: kth_smallest_element = QuickSelect(array,k)
     Finds the kth smallest element of an array in linear time.
     """
    (left,pivot,right)=partition1(a)
    print((left,pivot,right))
    if len(left)==k-1:
        result=pivot
    elif len(left)>k-1:
        result=quickselect1(left,k)
    else:
        result=quickselect1(right,k-len(left)-1)
    return result

def mainqs1():
    N=10
    k=4
    a=[random.randint(1,100) for i in range(N)]
    print('input array:',a)
    print('k=',k)
    b=quickselect1(a,k)
    print('kth largest element:',b)

mainqs1()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值