QuickSort in Python. Increment in array trouble -


i trying implement quicksort in python. problem how increment/decrement value of i/j in array a. know should write i=i+1 , there no such thing i++ in python, don't understand in way should this. newbie, here code.

def quicksort(a,lo,hi):     if(hi<=lo):         return     = lo - 1     j = hi     v = a[hi]      while true:         while(a[++i] < v):             pass          while(v < a[--j]):             if(j==lo):                 break         if(i>=j):             break         t = a[i]         a[i] = a[j]         a[j] = t      t = a[i]     a[i] = a[hi]     a[hi] = t     quicksort(a, lo, - 1)     quicksort(a, + 1, hi) 

in python, cannot assign , value, that's intentional limitation avoid issues typos, find proper sequence point...

you have no choice "emulate" c-ported code:

    while(a[++i] < v):         pass      while(v < a[--j]):         if(j==lo):             break 

(note both constructs generate infinite loop because:

++i == 

and

--j == j 

(applying unary plus number of times or unary minus number of times gives same number, see why don't 2 plus operators throw error (e.g., 1 + + 2))

so change to:

    += 1     while(a[i] < v):         += 1      j -= 1     while(v < a[j]):         if(j==lo):             break         j -= 1 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -