MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / comb_sort

Function comb_sort

sorts/comb_sort.py:22–52  ·  view source on GitHub ↗

Pure implementation of comb sort algorithm in Python :param data: mutable collection with comparable items :return: the same collection in ascending order Examples: >>> comb_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> comb_sort([]) [] >>> comb_sort([99, 45, -7, 8, 2, 0,

(data: list)

Source from the content-addressed store, hash-verified

20
21
22def comb_sort(data: list) -> list:
23 """Pure implementation of comb sort algorithm in Python
24 :param data: mutable collection with comparable items
25 :return: the same collection in ascending order
26 Examples:
27 >>> comb_sort([0, 5, 3, 2, 2])
28 [0, 2, 2, 3, 5]
29 >>> comb_sort([])
30 []
31 >>> comb_sort([99, 45, -7, 8, 2, 0, -15, 3])
32 [-15, -7, 0, 2, 3, 8, 45, 99]
33 """
34 shrink_factor = 1.3
35 gap = len(data)
36 completed = False
37
38 while not completed:
39 # Update the gap value for a next comb
40 gap = int(gap / shrink_factor)
41 if gap <= 1:
42 completed = True
43
44 index = 0
45 while index + gap < len(data):
46 if data[index] > data[index + gap]:
47 # Swap values
48 data[index], data[index + gap] = data[index + gap], data[index]
49 completed = False
50 index += 1
51
52 return data
53
54
55if __name__ == "__main__":

Callers 1

comb_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected