-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbubblesort.py
45 lines (42 loc) · 1.14 KB
/
bubblesort.py
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
40
41
42
43
44
45
#!/usr/bin/python
#############################################################
# Bubblesort for Python #
# by: Dennis Linuz <[email protected]> #
# Demonstration of a bubble #
#############################################################
import sys,time
input_array=[]
if (len(sys.argv) < 2):
print ""
print "Please input list items as arguments"
print "\nExample: ./bubblesort.py <item1> [item2] [item3] [item4] ..."
quit()
i=1
while (i < len(sys.argv)):
input_array.append(int(sys.argv[i]))
i+=1
def bubbleSort(array):
length=len(array)
result = True
global count
while result:
result = False
i=0
while (i < length-1):
if (array[i] > array[i+1]):
tempVar = array[i]
array[i] = array[i+1]
array[i+1] = tempVar
result = True
i=i+1
count+=1
print "Sorting: " + str(array)
return array
count = 0
time1 = time.time()
arrayResult = str(bubbleSort(input_array))
print ""
print "Sorted after " + str(count) + " tries."
print "Sorted: " + arrayResult
print "---"
print "Overall Time: " + str(time.time()-time1) + " seconds"