CombSort.java#287
Merged
Merged
Conversation
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
requested changes
Apr 2, 2018
christianbender
left a comment
There was a problem hiding this comment.
Good Work! I have some requests
| { | ||
| // Shrink gap by Shrink factor | ||
| gap = (gap*10)/13; | ||
| if (gap < 1) |
There was a problem hiding this comment.
Use curly braces. You can remove the if complete and writing instead gap = (gap < 1 ) ? 1 : gap;
| ob.sort(arr); | ||
|
|
||
| System.out.println("sorted array"); | ||
| for (int i=0; i<arr.length; ++i) |
| boolean swapped = true; | ||
|
|
||
| // Keep running while gap is more than 1 and last iteration caused a swap | ||
| while (gap != 1 || swapped == true) |
There was a problem hiding this comment.
You can write swapped instead of swapped == true
| class CombSort | ||
| { | ||
| // To find gap between elements | ||
| int getNextGap(int gap) |
There was a problem hiding this comment.
You can this methods declare as static. If you do this you can the methods right away use in the main-method.
| } | ||
|
|
||
| // Function to sort arr[] using Comb Sort | ||
| void sort(int arr[]) |
There was a problem hiding this comment.
You can this methods declare as static. If you do this you can the methods right away use in the main-method.
| // Driver method | ||
| public static void main(String args[]) | ||
| { | ||
| CombSort ob = new CombSort(); |
There was a problem hiding this comment.
Redundant: see my comments above.
As requested done the changes.
Author
|
Thank You. Done all the changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).