Quicksort in ascending order in Python

Asked

Viewed 435 times

1

I need to sort the number of times words repeat in the txt file, but every time I run the code it prints 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))
  • Do you need to be Quicksort? Or do you just want how much each word appears? If so, there is a better way to solve.

  • It has to be Quicksort. I’ve already managed to count the number of repetitions, but the program prints in descending order. I need him to print in ascending order.

  • You’re keeping everything on a list?

  • No. It’s in a txt file

1 answer

1


Dictionaries have no ordering. The function collections.Counter() returns a dictionary.

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

With a small modification in the function of quicksort of your question, it is possible to work with the sorting of a list of tuples and parameterize in which order the sort 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.

Exit:

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)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.