File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed
Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ '''Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares
2+ adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble
3+ Sort by using gap of size more than 1. The gap starts with a large value and shrinks by
4+ a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes
5+ more than one inversion counts with one swap and performs better than Bubble Sort.'''
6+
7+
8+ # Python program for implementation of CombSort
9+
10+
11+ def comb_sort (alist ):
12+ def swap (i , j ):
13+ alist [i ], alist [j ] = alist [j ], alist [i ]
14+
15+ gap = len (alist )
16+ shrink = 1.3
17+
18+ no_swap = False
19+ while not no_swap :
20+ gap = int (gap / shrink )
21+
22+ if gap < 1 :
23+ gap = 1
24+ no_swap = True
25+ else :
26+ no_swap = False
27+
28+ i = 0
29+ while i + gap < len (alist ):
30+ if alist [i ] > alist [i + gap ]:
31+ swap (i , i + gap )
32+ no_swap = False
33+ i = i + 1
34+
35+ #TEST CODE
36+ alist = input ('Enter the list of numbers separated by space: ' ).split ()
37+ alist = [int (x ) for x in alist ]
38+ comb_sort (alist )
39+ print ('Sorted list: ' , end = '' )
40+ print (alist )
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
You can’t perform that action at this time.
0 commit comments