-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sort.py
More file actions
39 lines (33 loc) · 767 Bytes
/
bubble-sort.py
File metadata and controls
39 lines (33 loc) · 767 Bytes
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
38
39
'''
Bubble Sort.
'''
from test import *
count = 0
def bubbleSort(a):
global count
j = 0
while True:
swapped = False
for i in range(0, len(a)-j-1):
if a[i] > a[i+1]:
count += 1
a[i],a[i+1] = a[i+1],a[i]
swapped = True
if swapped == False:
break
j += 1
def bubbleSort2(alist):
global count
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
count += 1
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
# bubbleSort(a)
# print a
# print "count: " + str(count)
bubbleSort2(a)
print a
print "count: " + str(count)