MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / modiBubbleSort

Function modiBubbleSort

BubbleSort.py:13–23  ·  view source on GitHub ↗
(alist)

Source from the content-addressed store, hash-verified

11
12# 改进的冒泡排序, 加入一个校验, 如果某次循环发现没有发生数值交换, 直接跳出循环
13def modiBubbleSort(alist):
14 exchange = True
15 passnum = len(alist) - 1
16 while passnum >= 1 and exchange:
17 exchange = False
18 for i in range(passnum):
19 if alist[i] > alist[i+1]:
20 alist[i], alist[i+1] = alist[i+1], alist[i]
21 exchange = True
22 passnum -= 1
23 return alist
24
25print(bubbleSort(alist))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected