Sorting algorithms/Quicksort: Difference between revisions
Content added Content deleted
No edit summary |
(→Python) |
||
Line 11: | Line 11: | ||
----------------------------------------------------------------------- |
----------------------------------------------------------------------- |
||
generic |
generic |
||
type Element_Type is private; |
type Element_Type is private; |
||
type Index_Type is (<>); |
type Index_Type is (<>); |
||
type Element_Array is array(Index_Type range <>) of Element_Type; |
type Element_Array is array(Index_Type range <>) of Element_Type; |
||
Line 196: | Line 196: | ||
a = #(4, 89, -3, 42, 5, 0, 2, 889) |
a = #(4, 89, -3, 42, 5, 0, 2, 889) |
||
a = quickSort a |
a = quickSort a |
||
==[[Python]]== |
|||
[[Category:Python]] |
|||
def quickSort(arr): |
|||
less = [] |
|||
pivotList = [] |
|||
more = [] |
|||
if len(arr) <= 1: |
|||
return arr |
|||
else: |
|||
pivot = arr[0] |
|||
for i in arr: |
|||
if i < pivot: |
|||
less.append(i) |
|||
elif i > pivot: |
|||
more.append(i) |
|||
else: |
|||
pivotList.append(i) |
|||
less = quickSort(less) |
|||
more = quickSort(more) |
|||
return less + pivotList + more |
|||
a = [4, 65, 2, -31, 0, 99, 83, 782, 1] |
|||
a = quickSort(a) |