-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbubble_sort.py
86 lines (63 loc) · 1.72 KB
/
bubble_sort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''
README
This module aims to introduce the popular sorting method--bubble sort
and three different kind of schemes are conducted by python 3 language.
'''
# -*- coding:utf-8 -*-
import math
class BubbleSortMethod(object):
# construction method
def __init__(self,array):
self.array=array
# the most common method, the maximum number in the array is swapped to the last position during an iteration.
def bubble_sort1(self):
array=self.array
n=len(array)
for i in range(n):
for j in range(n-i-1):
if array[j] > array[j+1]:
tmp=array[j]
array[j]=array[j+1]
array[j+1]=tmp
return array
# set a tag when each swapping operation occurs
def bubble_sort2(self):
array=self.array
n=len(array)
swap_tag=True
for i in range(n):
if swap_tag:
swap_tag=False
for j in range(n-i-1):
if array[j] > array[j+1]:
tmp=array[j]
array[j]=array[j+1]
array[j+1]=tmp
swap_tag=True # it means that the array has't been soted completely yet
else:
break
return array
# record the position where the last swapping operation occurs during the iterations
def bubble_sort3(self):
array=self.array
n=len(array)
swap_index=n
while swap_index>0:
k=swap_index
swap_index=0 # if no swapping operation occus, the loop will break
for j in range(k-1):
if array[j] > array[j+1]:
tmp=array[j]
array[j]=array[j+1]
array[j+1]=tmp
swap_index=j
return array
if __name__=='__main__':
testlist=[2,5,7,1,9,4,6,0,12,35,7]
bubble=BubbleSortMethod(testlist) # new object of BubbleSortMethod class
sortlist=bubble.bubble_sort1()
print(sortlist)
sortlist=bubble.bubble_sort2()
print(sortlist)
sortlist=bubble.bubble_sort3()
print(sortlist)