Browse Source

Merge pull request #14 from ServinDC/master

Create CombSort.py
pull/15/head
Jeffery Russell 6 years ago
committed by GitHub
parent
commit
41f985838f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions
  1. +14
    -0
      sorting/CombSort.py

+ 14
- 0
sorting/CombSort.py View File

@ -0,0 +1,14 @@
def combsort(numbers_list):
ordered = numbers_list.copy()
gap = len(numbers_list) # initial gap (first and last element)
swaps = True
while swaps or gap!=1:
swaps = False
for i in range(len(numbers_list)-gap):
if ordered[i] > ordered[i+gap]: # swaps without extra variable
ordered[i] = ordered[i+gap] - ordered[i]
ordered[i+gap] = ordered[i+gap] - ordered[i]
ordered[i] = ordered[i+gap] + ordered[i]
swaps = True
gap = max(gap-1, 1) # update gap, minimum gap is 1
return ordered

Loading…
Cancel
Save