From 277b148124cabaa468eca1abe839493dc3048bd8 Mon Sep 17 00:00:00 2001 From: irohitsatya Date: Tue, 27 Oct 2020 14:51:04 +0530 Subject: [PATCH 1/2] Add bubble sort Add bubble sort --- .../tdd/algorithms/SortingAlgorithms.java | 34 +++++++++++++++++++ .../tdd/algorithms/BubbleSortTest.java | 22 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java create mode 100644 src/test/java/com/ordestiny/tdd/algorithms/BubbleSortTest.java diff --git a/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java b/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java new file mode 100644 index 0000000..3ddc3e8 --- /dev/null +++ b/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java @@ -0,0 +1,34 @@ +package com.ordestiny.tdd.algorithms; + +public class SortingAlgorithms { + + /** + * Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in decreasing order. + * + * @param array + */ + public static int[] bubbleSort(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + for (int j = i + 1; j < array.length; j++) { + if (array[i] > array[j]) { + swap(i, j, array); + } + } + } + return array; + } + + /** + * Swaps values at given indices for an array. + * + * @param i + * @param j + * @param array + */ + private static void swap(int i, int j, int[] array) { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + +} diff --git a/src/test/java/com/ordestiny/tdd/algorithms/BubbleSortTest.java b/src/test/java/com/ordestiny/tdd/algorithms/BubbleSortTest.java new file mode 100644 index 0000000..d36ffa0 --- /dev/null +++ b/src/test/java/com/ordestiny/tdd/algorithms/BubbleSortTest.java @@ -0,0 +1,22 @@ +package com.ordestiny.tdd.algorithms; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class BubbleSortTest { + + private final int[] notSortedArray = {8, 7, 6, 5, 4, 3, 2, 1, 0}; + private final int[] sortedArray = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + + @Test + public void bubbleSort_IntegerArray_notSorted() { + assertArrayEquals(sortedArray, SortingAlgorithms.bubbleSort(notSortedArray)); + } + + @Test + public void bubbleSort_IntegerArray_sorted() { + assertArrayEquals(sortedArray, SortingAlgorithms.bubbleSort(sortedArray)); + } + +} From dfd1b80a16c2157be6d948e9f56af79f55e0aa56 Mon Sep 17 00:00:00 2001 From: irohitsatya Date: Tue, 27 Oct 2020 15:00:54 +0530 Subject: [PATCH 2/2] Remove some lines --- .../ordestiny/tdd/algorithms/SortingAlgorithms.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java b/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java index 3ddc3e8..3eca593 100644 --- a/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java +++ b/src/main/java/com/ordestiny/tdd/algorithms/SortingAlgorithms.java @@ -4,23 +4,18 @@ public class SortingAlgorithms { /** * Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in decreasing order. - * * @param array */ public static int[] bubbleSort(int[] array) { - for (int i = 0; i < array.length - 1; i++) { - for (int j = i + 1; j < array.length; j++) { - if (array[i] > array[j]) { + for (int i = 0; i < array.length - 1; i++) + for (int j = i + 1; j < array.length; j++) + if (array[i] > array[j]) swap(i, j, array); - } - } - } return array; } /** * Swaps values at given indices for an array. - * * @param i * @param j * @param array