Skip to content

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 25 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
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 Jan 24, 2024
ff3ebe6
added test
555vedant Jan 24, 2024
e853320
test
555vedant Jan 24, 2024
f6bf822
fixed empty file
555vedant Jan 24, 2024
478b19b
modification in logic
555vedant Jan 27, 2024
d6c1fa7
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant Jan 28, 2024
4005d1b
Merge branch 'master' into vedant3
555vedant Jan 28, 2024
de919b3
added new test according to bubble
555vedant Jan 28, 2024
db42248
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant Jan 28, 2024
e7ef24c
Update src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
555vedant Jan 30, 2024
0180513
fixed code to the properexchangesort
555vedant Jan 30, 2024
aae3ceb
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant Jan 30, 2024
f84c94c
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant Jan 31, 2024
0f4e46a
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant Jan 31, 2024
1e23a87
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant Jan 31, 2024
fc38b20
Update src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
555vedant Jan 31, 2024
af748c4
Update src/main/java/com/thealgorithms/sorts/ExchangeSort.java
555vedant Jan 31, 2024
65c70a5
added documentaion of algoritum
555vedant Jan 31, 2024
ca83f5a
added documentation
555vedant Jan 31, 2024
04d8a3d
modified logic
555vedant Jan 31, 2024
e26c3c9
final chnage
555vedant Feb 1, 2024
2c21671
style: remove blank line
vil02 Feb 1, 2024
aa725e1
removed line
555vedant Feb 1, 2024
0dd01b2
Merge branch 'vedant3' of https://github.com/555vedant/Javaproj into …
555vedant Feb 1, 2024
df7fd04
Merge branch 'master' into vedant3
vil02 Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/main/java/com/thealgorithms/sorts/ExchangeSort.java
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.
*
* @author 555vedant(Vedant Kasar)
* @see SortAlgorithm
*/
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;

// 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;
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thealgorithms.sorts;

public class ExchangeSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new ExchangeSort();
}
}