Search code examples
pythonselection-sort

Selection Sorting


I have this code that takes a txt file that prints out a list of countries.

def readCountries(filename):
    result=[]
    lines=open(filename)

    for line in lines:
        result.append(line.strip('\n').split(',\t'))
    for sublist in result:
        sublist[1]=float(sublist[1])
        sublist[2]=int(sublist[2])

    return result

I am trying to create a selection sort that sorts the list by population in descending order, and will print the information.

Here is my code what am I doing wrong?

def countryByPop(population):
    myList=readCountries('countries.txt')

    for i in range(0,len(myList)):
        largest = i
        for j in range(i,len(myList)):
            if myList[j]<myList[smallest]:
                smallest = j
        temp = myList[i]
        myList[i] = myList[smallest]
        myList[smallest] = temp

    if population range(0,len(myList))
        return temp
    else:
        print "Invalid Parameter: %s" %(population)

Solution

  • First, you define the variable largest and never use it again. You then use the undefined variable smallest. I assume you meant to change all instances of smallest to largest and the largest is to be the index in the list that points to the country with the largest population. You should change all instances of smallest to largest.

    To sort in descending order you want to find the largest item on each iteration of the outer for loop and then move it to myList[i]. However, by testing myList[j] < myList[smallest] you end up moving the smallest element to the beginning of the list. You should change that to > instead of <. This way the inner for loop will find the country that has a larger population than the one pointed to by largest.

    Also, your if statement is not actually checking the country population. Each element of myList is a list. So you are comparing a list to a list. Instead you want to compare an element of the list. If the third element of your list stores the population then the statement should read myList[j][2] > myList[largest][2]. Notice the second set of braces to access that population element.

    def countryByPop(population):
        myList=readCountries('countries.txt')
    
        for i in range(0,len(myList)):
            largest = i
            for j in range(i, len(myList)):
                if myList[j][2] > myList[largest][2]:
                    largest = j
            temp = myList[i]
            myList[i] = myList[largest]
            myList[largest] = temp
    
        if population < len(myList):
            return myList[population-1]
        else:
            print "Invalid Parameter: %s" % (population)
    

    If the population parameter is meant to retrieve the nth most populous country then you retrieve that item by myList[population-1]. The use of range() in the if statement was unnecessary. The code reflects this change. You should use a better name than population to clarify the intent, such as country_rank.