Bubble sort

Just like the previous challenge, this time we had to put a list in order but instead of using random we use the bubble sort method (thanks God!).

rw9ykew_700wa_0

It is so much better and efficient, in contrast with random, also it is less code so if you want to do it the better way….. don’t use any of this, there is a built in function in python that does it for you hehe

krqrjmd_700wa_0

Don’t go! it’s still usefull to know this way of sorting. Here it is:

bubble1

The same as the random code except that instead of sorting asigning random index we use the function bubble(). This function, while the list is not sorted, compares each pair of numbers that are side by side in the list and if the first one is greater than the second one it swiches the position of those numbers.

xw7v9db_700w_0

The rest is just printing the list an the amount of times the while repeated. Hava a look to the results to compare the difference between this and random sorting:

bubble2

And with 10 numbers:

bubble3

Big difference.

Here is a video to help you out:

Follow me on twitter @danigguemez

Code avaliable in Github: https://github.com/Guemez/TC1014/blob/master/bubblesort.py

#TC101 #python #bubblesort #DailyChallenge

Leave a comment