Python 循环条件下的运行时 def选择_排序(a): 对于范围(0,len(a)-1)内的i: minIndex=i 对于范围(i+1,len(a))内的j: 如果a[j]

Python 循环条件下的运行时 def选择_排序(a): 对于范围(0,len(a)-1)内的i: minIndex=i 对于范围(i+1,len(a))内的j: 如果a[j],python,python-3.x,data-structures,Python,Python 3.x,Data Structures,我想继续循环,直到运行时间大于60,但我想不出一个条件允许我这样做并将n增加1,因为运行时间是包含在循环中的一个产品,有什么想法吗?两个可能的选项是使用break语句 def selection_sort(a): for i in range(0, len(a) - 1): minIndex = i for j in range(i + 1, len(a)): if a[j] < a[minIndex]:

我想继续循环,直到运行时间大于60,但我想不出一个条件允许我这样做并将n增加1,因为运行时间是包含在循环中的一个产品,有什么想法吗?

两个可能的选项是使用
break
语句

def selection_sort(a):
    for i in range(0, len(a) - 1):
        minIndex = i
        for j in range(i + 1, len(a)):
            if a[j] < a[minIndex]:
                minIndex = j
            if minIndex != i:
                a[i], a[minIndex] = a[minIndex], a[i]

def selection_sort_runs():
    run_time_list = []
    n = 1
    while [having trouble thinking of loop condition here]
        start_time = time.time()
        rand = [random.randint(0, 100) for x in range(1, 10001*n)]
        selection_sort(rand)
        end_time = time.time()
        run_time = end_time - start_time
        run_time_list.append(run_time)
        if run_time < 60:
            n += 1

有两种可能的选择是使用
break
语句

def selection_sort(a):
    for i in range(0, len(a) - 1):
        minIndex = i
        for j in range(i + 1, len(a)):
            if a[j] < a[minIndex]:
                minIndex = j
            if minIndex != i:
                a[i], a[minIndex] = a[minIndex], a[i]

def selection_sort_runs():
    run_time_list = []
    n = 1
    while [having trouble thinking of loop condition here]
        start_time = time.time()
        rand = [random.randint(0, 100) for x in range(1, 10001*n)]
        selection_sort(rand)
        end_time = time.time()
        run_time = end_time - start_time
        run_time_list.append(run_time)
        if run_time < 60:
            n += 1

@好管闲事不一定。如果不使用if语句,则最终会超过1,因为在中断之前的最后一次迭代将超过60。你可以在循环结束后将其减1,以避免出现这种情况。是的。我没有仔细阅读每个变量所做的事情,并假设了一些事情:)@busybear不一定。如果不使用if语句,则最终会超过1,因为在中断之前的最后一次迭代将超过60。你可以在循环结束后将其减1,以避免出现这种情况。是的。我没有足够仔细地阅读每个变量所做的事情,并假设了一些事情:)
run_time = -1        
while run_time < 60:
        start_time = time.time()
        rand = [random.randint(0, 100) for x in range(1, 10001*n)]
        selection_sort(rand)
        end_time = time.time()
        run_time = end_time - start_time
        run_time_list.append(run_time)
        if run_time < 60:
            n += 1