# Bubble Sort Algorithm Animation

Page 1 of 1

## 2 Replies - 4502 Views - Last Post: 03 March 2013 - 01:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313999&amp;s=82355f94f796b7f2c73a5ea47377b2e7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Computer-Wiz

Reputation: 1
• Posts: 26
• Joined: 29-January 11

# Bubble Sort Algorithm Animation

Posted 01 March 2013 - 06:04 PM

Hi there, so I've been working on this code that will animate a bubble sort algorithm using bars. The one problem that i have is that i cant get the bars to do the animation 1 by one so that you can see the swap occurring. Right now the program will animate each swap set and then delete it so in the end you get the sorted bars but I want to make it so that only one bar is swaped each time. Here is what i want it to look like (this is my class website):

http://www.mmrelearn...iew.php?id=4221

```from Tkinter import*
import time

def sort():
x =100
y = 500
barWidth = 15
lst = [10, 25,5,15,30,20]

for item in lst:

bar = cv.create_rectangle(x, y, x+barWidth, y-(item*10), fill="red")
x += barWidth + 5
cv.update()

x= 100
y =500

for item in range(len(lst)-1, 0, -1):
for i in range(item):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
print lst
cv.delete(ALL)
for bars in lst:
time.sleep(0.1)
barSorted = cv.create_rectangle(x, y, x+barWidth, y-(bars*10), fill="red")
x += barWidth + 5
cv.update()

#MAIN

#Canvas
root = Tk()
root.title("Sorting Algorithm Animation")
w = 800
h = 600
cv = Canvas(width=w, height=h, bg='black')

cv.pack()
cv.update()
sort()
root.mainloop()

```

Is This A Good Question/Topic? 0

## Replies To: Bubble Sort Algorithm Animation

### #2 Computer-Wiz

Reputation: 1
• Posts: 26
• Joined: 29-January 11

## Re: Bubble Sort Algorithm Animation

Posted 03 March 2013 - 09:38 AM

Hi there, so i updated this code to organize it more but i still cant get it to do what i wanted. It still gives me the same output.

```'''
Created on Sep 12, 2010

@author: aaronkilpatrick
'''
from Tkinter import*
import time

def bubbleSort(lst):
for number in range(len(lst)-1, 0, -1):
for i in range(number):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
print lst
for j in lst:
cv.delete([j])
animation()
cv.delete(ALL)
cv.update()

def animation():
x , y = 220, 500
barWidth = 15

for item in lst:

cv.delete([item])
bar = cv.create_rectangle(x, y, x+barWidth, y-(item*15), fill="red")
x += barWidth + 5
time.sleep(0.2)
cv.update()

#MAIN

#Canvas
root = Tk()
root.title("Sorting Algorithm Animation")
w = 800
h = 600
cv = Canvas(width=w, height=h, bg='black')

cv.pack()
cv.update()

lst = [1,2,7,3,6,4,5,10,8,9,12,15,11,13,16,14,20,17,19,18]
bubbleSort(lst)
animation()

root.mainloop()

```

### #3 baavgai

• Dreaming Coder

Reputation: 6962
• Posts: 14,548
• Joined: 16-October 07

## Re: Bubble Sort Algorithm Animation

Posted 03 March 2013 - 01:42 PM

Your animation function should be doing all the animation work. Your sleep should happen after all the graphic changes have taken place.

e.g.
```# don't pretend you're using lst, it's a bloody global!
# def bubbleSort(lst):

def bubbleSort():
for number in range(len(lst)-1, 0, -1):
for i in range(number):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
print lst
animation()
# let animcation handle these
# cv.delete(ALL)
# cv.update()

def animation():
x , y = 220, 500
barWidth = 15

# this seems kind of pointless
# you're about to delete all
# for j in lst: cv.delete([j])
cv.delete(ALL)
for item in lst:
# this is also pointless, you've deleted, leave it alone
# cv.delete([item])
bar = cv.create_rectangle(x, y, x+barWidth, y-(item*15), fill="red")
x += barWidth + 5
# do this outside the loop
# time.sleep(0.2)
# cv.update()
cv.update()
time.sleep(0.2)

```

Now, if you could do a proper bubble sort and not use any globals, you'd have something. If you only changed the bars that moved, even better.