I need to write a program that generates a list containing an arbitrary number of random integers and produces a sorted list using the "selection sort" algorithm.
I don't know how to use i to count from 0 to the last index of the list.
I also am unsure of how to use j to count from i + 1 to the last index of the list.
When I run the program, it simply generates a random list of 10 integers, some negative and some positive. Below is a pic of the instructions, and my code so far. Thanks for any kind of help.
def main():
list= []
length= 10
num_operations= 0
while num_operations < length :
list= list + [randint(1, 100)]
num_operations+=1
print list
for i in range( 0, length ) :
i = i + 1
min = i
for j in range ( i + 1, length ) :
if list[j] < list[min] :
min = j
if min != i :
temp= list[i]
list[i]= list[min]
list[min]= temp
First something is wrong with your min calculation, the problem is the definition of min isn't as its right place, try this:
min = 0
for i in range( 0, length) :
for j in range ( i + 1, length) :
if list[j] < list[min] :
min = j
i = i + 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.