-
Notifications
You must be signed in to change notification settings - Fork 20k
Add ExchangeSort
#5029
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
Merged
Merged
Add ExchangeSort
#5029
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9026967
added ExchangeSort and its testcases
555vedant ff3ebe6
added test
555vedant e853320
test
555vedant f6bf822
fixed empty file
555vedant 478b19b
modification in logic
555vedant d6c1fa7
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant 4005d1b
Merge branch 'master' into vedant3
555vedant de919b3
added new test according to bubble
555vedant db42248
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant e7ef24c
Update src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
555vedant 0180513
fixed code to the properexchangesort
555vedant aae3ceb
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant f84c94c
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant 0f4e46a
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant 1e23a87
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant fc38b20
Update src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
555vedant af748c4
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant 65c70a5
added documentaion of algoritum
555vedant ca83f5a
added documentation
555vedant 04d8a3d
modified logic
555vedant e26c3c9
final chnage
555vedant 2c21671
style: remove blank line
vil02 aa725e1
removed line
555vedant 0dd01b2
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant df7fd04
Merge branch 'master' into vedant3
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static com.thealgorithms.sorts.SortUtils.*; | ||
|
||
/** | ||
* Exchange Sort (Cocktail Shaker Sort) implementation. | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author 555vedant(Vedant Kasar) | ||
* @see SortAlgorithm | ||
555vedant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
class ExchangeSort implements SortAlgorithm { | ||
|
||
/** | ||
* Implements generic exchange sort (Cocktail Shaker Sort) algorithm. | ||
* | ||
* @param array 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[] array) { | ||
int left = 0; | ||
int right = array.length - 1; | ||
|
||
while (left <= right) { | ||
boolean swapped = false; | ||
|
||
// Traverse from left to right | ||
for (int i = left; i < right; ++i) { | ||
if (greater(array[i], array[i + 1])) { | ||
swap(array, i, i + 1); | ||
swapped = true; | ||
} | ||
} | ||
|
||
// If no swap occurred, the array is already sorted | ||
if (!swapped) { | ||
break; | ||
} | ||
|
||
// Move the right boundary one position to the left | ||
--right; | ||
|
||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Traverse from right to left | ||
for (int i = right; i > left; --i) { | ||
if (greater(array[i - 1], array[i])) { | ||
swap(array, i - 1, i); | ||
swapped = true; | ||
} | ||
} | ||
|
||
// If no swap occurred, the array is already sorted | ||
if (!swapped) { | ||
break; | ||
} | ||
|
||
// Move the left boundary one position to the right | ||
++left; | ||
} | ||
|
||
return array; | ||
} | ||
} | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class ExchangeSortTest extends SortingAlgorithmTest { | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
SortAlgorithm getSortAlgorithm() { | ||
return new ExchangeSort(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.