Home > OS >  I have been trying to create a function to sort a short list of numbers from least to greatest using
I have been trying to create a function to sort a short list of numbers from least to greatest using

Time:01-18

Output:

[9, 99, -5, 5, 1, 3, 777]

Code:

def selection_sort(nums):
    for x in range(len(nums)-1):
        min_value = x
        for j in range(nums[min_value 1], len(nums)):
            if j < nums[min_value]:
               min_value = j
    if nums[min_value] != x:
        nums[min_value], nums[x] = nums[x], nums[min_value]
return nums


nums = [9, 99, 777, 3, 5, 1, -5]
print(selection_sort(nums))

The function has two loops. The first one makes so that whatever number(x) that the loop is on is identified as the minimum value. The second loop goes through every number to the right of x to see if there is a number smaller than x. And finally if there is a number smaller than x that number is swapped with x.

CodePudding user response:

Looks like you were mixing up what variables are indexes and which are the actually numbers you want to sort.

Here's yours but with the variables changed to some more memorable names.

def selection_sort(nums):
    for starting_index in range(len(nums)):
        min_index = starting_index
        for compare_index in range(starting_index, len(nums)):
            if nums[compare_index] < nums[min_index]:
               min_index = compare_index
        if nums[min_index] != nums[starting_index]:
            nums[min_index], nums[starting_index] = nums[starting_index], nums[min_index]
    return nums


nums = [9, 99, 777, 3, 5, 1, -5]
print(selection_sort(nums))

CodePudding user response:

There were several issues, here are the fixes:

  • min_value is a misleading variable name because it contains an index (x).
  • for j in range(nums[min_value 1], len(nums)): starts the range at the value that is at the min_value but it should start at min_value (or at x)
  • if j < nums[min_value] is comparing the index of the candidate element with the current smallest value (number). It need to compare the value at index j instead
  • if nums[min_value] != x: is at the wrong indentation level. It needs to be inside the for x in ... loop because you want to perform the swap for every value of x (not just the last one)
  • if nums[min_value] != x: compares the smallest value to an index (x). it needs to compare it to the value at index x
  • the function sorts the list in-place, it doesn't need to return it at the end

...

def selection_sort(nums):
    for x in range(len(nums)-1):
        min_value = x
        for j in range(min_value 1, len(nums)): # range on indexes
            if nums[j] < nums[min_value]:       # compare values to values
               min_value = j
        if min_value != x:        # indent and compare values to values
            nums[min_value], nums[x] = nums[x], nums[min_value]

To make the code smaller, you could use the min() function to get the index of the smallest remaining number:

def selection_sort(nums):
    for i in range(len(nums)-1):
        j = min(range(i,len(nums)),key=lambda i:nums[i])
        nums[i],nums[j] = nums[j],nums[i]
  •  Tags:  
  • Related