简体   繁体   中英

number sorting function python not working (selection sort)

nums = [554, 565, 367, 69, 869, 965, 506, 136, 913, 463, 987, 315, 967, 35]
print(nums)
y=0
x=0
min=nums[x]
while y<len(nums):
  while x<len(nums):
    if min>nums[x]:
      c=x
      min=nums[c]
    x=x+1
  x=0
  tmp=nums[y]
  nums[0]=min
  y+=1
  min=tmp

print(nums)

I am confused why this code doesnt sort the numbers from lowest to highest. I am supposed to do selection sort

Change the x=0 to x=y+1 . This is a selection sort and you might check this for help.

EDIT: change algorithm link to selection sort.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM