From 3ab12c27fd4d7689e565301c5e977e125280c921 Mon Sep 17 00:00:00 2001 From: David <32831710+ServinDC@users.noreply.github.com> Date: Fri, 26 Oct 2018 20:06:10 -0500 Subject: [PATCH] Create CombSort.py Improved version of the bubble-sort method. --- sorting/CombSort.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sorting/CombSort.py diff --git a/sorting/CombSort.py b/sorting/CombSort.py new file mode 100644 index 0000000..20ba3de --- /dev/null +++ b/sorting/CombSort.py @@ -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