c - Python 'pointer arithmetic' - Quicksort -
in idiomatic c fashion, 1 can implement quicksort in simple way 2 arguments:
void quicksort(int inputarray[], int numelems);
we can safely utilize 2 arguments later subdivisions (i.e. partitions, they're commonly called) via pointer arithmetic:
//later on in our quicksort routine... quicksort(inputarray+last+1, numelems-last-1);
in fact, asked before on because untrained in pointer arithmetic @ time: see passing array function odd format - “v+last+1”
basically, is possible replicate same behavior in python , if so, how? have noticed lists can subdivided colon within of square brackets (the slicing operator), piece operator not pass list point on; 1st element (0th index) still same in both cases.
as you're aware, python's piece syntax makes copy, in order manipulate subsection of list (not "array", in python) in place, need pass around both list , start-index , size (or end-index) of portion under discussion, much in c. signature of recursive function like:
def quicksort( inputlist, numelems, startindex = 0 ):
and recursive phone call like:
quicksort( inputlist, numelems-last-1, last+1 )
throughout function you'd add together startindex
whatever list accesses make.
python c arrays list pointers
No comments:
Post a Comment