AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Bubble sort algorithm - Python

Go down

Bubble sort algorithm - Python

Post by Lucaci Andrei on Mon Feb 13, 2012 5:43 am

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''
Created on Feb 6, 2012

@author: sin
'''
import random
n = 200
h = []
for i in range(n):
h.append(random.randint(0, n))

def buble(d):
for i in range(0, len(d) - 1):
swap = True
for j in range(0, len(d) - i - 1):
if d[j] > d[j + 1]:
d[j], d[j + 1] = d[j + 1], d[j]
swap = True
if swap == False:break

print h
buble(h)
print h

"""2nd sorting"""

random.shuffle(h)
print h
buble(h)
print h

def test():
l = [8, 4, 5, 6, 2, 4, 3, 2, 7, 5]
buble(l)
assert l == [2, 2, 3, 4, 4, 5, 5, 6, 7, 8]
assert l[3] < l[5]
test()

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum