python中位数代码_中位数选择python

我正在实现Select算法(又称确定性选择)。我已经让它适用于小数组/列表,但是当我的数组大小超过26时,它会出现以下错误:“RuntimeError:maximum recursion depth exceeded”。对于大小为25及以下的阵列没有问题。在

我的最终目标是让它在大小为500的数组中运行并进行多次迭代。迭代不是问题。我已经研究过StackOverflow,并看到了许多其他文章:Python implementation of "median of medians" algorithm。我有一种预感,我随机生成的数组中的重复可能导致了一个问题,但似乎并不是这样。在

我的代码是:import math

import random

# Insertion Sort Khan Academy video: https://www.youtube.com/watch?v=6pyeMmJTefg&list=PL36E7A2B75028A3D6&index=22

def insertion_sort(A): # Sorting it in place

for index in range(1, len(A)):# range is up to but not including len(A)

value = A[index]

i = index - 1 # index of the item that is directly to the left

while i >= 0:

if value < A[i]:

A[i + 1] = A[i]

A[i] = value

i = i - 1

else:

break

timeslo = 0 # I think that this is a global variable

def partition(A, p):

global timeslo

hi = [] #hold things larger than our pivot

lo = [] # " " smaller " " "

for x in A: # walk through all the elements in the Array A.

if x

lo = lo + [x]

timeslo = timeslo + 1 #keep track no. of comparisons

else:

hi = hi + [x]

return lo,hi,timeslo

def get_chunks(Acopy, n):

# Declare some empty lists to hold our chunks

chunk = []

chunks = []

# Step through the array n element at a time

for x in range(0, len(Acopy), n): # stepping by size n starting at the beginning

# of the array

chunk = Acopy[x:x+n] # Extract 5 elements

# sort chunk and find its median

insertion_sort(chunk) # in place sort of chunk of size 5

# get the median ... (i.e. the middle element)

# Add them to list

mindex = (len(chunk)-1)/2 # pick middle index each time

chunks.append(chunk[mindex])

# chunks.append(chunk) # assuming subarrays are size 5 and we want the middle

# this caused some trouble because not all subarrays were size 5

# index which is 2.

return chunks

def Select(A, k):

if (len(A) == 1): # if the array is size 1 then just return the one and only element

return A[0]

elif (len(A) <= 5): # if length is 5 or less, sort it and return the kth smallest element

insertion_sort(A)

return A[k-1]

else:

M = get_chunks(A, 5) # this will give you the array of medians,,, don't sort it....WHY ???

m = len(M) # m is the size of the array of Medians M.

x = Select(M, m/2)# m/2 is the same as len(A)/10 FYI

lo, hi, timeslo = partition(A, x)

rank = len(lo) + 1

if rank == k: # we're in the middle -- we're done

return x, timeslo # return the value of the kth smallest element

elif k < rank:

return Select(lo, k) # ???????????????

else:

return Select(hi, k-rank)

################### TROUBLESHOOTING ################################

# Works with arrays of size 25 and 5000 iterations

# Doesn't work with " 26 and 5000 "

#

# arrays of size 26 and 20 iterations breaks it ?????????????????

# A = []

Total = 0

n = input('What size of array of random #s do you want?: ')

N = input('number of iterations: ')

# n = 26

# N = 1

for x in range(0, N):

A = random.sample(range(1,1000), n) # make an array or list of size n

result = Select(A, 2) #p is the median of the medians, 2 means the 3rd smallest element

Total = Total + timeslo # the total number of comparisons made

print("the result is"), result

print("timeslo = "), timeslo

print("# of comparisons = "), Total

# A = [7, 1, 3, 5, 9, 2, 83, 8, 4, 13, 17, 21, 16, 11, 77, 33, 55, 44, 66, 88, 111, 222]

# result = Select(A, 2)

# print("Result = "), result

任何帮助都将不胜感激。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值