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
I want to continue the loop until run_time is greater than 60, but I can't think of a condition that will allow me to do so and increment n by one because run_time is a product that is contained inside of the loop, any ideas?
Two of the possible options you have would be to use a break
statement
while True:
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
else:
break
Or you could refactor the code so the run_time
variable is initialized outside the loop.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.