refactor: cleanup CombSort (#5303)

refactor: cleanup CombSort

Co-authored-by: Alex Klymenko <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-02 09:06:45 +02:00
committed by GitHub
parent 5113101e5d
commit fccd141014
2 changed files with 39 additions and 116 deletions

View File

@ -16,76 +16,57 @@ package com.thealgorithms.sorts;
* @see SortAlgorithm
*/
class CombSort implements SortAlgorithm {
private static final double SHRINK_FACTOR = 1.3;
// To find gap between elements
private int nextGap(int gap) {
// Shrink gap by Shrink factor
gap = (gap * 10) / 13;
/**
* Method to find the next gap
*
* @param gap the current gap
* @return the next gap value
*/
private int getNextGap(int gap) {
gap = (int) (gap / SHRINK_FACTOR);
return Math.max(gap, 1);
}
/**
* Function to sort arr[] using Comb
* Method to sort the array using CombSort
*
* @param arr - an array should be sorted
* @return sorted array
* @param arr the array to be sorted
* @param <T> the type of elements in the array
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] arr) {
int size = arr.length;
// initialize gap
int gap = size;
// Initialize swapped as true to make sure that loop runs
int gap = arr.length;
boolean swapped = true;
// Keep running while gap is more than 1 and last iteration caused a swap
while (gap != 1 || swapped) {
// Find next gap
gap = nextGap(gap);
// Initialize swapped as false so that we can check if swap happened or not
swapped = false;
// Compare all elements with current gap
for (int i = 0; i < size - gap; i++) {
if (SortUtils.less(arr[i + gap], arr[i])) {
// Swap arr[i] and arr[i+gap]
SortUtils.swap(arr, i, i + gap);
swapped = true;
}
}
gap = getNextGap(gap);
swapped = performSwaps(arr, gap);
}
return arr;
}
// Driver method
public static void main(String[] args) {
CombSort ob = new CombSort();
Integer[] arr = {
8,
4,
1,
56,
3,
-44,
-1,
0,
36,
34,
8,
12,
-66,
-78,
23,
-6,
28,
0,
};
ob.sort(arr);
/**
* Method to perform the swapping of elements in the array based on the current gap
*
* @param arr the array to be sorted
* @param gap the current gap
* @param <T> the type of elements in the array
* @return true if a swap occurred, false otherwise
*/
private <T extends Comparable<T>> boolean performSwaps(final T[] arr, final int gap) {
boolean swapped = false;
System.out.println("sorted array");
SortUtils.print(arr);
for (int i = 0; i < arr.length - gap; i++) {
if (SortUtils.less(arr[i + gap], arr[i])) {
SortUtils.swap(arr, i, i + gap);
swapped = true;
}
}
return swapped;
}
}