Skip to content

Commit

Permalink
Versão simplificada
Browse files Browse the repository at this point in the history
  • Loading branch information
juanplopes committed Apr 3, 2012
1 parent 5a564e0 commit d4c2b3f
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions algorithms/sorting/quicksort.py
Expand Up @@ -5,6 +5,7 @@
C.A.R. Hoare
Colaborador:
Adriano Melo (adriano@adrianomelo.com)
Juan Lopes (me@juanlopes.net)
Tipo:
sorting
Descrição:
Expand All @@ -22,25 +23,14 @@
"""
from random import randint

def quicksort (array):
if len(array) <= 1:
return array

pivot = array.pop()

smaller = []
equal = [pivot]
larger = []

for ele in array:
if ele < pivot:
smaller.append(ele)
elif ele > pivot:
larger.append(ele)
else:
equal.append(ele)

return quicksort(smaller) + equal + quicksort(larger)
def quicksort(V):
if len(V) <= 1:
return V

pivot = V.pop()
lesser = [x for x in V if x < pivot]
greater = [x for x in V if x >= pivot]
return quicksort(lesser) + [pivot] + quicksort(greater)

print quicksort([i for i in xrange(30)]) # worst case
print quicksort([3 for i in xrange(30)]) # best case
Expand Down

0 comments on commit d4c2b3f

Please sign in to comment.