Coding for Beginners in easy steps by McGrath Mike

Coding for Beginners in easy steps by McGrath Mike

Author:McGrath, Mike
Language: eng
Format: epub
ISBN: 9781840786422
Publisher: In Easy Steps Limited
Published: 2015-06-09T16:00:00+00:00


In this example the array is only small but the quick sort algorithm efficiently partitions then re-assembles large arrays in exactly the same way.

Opinions vary as to which element is best to choose as the pivot in the quick sort algorithm. Some coders like to choose a middle element, as in the merge sort algorithm in the previous example. Others prefer to choose the first or last element, or an element at some arbitrary position in between – like the example opposite.

Start a new program by declaring a function to receive a list reference and repeatedly iterate through an array list

def quick_sort( array ) :

if len( array ) > 1 :

pivot = int( len( array ) -1 )

less = [ ] ; more = [ ]

# Algorithm sequence to be added here.

quick_sort( less ) ; quick_sort( more )

print( ‘\tLess:’ , less , ‘\tPivot:’ , array[ pivot ] , \

‘\tMore:’ , more )

array[ : ] = less + [ array[ pivot ] ] + more

print( ‘\t\t...Merged:’ , array )



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.