How should I tweak the selection sort code below just enough to make it work while still looking a little the same? Python

Viewed 107

The code below is not iterating through the list and sorting the list. How should I tweak it just enough to make it work while still looking a little the same? I've tried multiple ways to make it work to no avail. Oh and, I'm aware of the Selection sorting shortcut, the sort() function, but I want to learn the long way as well in how to code functions, programs and processes. Thanks!

def sortList(L,n):
    minValue = L[0]
    L2 = []
    idx = 0
    counter = 0
    
    while (counter < n):
        v = L[counter]
        if v < minValue:
            minValue = v
            idx = counter
            L2.append(minValue)
            del L[idx]
            n-=1

        counter += 1

    return L2

L = [34, -1, 0, 89, 21, -40, 7]
n = len(L)

print(sortList(L, n))
3 Answers

You can implement a insertion sort:

def sortList(L):
    L2 = []
    
    while len(L) != 0:
        minValue = L[0]
        indx = 0 # index of the element that will be deleted
        counter = 0 # iterating counter
        for num in L:
            if num<minValue: 
                minValue = num
                indx=counter
            counter+=1
            
        L2.append(minValue)
        del L[indx]

    return L2

L = [34, -1, 0, 89, 21, -40, 7]

print(sortList(L))

or you can implement a selection sort:

def sortList(L):
    
    for counter in range(0,len(L)):
        minValueIndex = counter

        for indx in range(counter,len(L)):
            if(L[indx] < L[minValueIndex]):
                minValueIndex = indx

        L[counter],L[minValueIndex] = L[minValueIndex],L[counter]

L = [34, -1, 0, 89, 21, -40, 7]

sortList(L)
print(L)

Your code does not follow the selection sort logic (there are two main errors: selection sort does not use an auxiliary arrangement, such as L2; ​​you have not changed the value of the current ( L[counter] ) and lower value ( L[minValueIndex] ) variables). Try using print on the method lines to try to understand the logic error of your algorithm.

import sys
def selection_sort(unsorted_list):
    """Traverses the list to finds the min and inserts it in the beginning. 
    Then repeats traversing through the unsorted members, 
    each time popping and inserting the min after the previous mins."""

    my_list = list(unsorted_list)
    counter = 0
    j = 0
    while j < len(my_list):
        min = sys.maxsize
        min_index = -1
        for i in range(j, len(my_list)):
            counter += 1
            if my_list[i] < min:
                min = my_list[i]
                min_index = i
        
        a = my_list.pop(min_index)
        my_list.insert(j, a)
        j += 1
    return my_list

print(selection_sort([34, -1, 0, 89, 21, -40, 7]))
#output: [-40, -1, 0, 7, 21, 34, 89]

I highly recommend to use nested loops because it is easier, but I decided to give this a go

make it work while still looking a little the same

def sortList(L,n):
    minValue = L[0]+1
    L2 = []
    idx = 0
    counter = 0

    while (len(L) > 0):
        v = L[counter]
        if v <= minValue:
            minValue = v
            idx = counter
            n-=1
        counter += 1
        if counter >= len(L):
            L2.append(minValue)
            del L[idx]
            counter = 0
            if len(L):
                minValue = L[0]

    return L2

L = [34, -1, 0, 89, 21, -40, 7]
n = len(L)

print(sortList(L, n))
Related