|
|
@ -49,7 +49,7 @@ def quick_sort_accumulation(data, a): |
|
|
|
return a |
|
|
|
less, equal, greater = partition(data) |
|
|
|
return quick_sort_accumulation(less, |
|
|
|
equal + quick_sort_accumulation(greater, a)) |
|
|
|
equal + quick_sort_accumulation(greater, a)) |
|
|
|
|
|
|
|
|
|
|
|
def quicksort(data): |
|
|
@ -59,5 +59,53 @@ def quicksort(data): |
|
|
|
return quick_sort_accumulation(data, []) |
|
|
|
|
|
|
|
|
|
|
|
def iterative_partition(data, left, right): |
|
|
|
""" |
|
|
|
Function which partitions the data into two segments, |
|
|
|
the left which is less than the pivot and the right |
|
|
|
which is greater than the pivot. The pivot for this |
|
|
|
algo is the right most index. This function returns |
|
|
|
the ending index of the pivot. |
|
|
|
|
|
|
|
:param data: array to be sorted |
|
|
|
:param left: left most portion of array to look at |
|
|
|
:param right: right most portion of the array to look at |
|
|
|
""" |
|
|
|
x = data[right] |
|
|
|
i = left - 1 |
|
|
|
j = left |
|
|
|
while j < right: |
|
|
|
if data[j] <= x: |
|
|
|
i = i + 1 |
|
|
|
data[i], data[j] = data[j], data[i] |
|
|
|
j = j+1 |
|
|
|
data[i + 1], data[right] = data[right], data[i + 1] |
|
|
|
return i + 1 |
|
|
|
|
|
|
|
|
|
|
|
def iterative_quick_sort(data): |
|
|
|
""" |
|
|
|
In place implementation of quick sort |
|
|
|
|
|
|
|
Wrapper function for iterative_quick_sort_helper which |
|
|
|
initalizes, left, right to be the extrema of the array. |
|
|
|
""" |
|
|
|
iterative_quick_sort_helper(data, 0, len(data) -1) |
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
def iterative_quick_sort_helper(data, left, right): |
|
|
|
""" |
|
|
|
Uses the divide and conquer algo to sort an array |
|
|
|
|
|
|
|
:param data: array of data |
|
|
|
:param left: left index bound for sorting |
|
|
|
:param right: right bound for sorting |
|
|
|
""" |
|
|
|
if left < right: |
|
|
|
pivot = iterative_partition(data, left, right) |
|
|
|
iterative_quick_sort_helper(data, left, pivot -1) |
|
|
|
iterative_quick_sort_helper(data, pivot+1, right) |
|
|
|
|
|
|
|
|
|
|
|
print quicksort([1,3,1,5,7,9,2,3,5,5,6]) |
|
|
|
print iterative_quick_sort([1,3,1,5,7,9,2,3,5,5,6]) |