Permalink
Browse files

Merge pull request #70 from juanplopes/patch-1

Versão simplificada do quicksort
  • Loading branch information...
2 parents 5a564e0 + d4c2b3f commit cb6328ec200e2ec8b7f1fedb538138903d840c79 @hacktoon committed Apr 3, 2012
Showing with 9 additions and 19 deletions.
  1. +9 −19 algorithms/sorting/quicksort.py
@@ -5,6 +5,7 @@
C.A.R. Hoare
Colaborador:
Adriano Melo (adriano@adrianomelo.com)
+ Juan Lopes (me@juanlopes.net)
Tipo:
sorting
Descrição:
@@ -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

0 comments on commit cb6328e

Please sign in to comment.