Repository: TheAlgorithms/Java
Pull request #5029 (2024-01-24 20:06:40)

Add `ExchangeSort`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [ ] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

555vedant: pls suggest me whats the error im doing i will fix it up till my branch get into merge
555vedant: Done sir
555vedant: @vil02  sir , I am done with it!!
555vedant: now please can you merge it !
555vedant: all clear 

Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/ExchangeSort.java b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java
new file mode 100644
index 00000000..28303430
--- /dev/null
+++ b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java
@@ -0,0 +1,47 @@
+package com.thealgorithms.sorts;
+
+/**
+ * ExchangeSort is an implementation of the Exchange Sort algorithm.
+ *
+ * <p>
+ * Exchange sort works by comparing each element with all subsequent elements,
+ * swapping where needed, to ensure the correct placement of each element
+ * in the final sorted order. It iteratively performs this process for each
+ * element in the array. While it lacks the advantage of bubble sort in
+ * detecting sorted lists in one pass, it can be more efficient than bubble sort
+ * due to a constant factor (one less pass over the data to be sorted; half as
+ * many total comparisons) in worst-case scenarios.
+ * </p>
+ *
+ * <p>
+ * Reference: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort
+ * </p>
+ *
+ * @author 555vedant (Vedant Kasar)
+ */
+class ExchangeSort implements SortAlgorithm {
+    /**
+     * Implementation of Exchange 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) {
+        for (int i = 0; i < array.length - 1; i++) {
+            for (int j = i + 1; j < array.length; j++) {
+                if (array[i].compareTo(array[j]) > 0) {
+                    swap(array, i, j);
+                }
+            }
+        }
+        return array;
+    }
+
+    private <T> void swap(T[] array, int i, int j) {
+        T temp = array[i];
+        array[i] = array[j];
+        array[j] = temp;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java b/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
new file mode 100644
index 00000000..6c4271fa
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java
@@ -0,0 +1,8 @@
+package com.thealgorithms.sorts;
+
+public class ExchangeSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new ExchangeSort();
+    }
+}