Skip to content

CombSort.java#287

Merged
christianbender merged 2 commits into
masterfrom
unknown repository
Apr 8, 2018
Merged

CombSort.java#287
christianbender merged 2 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Oct 28, 2017

Copy link
Copy Markdown

Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one inversion counts with one swap and performs better than Bubble Sort.

The shrink factor has been empirically found to be 1.3 (by testing CombSort on over 200,000 random lists) [Source: Wiki]

Although, it works better than Bubble Sort on average, worst case remains O(n2).

Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one inversion counts with one swap and performs better than Bubble Sort.

The shrink factor has been empirically found to be 1.3 (by testing CombSort on over 200,000 random lists) [Source: Wiki]

Although, it works better than Bubble Sort on average, worst case remains O(n2).

@christianbender christianbender left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Work! I have some requests

Comment thread Sorts/CombSort.java Outdated
{
// Shrink gap by Shrink factor
gap = (gap*10)/13;
if (gap < 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces. You can remove the if complete and writing instead gap = (gap < 1 ) ? 1 : gap;

Comment thread Sorts/CombSort.java Outdated
ob.sort(arr);

System.out.println("sorted array");
for (int i=0; i<arr.length; ++i)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces

Comment thread Sorts/CombSort.java Outdated
boolean swapped = true;

// Keep running while gap is more than 1 and last iteration caused a swap
while (gap != 1 || swapped == true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write swapped instead of swapped == true

Comment thread Sorts/CombSort.java Outdated
class CombSort
{
// To find gap between elements
int getNextGap(int gap)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can this methods declare as static. If you do this you can the methods right away use in the main-method.

Comment thread Sorts/CombSort.java Outdated
}

// Function to sort arr[] using Comb Sort
void sort(int arr[])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can this methods declare as static. If you do this you can the methods right away use in the main-method.

Comment thread Sorts/CombSort.java
// Driver method
public static void main(String args[])
{
CombSort ob = new CombSort();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant: see my comments above.

As requested done the changes.
@ghost

ghost commented Apr 5, 2018

Copy link
Copy Markdown
Author

Thank You. Done all the changes.

@christianbender christianbender merged commit 1f76cda into TheAlgorithms:master Apr 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants