mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
Updated CombSort.java
As requested done the changes.
This commit is contained in:
@ -3,17 +3,15 @@
|
|||||||
class CombSort
|
class CombSort
|
||||||
{
|
{
|
||||||
// To find gap between elements
|
// To find gap between elements
|
||||||
int getNextGap(int gap)
|
static int getNextGap(int gap)
|
||||||
{
|
{
|
||||||
// Shrink gap by Shrink factor
|
// Shrink gap by Shrink factor
|
||||||
gap = (gap*10)/13;
|
gap = (gap*10)/13;
|
||||||
if (gap < 1)
|
gap = (gap < 1) ? 1: gap;
|
||||||
return 1;
|
|
||||||
return gap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to sort arr[] using Comb Sort
|
// Function to sort arr[] using Comb Sort
|
||||||
void sort(int arr[])
|
static void sort(int arr[])
|
||||||
{
|
{
|
||||||
int n = arr.length;
|
int n = arr.length;
|
||||||
|
|
||||||
@ -24,7 +22,7 @@ class CombSort
|
|||||||
boolean swapped = true;
|
boolean swapped = true;
|
||||||
|
|
||||||
// Keep running while gap is more than 1 and last iteration caused a swap
|
// Keep running while gap is more than 1 and last iteration caused a swap
|
||||||
while (gap != 1 || swapped == true)
|
while (gap != 1 || swapped)
|
||||||
{
|
{
|
||||||
// Find next gap
|
// Find next gap
|
||||||
gap = getNextGap(gap);
|
gap = getNextGap(gap);
|
||||||
@ -57,8 +55,8 @@ class CombSort
|
|||||||
ob.sort(arr);
|
ob.sort(arr);
|
||||||
|
|
||||||
System.out.println("sorted array");
|
System.out.println("sorted array");
|
||||||
for (int i=0; i<arr.length; ++i)
|
for (int i=0; i<arr.length; ++i) {
|
||||||
System.out.print(arr[i] + " ");
|
System.out.print(arr[i] + " ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user