File tree Expand file tree Collapse file tree 2 files changed +12
-11
lines changed
Expand file tree Collapse file tree 2 files changed +12
-11
lines changed Original file line number Diff line number Diff line change 33
44
55def bubble_sort (L ):
6- for m in range (len (L )):
7- n = 0
8- for n in range (len (L )):
9- if L [m ] < L [n ]:
10- L [m ], L [n ] = L [n ], L [m ]
6+ if len (L ) < 2 :
7+ return L
8+ for i in range (len (L )):
9+ for j in range (1 , len (L )):
10+ if L [j - 1 ] > L [j ]:
11+ L [j - 1 ], L [j ] = L [j ], L [j - 1 ]
1112 return L
1213
1314if __name__ == '__main__' :
Original file line number Diff line number Diff line change 1- # -*- coding: utf-8 -*-
1+ # coding= utf-8
22from random import randrange
33
44
5- def insert_srot (L ):
6- if len (L ) <= 0 :
5+ def insert_sort (L ):
6+ if len (L ) < 2 :
77 return L
88 for i in range (1 , len (L )):
99 tmp = L [i ]
1010 j = i - 1
11- while j >= 0 and tmp < L [j ]:
11+ while j >= 0 and L [j ] > tmp :
1212 L [j + 1 ] = L [j ]
1313 j -= 1
1414 L [j + 1 ] = tmp
1515
1616 return L
1717
1818if __name__ == '__main__' :
19- L = [randrange (100 ) for _ in range (10 )]
20- print (insert_srot (L ))
19+ L = [randrange (1000 ) for _ in range (10 )]
20+ print (insert_sort (L ))
You can’t perform that action at this time.
0 commit comments