1. Cédric Bonhomme
  2. Sort this messy list

Source

Sort this messy list / selection.py





class Selection(object):
    """
    Selection sort - O(n^2)
    """
    def __init__(self, l):
        self.unsorted_list = l

    def sort(self):
        l = self.unsorted_list
        for i in range(len(l)-1):
            mini=i
            for j in range(i+1,len(l)):
                if l[j] < l[mini]:
                    mini=j
                self.swap(l,i,mini)

    def swap(self, l, i, j):
        """echange 2 valeurs d'une liste"""
        l[i], l[j] = l[j], l[i]

class Selection_Better(object):
    """
    Selection sort - O(n^2)
    """
    def __init__(self, l):
        self.unsorted_list = l

    def sort(self):
        l = self.unsorted_list
        for i in range(len(l) - 1):
            mini=i
            lmini = l[i]
            for j in range(i + 1, len(l)):
                if l[j] < lmini:
                    mini = j
                    lmini = l[j]
            l[mini], l[i] = l[i], lmini