python最大值最小值函数,Python中列表的最小值和最大值(不使用min / max函数)

I was wondering if there is a way to find min & max of a list without using min/max functions in Python. So i wrote a small code for the same using recursion. My logic is very naive: I make two stacks (min_stack and max_stack) which keep track of minimum and maximum during each recursive call. I have two questions:

Could somebody help me estimate the complexity of my code?

Is there a better way to do this? Will sorting the list using mergesort/quicksort and picking up first and last element give a better performance?

Thank you

Here is my attempt in Python:

minimum = []

maximum = []

# Defining Stack Class

class Stack:

def __init__(self) :

self.items = []

def push(self, item) :

self.items.append(item)

def pop(self) :

return self.items.pop()

def access(self, index):

return self.items[index]

def isEmpty(self) :

return (self.items == [])

def length(self):

return len(self.items)

def minmax(input_list):

# make two stacks, one for min and one for max

min_stack = Stack()

max_stack = Stack()

# comparing the first two elements of the list and putting them in appropriate stack

if input_list[0]

min_stack.push(input_list[0])

max_stack.push(input_list[1])

else:

max_stack.push(input_list[0])

min_stack.push(input_list[1])

# Pushing remaining elements of the list into appropriate stacks.

for i in range(2, len(input_list)):

if input_list[i] < min_stack.access(-1):

min_stack.push(input_list[i])

else:

max_stack.push(input_list[i])

# to find minimum

minlist = []

while min_stack.length() > 0:

minlist.append(min_stack.pop())

# to find maximum

maxlist = []

while max_stack.length() > 0:

maxlist.append(max_stack.pop())

if len(minlist) > 1:

minmax(minlist)

else:

minimum.append(minlist)

if len(maxlist) > 1:

minmax(maxlist)

else:

maximum.append(maxlist)

def main():

input_list = [2, 0, 2, 7, 5, -1, -2]

print 'Input List is: ', input_list

minmax(input_list)

print 'Global Minimum is: ', minimum[0]

print 'Global Maximum is: ', maximum[len(maximum)-1]

if __name__ == "__main__":

main()

解决方案

Using sorted() would, of course, be reliable, quick to write, and high performance for moderate-sized lists because it is built-in. For large lists, an O(n) algorithm would be faster e.g.:

def minmax1 (x):

# this function fails if the list length is 0

minimum = maximum = x[0]

for i in x[1:]:

if i < minimum:

minimum = i

else:

if i > maximum: maximum = i

return (minimum,maximum)

print(minmax1([9,8,7,6,5,4,3,2,1,11,12,13,14,15,16,17,18,19]))

print(minmax1([1]))

print(minmax1([2, 0, 2, 7, 5, -1, -2]))

... for which the output is:

(1, 19)

(1, 1)

(-2, 7)

I was interested to check the performance of the two alternatives. On my PC running Windows XP and Python 3.2.3, I found that the sorting approach is faster than the minmax1() function defined above for lists of fewer than 500 elements but, for longer lists, the O(n) minmax1() is faster. My timing test code was as follows:

def minmax_sort(x):

x = sorted(x)

return (x[0],x[-1])

import timeit

aa = list(range(0,100))

a = aa

while (1):

stime = min(timeit.repeat('minmax_sort(a)', "from __main__ import minmax_sort,a",number=1000))

mtime = min(timeit.repeat('minmax1(a)', "from __main__ import minmax,a",number=1000))

if (stime > mtime):

break

else:

a = a + aa

print(len(a))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值