Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
Line 3,147: Line 3,147:
more = [x for x in list[1:] if x >= pivot]
more = [x for x in list[1:] if x >= pivot]
return qsort(less) + [pivot] + qsort(more)</lang>
return qsort(less) + [pivot] + qsort(more)</lang>

More correctly in some tests:
<lang python>from random import *

def qSort(a):
if len(a) <= 1:
return a
else:
q = choice(a)
return qSort([elem for elem in a if elem < q]) + [q] * a.count(q) + qSort([elem for elem in a if elem > q])</lang>


<lang python>def quickSort(a):
if len(a) <= 1:
return a
else:
less = []
more = []
pivot = choice(a)
for i in a:
if i < pivot:
less.append(i)
if i > pivot:
more.append(i)
less = quickSort(less)
more = quickSort(more)
return less + [pivot] * a.count(pivot) + more</lang>


=={{header|Qi}}==
=={{header|Qi}}==