From 742075d1ab717ba9e15b525faabf2e0f2b80ff20 Mon Sep 17 00:00:00 2001 From: Vihan <65143775+tinyCodersDen@users.noreply.github.com> Date: Sun, 27 Oct 2024 21:17:26 -0500 Subject: [PATCH 1/2] Create Comb Sort Program --- Algorithms/Sorting Algorithms/CombSort.py | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Algorithms/Sorting Algorithms/CombSort.py diff --git a/Algorithms/Sorting Algorithms/CombSort.py b/Algorithms/Sorting Algorithms/CombSort.py new file mode 100644 index 0000000..b683289 --- /dev/null +++ b/Algorithms/Sorting Algorithms/CombSort.py @@ -0,0 +1,43 @@ +# Time complexity: O(n^2) + +def getGap(gap): + + # Reduce Gap by a factor + gap = (gap * 10)/13 + if gap < 1: + return 1 + return gap + +# Comb Sort: +def combSort(arr): + n = len(arr) + + # Set gap to n at beginning: + gap = n + + # Set swapped to True so that while loop can run: + swapped = True + + # We keep running the while loop when gap>1 and the most recent iteration makes a swap: + while gap != 1 or swapped == 1: + + # Finding the coming gap: + gap = getGap(gap) + + # We set swap as false so we can check during for loop if a swap is needed: + swapped = False + + # Compare all elements with the current change in elements: + for i in range(0, n-gap): + if arr[i] > arr[i + gap]: + arr[i], arr[i + gap] = arr[i + gap], arr[i] + swapped = True + + +# Example array: +arr = [8, 3, 1, 30, -44, 23, -6, -28, 0] +combSort(arr) + +print ("Sorted array:") +for i in arr: + print(i) From 0ba848171a56834d72b095d54f3123cb323fc5bf Mon Sep 17 00:00:00 2001 From: Vihan <65143775+tinyCodersDen@users.noreply.github.com> Date: Sun, 27 Oct 2024 21:42:29 -0500 Subject: [PATCH 2/2] Update README with Comb Sort Explanation --- Algorithms/Sorting Algorithms/README.md | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Algorithms/Sorting Algorithms/README.md b/Algorithms/Sorting Algorithms/README.md index 0517486..9ce2dcb 100644 --- a/Algorithms/Sorting Algorithms/README.md +++ b/Algorithms/Sorting Algorithms/README.md @@ -172,13 +172,12 @@ If r > l
-6. Radix Sort
+7. Radix Sort
Radix sort is an integer sorting algorithm that sorts data with integer keys by grouping the keys by individual digits that share the same significant position and value (place value). Radix sort uses counting sort as a subroutine to sort an array of numbers.

Because integers can be used to represent strings (by hashing the strings to integers), radix sort works on data types other than just integers. Because radix sort is not comparison based, it is not bounded by \Omega(n \log n)Ω(nlogn) for running time — in fact, radix sort can perform in linear time. - ### Algorithm
    @@ -198,6 +197,30 @@ Sort the elements based on the unit place digits (X=0).
+ + + +
+8.Comb Sort +
+Comb Sort is a comparison based sorting technique that compares the differences between the elements of the list. These differences are called as gaps, and the gaps are then sorted out from the lowest negative numbers to the highest positive numbers. +
+
The array runs in O(n^2) time complexity, but helps make a difference array to solve other problems efficiently. +Algorithm + +To sort an array of size n in ascending order: +
    +
  1. Find all differences/gaps between each element to each element
  2. +
  3. Reduce gaps through the following equation: (number of elements in array)/(number of elements in array/first element)
  4. +
  5. Swap elements based on the gap between the 2 numbers.
  6. +
  7. Repeat until array is sorted.
  8. +

+ + + +
+ +