1

I'm writing a program in Python that implements a selection sort algorithm and sorts the elements of a list in descending order.

Let's say my input is l = [242, 18, 44, 201, 1111].

My logic is as follows:

  • l = [242, 18, 44, 201, 1111] # switch l[0] (242) and l[len(l)-1] (1111)
  • l = [1111, 18, 44, 201, 242] # switch l[1] (18) and l[len(l)-1] (242)
  • l = [1111, 242, 44, 201, 18] # switch l[2] (44) and l[len(l)-2] (201)

The output would be [1111, 242, 201, 44, 18].

So, here's the code I'm implementing based on the above logic:

def selSort(l):
    '''Sorts the list in descending order using a selection sort algorithm.

    Params: l (list): unsorted list
    Sorts: unsorted list in descending order
    '''
    start = len(l)-1
    while start != 0:
        for i in range(len(l)):
            if l[i] < l[start]:
                l[i], l[start] = l[start], l[i]
        start -= 1

It seems as though I overestimated my logic because the algorithm's output is [1111, 18, 242, 201, 44].

After some debugging, I was able to find that after a couple traversals l is properly sorted, but the while loop still hasn't met its terminating condition. This means that there's going to be some unwanted overlap between start and i. For example, when start = 3 and i = 4, l[i] < l[start], resulting in l = [1111, 242, 201, 18, 44]. After an additional traversal, we're at the incorrect output I showed above.

What would be an elegant (I'm aware selection sort isn't the most efficient algorithm) and Pythonic solution to this issue? I'm trying to achieve this without using any built-in functions (with the exception of len and range), methods, or outside libraries (if possible).

I've checked out the Selection Sort Algorithm Python and Selection Sort Algorithm in Java posts here on SO. The former uses list methods (which I'm trying to avoid) and I don't understand java syntax well enough to make use of the latter.

2 Answers 2

2

This should work. It also uses range() to avoid having to use a while loop.

def selSort(l):
'''Sorts the list in descending order using a selection sort algorithm.

Params: l (list): unsorted list
Sorts: unsorted list in descending order
'''
for i in range(len(l)):

    # Find the largest element in list
    max_index = i
    for j in range(i + 1, len(l)):
        if l[max_index] < l[j]:
            max_index = j

    # Swap the first and max item
    l[i], l[max_index] = l[max_index], l[i]
Sign up to request clarification or add additional context in comments.

Comments

2

The logic of the select sort algorithm (descending order) : is to iterate over the table n-1 times(n being the number of elements in the list). at the ith iteration, we select the highest element between the indices i+1 and n and swap that element with the element at the position i in the list. This leads to the following code :

def selSort(l):
    '''Sorts the list in descending order using a selection sort algorithm.

    Params: l (list): unsorted list
    Sorts: unsorted list in descending order
    '''
    for i in range(len(l)-1) :
        posMax = i
        for j in range(i+1,len(l)):
            if l[j] > l[posMax]:
                posMax = j
        l[posMax], l[i] = l[i], l[posMax]
    return l

l = selSort([242, 18, 44, 201, 1111])
print(l) #prints [1111, 242, 201, 44, 18]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.