Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
imported>MM
m (→‎{{header|Python}}: : un-pythonic to have "else:" after "if condition: … return", as superfluous.)
imported>MM
(→‎{{header|Python}}: use speaking variable names.)
Line 8,010: Line 8,010:


=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang="python">def quickSort(arr):
<syntaxhighlight lang="python">def quick_sort(sequence):
less = []
lesser = []
pivotList = []
equal = []
more = []
greater = []
if len(arr) <= 1:
if len(sequence) <= 1:
return arr
return sequence
pivot = arr[0]
pivot = sequence[0]
for i in arr:
for element in sequence:
if i < pivot:
if element < pivot:
less.append(i)
lesser.append(element)
elif i > pivot:
elif element > pivot:
more.append(i)
greater.append(element)
else:
else:
pivotList.append(i)
equal.append(element)
less = quickSort(less)
lesser = quick_sort(lesser)
more = quickSort(more)
greater = quick_sort(greater)
return less + pivotList + more
return lesser + equal + greater




a = [4, 65, 2, -31, 0, 99, 83, 782, 1]
a = [4, 65, 2, -31, 0, 99, 83, 782, 1]
a = quickSort(a)
a = quick_sort(a)
</syntaxhighlight>
</syntaxhighlight>