Skip to content

Patch 1 #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Algorithms/Sorting Algorithms/CombSort.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 25 additions & 2 deletions Algorithms/Sorting Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ If r > l <br>

<details>
<summary>
6. <a href ="https://github.com/aswnss-m/Python/blob/main/Algorithms/Sorting%20Algorithms/RadixSort.py">Radix Sort</a><br>
7. <a href ="https://github.com/aswnss-m/Python/blob/main/Algorithms/Sorting%20Algorithms/RadixSort.py">Radix Sort</a><br>
<b>Radix sort</b> 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.
</summary><br>
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
<blockquote>
<ol>
Expand All @@ -198,6 +197,30 @@ Sort the elements based on the unit place digits (X=0).</li>
</details>


<!--------------------------------------------------------------------------------------------------------------------------------------------------------->
<!--Comb Sort -->
<!--------------------------------------------------------------------------------------------------------------------------------------------------------->
<details>
<summary>8.<a href="https://github.com/Anjan50/Python/blob/main/Algorithms/Sorting%20Algorithms/CombSort.py">Comb Sort</a>
<br>
<b>Comb Sort </b>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.
</summary>
<br>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:
<ol>
<li>Find all differences/gaps between each element to each element </li>
<li>Reduce gaps through the following equation: (number of elements in array)/(number of elements in array/first element)</li>
<li>Swap elements based on the gap between the 2 numbers.</li>
<li>Repeat until array is sorted.</li>
</ol><br>

<img src="https://cdn.emre.me/sorting/comb_sort.gif">

</details>





Expand Down