diff --git a/pom.xml b/pom.xml index 4db680c0..2b08c44d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 com.rampatra.adsj @@ -31,7 +32,8 @@ https://github.com/rampatra/Algorithms-and-Data-Structures-in-Java scm:git:git://github.com/rampatra/Algorithms-and-Data-Structures-in-Java.git - scm:git:ssh://git@github.com/rampatra/Algorithms-and-Data-Structures-in-Java.git + scm:git:ssh://git@github.com/rampatra/Algorithms-and-Data-Structures-in-Java.git + HEAD @@ -60,57 +62,110 @@ junit-jupiter-api 5.5.1 + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-params + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.6.2 + test + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + test + + + junit + junit + 4.13 + test + + + org.openjfx + javafx-base + 16 + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + compile + + - - - release - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/test/Trung/BubbleSort.java b/src/main/test/Trung/BubbleSort.java new file mode 100644 index 00000000..d837ba99 --- /dev/null +++ b/src/main/test/Trung/BubbleSort.java @@ -0,0 +1,50 @@ +import java.util.Arrays; + + +/** + * @author rpatra16 + * @since 03/11/2018 + */ +public class BubbleSort { + + /** + * In bubble sort, we start at the beginning of the array and swap + * the first two elements if the first is greater than the second. + * Then, we go to the next pair, and so on, continuously making sweeps + * of the array until it is sorted. In doing so, the smaller items + * slowly "bubble" up to the beginning of the list and in each inner + * iteration the largest element is sorted. Ergo, the inner loop runs + * until {@code length - i - 1} times. Time complexity: O(n^2). Space + * complexity: O(1), in place. To learn more: {@see https://youtu.be/6Gv8vg0kcHc} + * + * @param ar to be sorted + */ + public static void bubbleSort(int[] ar) { + for (int i = 0; i < ar.length - 1; i++) { + for (int j = 0; j < ar.length - i - 1; j++) { + if (ar[j] > ar[j + 1]) { + swap(ar, j, j + 1); + } + } + } + } + + public static void swap(int[] ar, int i, int j) { + int temp = ar[i]; + ar[i] = ar[j]; + ar[j] = temp; + } + + public static void main(String[] args) { + int[] ar = {2, 5, 1, 7, 8}; + System.out.println(Arrays.toString(ar)); + bubbleSort(ar); + System.out.println(Arrays.toString(ar)); + ar = new int[]{7, 5, 1, 7, 8, 0, 23}; + System.out.println(Arrays.toString(ar)); + bubbleSort(ar); + System.out.println(Arrays.toString(ar)); + } +} + + diff --git a/src/main/test/java/TestSort.java b/src/main/test/java/TestSort.java new file mode 100644 index 00000000..9da33526 --- /dev/null +++ b/src/main/test/java/TestSort.java @@ -0,0 +1,75 @@ +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class TestSort { + @BeforeAll + public static void BeforeAll() { + System.out.println("Start All Test"); + } + + @AfterAll + public static void AfterAll() { + System.out.println("End All Test"); + } + + @BeforeEach + public void BeforeEach() { + System.out.println("Begin Test"); + } + + @AfterEach + public void AfterEach() { + System.out.println("After Test"); + } + + @Test + @DisplayName("Default Test") + void testArray() { + int[] ar = {2, 5, 1, 7, 8}; + BubbleSort.bubbleSort(ar); + int[] expected = {1, 2, 5, 7, 8}; + assertArrayEquals(expected, ar); + } + + @Test + @Tag("Array") + @DisplayName("Test With Empty Array") + public void testEmptyArray() { + int[] ar = {}; + BubbleSort.bubbleSort(ar); + int[] expected = {}; + assertArrayEquals(expected, ar); + } + + @Test + @Tag("Array") + @DisplayName("Test With Duplicate Element") + public void testDuplicateParameter() { + int[] ar = {9, 5, 6, 0, 5, 6, 11, 2, 4}; + BubbleSort.bubbleSort(ar); + int[] expected = {0, 2, 4, 5, 5, 6, 6, 9, 11}; + assertArrayEquals(expected, ar); + } + + @Test + @Tag("Array") + @DisplayName("Test With Negative Integer Element") + public void testSameArray() { + int[] ar = {-1, -2, 5, 7, -8}; + BubbleSort.bubbleSort(ar); + int[] expected = {-8, -2, -1, 5, 7}; + Assertions.assertArrayEquals(expected, ar); + } + + @Test + @Tag("Array") + @DisplayName("Test With Array Reverse") + public void testReverseArray() { + int[] ar = {8, 7, 5, 2, 1}; + BubbleSort.bubbleSort(ar); + int[] expected = {1, 2, 5, 7, 8}; + assertArrayEquals(expected, ar); + } +} +