Tuesday, 15 May 2012

Quicksort without the final merge Python -



Quicksort without the final merge Python -

i new coding , trying implement quicksort in python. next code output not correct. going wrong? choosing pivot first element , without having merge.

def quicksort(a): if len(a) > 1: p = a[0] = 0 j in a[1:]: if j < p: a[i] , j = j , a[i] = i+1 a[0] , a[i] = a[i] , a[0] left = a[:i] right = a[i+1:] left = quicksort(left) right = quicksort(right) homecoming else: homecoming

after changes, code is

def quicksort(a, left = none, right =none): if left none: left = 0 if right none: right = len(a) p =a[left] = left +1 j in a[left+1:right]: if a[j] < p: a[i] , a[j] = a[j], a[i] = + 1 a[left] , a[i-1] = a[i-1], a[left] quicksort(a[:i]) quicksort(a[i+1:]) homecoming

i getting error, list index out of range.

these making copies of slices of a

left = a[:i] right = a[i+1:]

anything re-create won't impact a

to modify a inplace, should pass left , right indices parameters

def quicksort(a, left=none, right=none): if left none: left = 0 if right none right = len(a) ...

now remembering left , right indices here (not lists in code). modify rest of function create changes a directly

python quicksort

No comments:

Post a Comment