Skip to content

Commit d3eb64a

Browse files
committed
modify bubble-sort
1 parent f80fb7d commit d3eb64a

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

‎bubble-sort.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44

55
defbubble_sort(L):
6-
forminrange(len(L)):
7-
n=0
8-
forninrange(len(L)):
9-
ifL[m] <L[n]:
10-
L[m], L[n] =L[n], L[m]
6+
iflen(L) <2:
7+
returnL
8+
foriinrange(len(L)):
9+
forjinrange(1, len(L)):
10+
ifL[j-1] >L[j]:
11+
L[j-1], L[j] =L[j], L[j-1]
1112
returnL
1213

1314
if__name__=='__main__':
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# -*-coding: utf-8 -*-
1+
# coding=utf-8
22
fromrandomimportrandrange
33

44

5-
definsert_srot(L):
6-
iflen(L) <=0:
5+
definsert_sort(L):
6+
iflen(L) <2:
77
returnL
88
foriinrange(1, len(L)):
99
tmp=L[i]
1010
j=i-1
11-
whilej>=0andtmp<L[j]:
11+
whilej>=0andL[j]>tmp:
1212
L[j+1] =L[j]
1313
j-=1
1414
L[j+1] =tmp
1515

1616
returnL
1717

1818
if__name__=='__main__':
19-
L= [randrange(100) for_inrange(10)]
20-
print(insert_srot(L))
19+
L= [randrange(1000) for_inrange(10)]
20+
print(insert_sort(L))

0 commit comments

Comments
(0)