简体   繁体   English

在排序列表中查找大于给定数字的最小数字

[英]Find the smallest number that is greater than a given number in a sorted list

Given a sorted list of numbers, I need to find the smallest number that is greater than a given number. 给定一个排序的数字列表,我需要找到大于给定数字的最小数字。 Consider this list: 考虑这个清单:


arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383]

Say the specified number is 320. Then, my method should return 353 as 353 is the smallest number greater than 320. 假设指定的数字是320.然后,我的方法应该返回353,因为353是大于320的最小数字。

I am trying to use a slightly modified form of binary search; 我试图使用略微修改的二进制搜索形式; however on execution the program goes into infinite loop. 但是在执行时,程序进入无限循环。


def modBinarySearch(arr,x):
    l=len(arr)
    mid=l/2
    if arr[mid]>=x and arr[mid-1]<x:
        return arr[mid]
    elif arr[mid]>x and arr[mid-1]>x:
        modBinarySearch(arr[mid:l],x)
    else: 
        modBinarySearch(arr[0:mid],x)

N=int(raw_input())
arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383]
print modBinarySearch(arr,N)

Can someone point out what I am doing wrong ? 有人可以指出我做错了什么吗?

There is a standard module, bisect , that does this already: 有一个标准模块, bisect ,已经这样做:

In [49]: arr[bisect.bisect(arr, 320)]
Out[49]: 353

I think this should be the go-to method for searching sorted lists. 我认为这应该是搜索排序列表的首选方法。 There are a few examples in the manual. 手册中有几个例子

As to your implementation, there is a number of problems: 至于你的实现,有很多问题:

  1. Your recursion doesn't handle small arrays correctly. 您的递归不能正确处理小数组。
  2. The slicing done in the second branch is incorrect. 在第二个分支中完成的切片是不正确的。
  3. Your function doesn't return anything. 你的功能不会返回任何东西。
  4. Because arr is in ascending order, arr[mid]>x and arr[mid-1]>x is equivalent to arr[mid-1]>x , suggesting you didn't write what you meant 因为arr是按升序排列的, arr[mid]>x and arr[mid-1]>x相当于arr[mid-1]>x ,这表明你没有写出你的意思

Last but not least, recursion and all that slicing are completely unnecessary for this problem. 最后但并非最不重要的是,递归和所有切片对于这个问题完全没有必要。

If the size of your lists is going to be 15, ditch the binary search altogether and use a sequential search. 如果列表的大小为15,则完全抛弃二进制搜索并使用顺序搜索。

You'll find the code much easier to write and, unless you need to do it many millions of times per second, the sequential solution will be more than fast enough. 您会发现代码更容易编写,除非您需要每秒执行数百万次,否则顺序解决方案将足够快。

If you do need to stick with the binary search, your first step should be to actually return the results of your recursive calls. 如果确实需要坚持使用二进制搜索,你的第一步应该是实际回报您的递归调用的结果。

If arr[mid] and arr[mid-1], both are greater than your number, you should search in arr[0:mid], don't you think? 如果arr [mid]和arr [mid-1]两者都大于你的数字,你应该搜索arr [0:mid],你不觉得吗?

 elif arr[mid]>x and arr[mid-1]>x:
    modBinarySearch(arr[0:mid],x)
else: 
    modBinarySearch(arr[mid:1],x)
def modBinarySearch(arr, n):
    m = len(arr) / 2

    if arr[m] >= n and arr[m - 1] < n:
        return arr[m]
    elif arr[m] > n and arr[m - 1] > n:
        return modBinarySearch(arr[:m], n)
    else:
        return modBinarySearch(arr[m:], n)


arr = [1, 2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383]
n = 320
print(modBinarySearch(arr, n))
     python 3.2

     next(i for  i in arr if i>320)

The bisect module is your best choice for this: bisect模块是您的最佳选择:

from bisect import bisect_left, bisect_right

arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383]


def find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect_left(a, x)
    if i:
        return a[i-1]
    raise ValueError

def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect_right(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

print find_lt(arr,320)          
print find_gt(arr,320)  

prints 版画

313
353     

IF the list is sorted: 如果列表已排序:

x = range(20)
N= 15

for i in x:
    if i>N:
        print i
        break

Gives 16. 给16。

If using numpy: 如果使用numpy:

x = np.arange(20)
N = 15
x[x>15][0]

Gives 16. 给16。

If you were looking for easy implementations, for your specific problem, let me get back on that. 如果您正在寻找简单的实现,针对您的具体问题,让我重温一下。

def modBinarySearch(arr,x):
    l=len(arr)
    mid=l/2
    if arr[mid] >= x and arr[mid-1] < x:
        return arr[mid]
    elif arr[mid]>x and arr[mid-1]>x:
        num = modBinarySearch(arr[0:mid],x)
    else:
        num = modBinarySearch(arr[mid:l],x)
    return num

N=int(raw_input('Enter a number: '))
arr=[1, 2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383]
print modBinarySearch(arr,N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C 函数从按递增顺序排序的数组中返回大于给定数字的最小数字的索引 - C function that returns index of smallest number greater than a given number from an array sorted in increased order 从排序列表中获取大于给定数字的第一个元素 - Get first element greater than a given number from a sorted list 在正则表达式中查找大于给定参数的数字 - Find number greater than given parameter in regex 有效地查找数量最小的索引,该索引大于大型排序列表中的某个值 - Efficiently find index of smallest number larger than some value in a large sorted list 算法(Python):找到大于k的最小数 - Algorithm (Python): find the smallest number greater than k Python二进制搜索类函数,用于查找排序列表中第一个大于特定值的数字 - Python binary search-like function to find first number in sorted list greater than a specific value 给定一个排序列表,找到数字的索引 - given a sorted list, find the index of number python:当最小元素数大于1时列表中的第二个最小值 - python: second smallest value in list when number of smallest elements is greater than 1 如何获得排序列表中最大和最小的数字? - How to get largest and smallest number in a sorted list? 在列表中找到连续数字大于“ n”的最后一个数字 - find the last number in list with consecutive number of numbers greater than “n”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM