[Tutor] sorting algorithim

Alan Gauld alan.gauld at btinternet.com
Fri Apr 24 19:29:47 CEST 2009


"tonyon boyo" <taboyo22 at yahoo.com> wrote 

> i know how to print for bubble sort in python, is 
> there a way to print each pass in the sort so 
> i can see what happens at each step? thanks

Yes add a print statement inside the looop 
that prints the list being sorted.

> def bubblesort(list):
>      for passes in range(len(list)-1, 0, -1):

You could use list slicing here:

       for passes in list[-2::-1]:

But you might find using a while loop is easier than a for loop
for bubble sort

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list