2 Replies - 2155 Views - Last Post: 03 March 2013 - 01:42 PM Rate Topic: -----

#1 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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()




Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1