Bubble Sort using Python

I have recently started using Python basically Jython to use with Java applications. The love for python is growing, I kind of started liking code in Python. Though I am still looking for some more practical reasons where I can see the effective mixing of Java and Python. So I started with  very basic program of doing bubble sort on a collection. Here it goes.

#!/usr/bin/python
import time;

class BubbleSort() :

def __init__(self):
self.doSearch();

def doSearch(self):
list = [33,23, 13, 43, 63,53,73,83,93, 3]
found = 1 # assume there is at least one item to sort
while (found == 1): #set infinite loop
pos = 0; # start with initial pos
for i in range(len(list)-1):
next = list[i+1] #get the next element
if list[i] > next :
list[i+1] = list[i] #do a swap
list[i] = next
#print list
pos = i;

          if pos == 0:
found = 0 # all sorted, exit loop

print list

def getTime(self):
localtime = time.asctime( time.localtime(time.time()))
print localtime;

if __name__ == “__main__”:
obj = BubbleSort()

Keep watching for few more useful posts on Python/Jython.


Thanks
R Vashi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s