-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathcomb_sort.py
More file actions
28 lines (28 loc) · 718 Bytes
/
comb_sort.py
File metadata and controls
28 lines (28 loc) · 718 Bytes
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
def get_next_gap(gap):
gap = (gap * 10) // 13
if gap < 1:
return 1
return gap
def comb_sort(a):
n = len(a)
gap = n
swapped = True
while gap != 1 or swapped:
gap = get_next_gap(gap)
swapped = False
for i in range(n - gap):
if a[i] > a[i + gap]:
a[i], a[i + gap] = a[i + gap], a[i]
swapped = True
if __name__ == "__main__":
n = int(input("Enter the length of the array: "))
a = []
print("Enter the elements of the array:")
for _ in range(n):
element = int(input())
a.append(element)
print("Original array:")
print(*a)
comb_sort(a)
print("Sorted array:")
print(*a)