[Tutor] bubble sort function

Spyros Charonis s.charonis at gmail.com
Sat Nov 15 20:34:43 CET 2014


Thank you Alan,

When I initiated the loop with the condition:

for i in range(len(unsorted)):


Python raised an IndexError saying I had gone out of bounds. Hence the
change to:

for i in range(0, size)


Yes, I actually the loop only consists of:


while unsorted[i] > unsorted[i+1]:

    # Use a tuple assignment in order to swap the value of two variables

    unsorted[i], unsorted[i+1] = unsorted[i+1], unsorted[i]

    iterations += 1


Sorry about that. the *iterations* update and sorted_vec assignment are
outside of the loop body.


This is indeed just a learning exercise, I am aware that lists have sort()
and reverse() methods. I'm in the process of learning a bit about data
structures & algorithms using Python as my implementation language.




On Sat, Nov 15, 2014 at 7:02 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 15/11/14 16:46, Spyros Charonis wrote:
>
>  def bubble_sort_ascending(unsorted):
>>         iterations = 0
>>         size = len(unsorted) - int(1)
>>
>
> Don't convert 1 to an int - it already is.
>
>          for i in range(0, size):
>>
>
> This will result in 'i' going from zero to len()-2.
> Is that what you want?
>
>               unsorted[i] = float(unsorted[i])
>>
>
> Comparing ints to floats or even comparing two floats
> is notoriously error prone due to the imprecision of
> floating point representation. You probably don't want
> to do the conversion.
>
> And if you must do it, why do you only do it once,
> outside the while loop?
>
>               while unsorted[i] > unsorted[i+1]:
>>                    unsorted[i], unsorted[i+1] = unsorted[i+1], unsorted[i]
>>                    iterations += 1
>>
>
> I assume you intended to end the loop body here?
> But the following lines are indented so are included
> in the loop.
>
> Also because you never change 'i' the loop can only
> ever run once. So really you could use a an if
> statement instead of the while loop?
>
> Finally, iterations is really counting swaps. Is that what you want it to
> count or os it actually loop iterations? If so which? The for loop or the
> while loop or the sum of both?
>
>                     sorted_vec = unsorted[:]
>>                    print "\nIterations completed: %s\n" %(iterations)
>>         return sorted_vec
>>
>
> Since you never alter sorted_vec there is no point in creating it.
> Just return unsorted - which is now sorted...
>
>
>  and I have to call it again for the the sorting operation to complete.
>> Is there something I am missing in my code? Why does it not sort the
>> entire list at once and just count all completed iterations?
>>
>
> There are several things missing or broken, the few I've pointed
> out above will help but the algorithm seems suspect to me. You need
> to revisit the core algorithm I suspect.
>
> BTW I assume this is just a learning exercise since the default
> sorting algorithm will virtually always be better than bubble
> sort for any real work!
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141115/5a9f5829/attachment-0001.html>


More information about the Tutor mailing list