From bed927bb6c390dce3d8280f080360dde60769769 Mon Sep 17 00:00:00 2001 From: chrisjaimes <45322800+chrisjaimes@users.noreply.github.com> Date: Fri, 28 Oct 2022 00:19:41 -0400 Subject: [PATCH] Bael 5723 gravity sort and unit test (#12891) * add Gravity Sort java class * gravity sort in java with unit test * Update GravitySort.java fix name in setupAbacus method * Update GravitySortUnitTest.java fix name in unit test method Co-authored-by: Christian Jaimes --- .../algorithms/gravitysort/GravitySort.java | 60 +++++++++++++++++++ .../gravitysort/GravitySortUnitTest.java | 15 +++++ 2 files changed, 75 insertions(+) create mode 100644 algorithms-modules/algorithms-sorting-2/src/main/java/com/baeldung/algorithms/gravitysort/GravitySort.java create mode 100644 algorithms-modules/algorithms-sorting-2/src/test/java/com/baeldung/algorithms/gravitysort/GravitySortUnitTest.java diff --git a/algorithms-modules/algorithms-sorting-2/src/main/java/com/baeldung/algorithms/gravitysort/GravitySort.java b/algorithms-modules/algorithms-sorting-2/src/main/java/com/baeldung/algorithms/gravitysort/GravitySort.java new file mode 100644 index 0000000000..4d60dbbf25 --- /dev/null +++ b/algorithms-modules/algorithms-sorting-2/src/main/java/com/baeldung/algorithms/gravitysort/GravitySort.java @@ -0,0 +1,60 @@ +package com.baeldung.algorithms.gravitysort; + +public class GravitySort { + + public static int findMax(int[] A) { + int max = A[0]; + for (int i = 1; i< A.length; i++) { + if (A[i] > max) { + max = A[i]; + } + } + return max; + } + + public static boolean[][] setupAbacus(int[] A, int m) { + boolean[][] abacus = new boolean[A.length][m]; + for (int i = 0; i < abacus.length; i++) { + int number = A[i]; + for (int j = 0; j < abacus[0].length && j < number; j++) { + abacus[A.length - 1 - i][j] = true; + } + } + return abacus; + } + + public static void dropBeads(boolean[][] abacus, int[] A, int m) { + for (int i = 1; i < A.length; i++) { + for (int j = m - 1; j >= 0; j--) { + if (abacus[i][j] == true) { + int x = i; + while (x > 0 && abacus[x - 1][j] == false) { + boolean temp = abacus[x - 1][j]; + abacus[x - 1][j] = abacus[x][j]; + abacus[x][j] = temp; + x--; + } + } + } + } + } + + public static void transformToList(boolean[][] abacus, int[] A) { + int index = 0; + for (int i = abacus.length - 1; i >= 0; i--) { + int beads = 0; + for (int j = 0; j < abacus[0].length && abacus[i][j] == true; j++) { + beads++; + } + A[index++] = beads; + } + } + + public static void sort(int[] A) { + int m = findMax(A); + boolean[][] abacus = setupAbacus(A, m); + dropBeads(abacus, A, m); + // transform abacus into sorted list + transformToList(abacus, A); + } +} diff --git a/algorithms-modules/algorithms-sorting-2/src/test/java/com/baeldung/algorithms/gravitysort/GravitySortUnitTest.java b/algorithms-modules/algorithms-sorting-2/src/test/java/com/baeldung/algorithms/gravitysort/GravitySortUnitTest.java new file mode 100644 index 0000000000..89fc1ed687 --- /dev/null +++ b/algorithms-modules/algorithms-sorting-2/src/test/java/com/baeldung/algorithms/gravitysort/GravitySortUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.algorithms.gravitysort; + +import org.junit.Assert; +import org.junit.Test; + +public class GravitySortUnitTest { + + @Test + public void givenIntegerArray_whenSortedWithGravitySort_thenGetSortedArray() { + int[] actual = { 9, 9, 100, 3, 57, 12, 3, 78, 0, 2, 2, 40, 21, 9 }; + int[] expected = { 0, 2, 2, 3, 3, 9, 9, 9, 12, 21, 40, 57, 78, 100 }; + GravitySort.sort(actual); + Assert.assertArrayEquals(expected, actual); + } +}