Quicksort in ascending order in Python

0

I need to sort the number of times the words are repeated in the txt file, but every time I slash the code, it's printable in descending order.

Code below:

from collections import Counter

with open('/Users/DIGITAL/Desktop/Python/teste.txt') as f:
    ocorrencias = Counter(f.read().split())
print(ocorrencias)

def quicksort(ocorrencias):
    if len (lista) <= 1:
        return lista
    pivo = lista[0]
    iguais = [x for x in lista if x == pivo]
    menores =[ x for x in lista if x < pivo]
    maiores = [x for x in lista if x > pivo]
    return quicksort(menores)+iguais+quicksort(maiores)
    print(quicksort(ocorrencias))
    
asked by anonymous 08.10.2017 / 04:25

1 answer

1

Dictionaries do not have sort . The collections.Counter() function returns a dictionary.

The secret to sorting dictionaries is to convert them to a list of tuples and then sort them.

With a small modification to the quicksort function of your question, you can work with the ordering of a list of tuples and parameterize the order in which the ordering of the list will occur:

from collections import Counter

def quicksort( array, reverse=False ):
    if len(array) <= 1:
        return array
    ( _, pv ) = array[0]
    iguais = [ (k,v) for k,v in array if v == pv ]
    maiores = quicksort([ (k,v) for k,v in array if v > pv ],reverse)
    menores = quicksort([ (k,v) for k,v in array if v < pv ],reverse)
    if reverse == True:
        return maiores + iguais + menores
    else:
        return menores + iguais + maiores


def ordenar( dic, reverse=False ):
    return quicksort( [ (k,v) for k,v in dic.items() ], reverse );


with open('teste.txt') as f:
    palavras = Counter( f.read().split() )

print "Crescente:", ordenar( palavras, reverse=False )
print "Decrescente:", ordenar( palavras, reverse=True )

test.txt

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Output:

Crescente: [('laborum.', 1), ('ad', 1), ('irure', 1), ('ea', 1), ('officia', 1),
            ('sunt', 1), ('eu', 1), ('sed', 1), ('elit,', 1), ('enim', 1),
            ('Duis', 1), ('et', 1), ('aliqua.', 1), ('labore', 1), ('incididunt', 1),
            ('reprehenderit', 1), ('est', 1), ('quis', 1), ('sit', 1),
            ('veniam,', 1), ('nostrud', 1), ('qui', 1), ('id', 1),
            ('consectetur', 1), ('aute', 1), ('consequat.', 1), ('mollit', 1),
            ('aliquip', 1), ('nulla', 1), ('Lorem', 1), ('do', 1), ('non', 1),
            ('commodo', 1), ('Ut', 1), ('sint', 1), ('velit', 1), ('cillum', 1),
            ('pariatur.', 1), ('ex', 1), ('esse', 1), ('proident,', 1), ('magna', 1),
            ('cupidatat', 1), ('ullamco', 1), ('deserunt', 1), ('ipsum', 1),
            ('amet,', 1), ('nisi', 1), ('fugiat', 1), ('occaecat', 1), ('minim', 1),
            ('culpa', 1), ('tempor', 1), ('laboris', 1), ('anim', 1),
            ('adipiscing', 1), ('Excepteur', 1), ('voluptate', 1), ('exercitation', 1),
            ('eiusmod', 1), ('dolore', 2), ('ut', 2), ('dolor', 2), ('in', 3)]

Decrescente: [('in', 3), ('dolore', 2), ('ut', 2), ('dolor', 2), ('laborum.', 1),
              ('ad', 1), ('irure', 1), ('ea', 1), ('officia', 1), ('sunt', 1),
              ('eu', 1), ('sed', 1), ('elit,', 1), ('enim', 1), ('Duis', 1),
              ('et', 1), ('aliqua.', 1), ('labore', 1), ('incididunt', 1),
              ('reprehenderit', 1), ('est', 1), ('quis', 1), ('sit', 1),
              ('veniam,', 1), ('nostrud', 1), ('qui', 1), ('id', 1),
              ('consectetur', 1), ('aute', 1), ('consequat.', 1), ('mollit', 1),
              ('aliquip', 1), ('nulla', 1), ('Lorem', 1), ('do', 1), ('non', 1),
              ('commodo', 1), ('Ut', 1), ('sint', 1), ('velit', 1), ('cillum', 1),
              ('pariatur.', 1), ('ex', 1), ('esse', 1), ('proident,', 1),
              ('magna', 1), ('cupidatat', 1), ('ullamco', 1), ('deserunt', 1),
              ('ipsum', 1), ('amet,', 1), ('nisi', 1), ('fugiat', 1), ('occaecat', 1),
              ('minim', 1), ('culpa', 1), ('tempor', 1), ('laboris', 1), ('anim', 1),
              ('adipiscing', 1), ('Excepteur', 1), ('voluptate', 1), ('exercitation', 1),
              ('eiusmod', 1)]
    
08.10.2017 / 04:54