From 35f23d2ddc26a163e9ec645fc5146c1bab3d24b1 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 26 Aug 2024 09:33:24 +0200 Subject: [PATCH] refactor: `BoyerMoore` (#5395) --- .../com/thealgorithms/others/BoyerMoore.java | 75 +++++++++++++------ .../thealgorithms/others/BoyerMooreTest.java | 4 +- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index e67427de..3fb97724 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -1,54 +1,81 @@ -/* this Code is the illustration of Boyer moore's voting algorithm to -find the majority element is an array that appears more than n/2 times in an array -where "n" is the length of the array. -For more information on the algorithm refer -https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm - */ package com.thealgorithms.others; import java.util.Optional; +/** + * Utility class implementing Boyer-Moore's Voting Algorithm to find the majority element + * in an array. The majority element is defined as the element that appears more than n/2 times + * in the array, where n is the length of the array. + * + * For more information on the algorithm, refer to: + * https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm + */ public final class BoyerMoore { private BoyerMoore() { } - public static Optional findMajor(final int[] a) { - final var candidate = findCandidate(a); - final var count = countOccurrences(candidate, a); - if (isMajority(count, a.length)) { + /** + * Finds the majority element in the given array if it exists. + * + * @param array the input array + * @return an Optional containing the majority element if it exists, otherwise an empty Optional + */ + public static Optional findMajorityElement(int[] array) { + if (array == null || array.length == 0) { + return Optional.empty(); + } + + int candidate = findCandidate(array); + int count = countOccurrences(candidate, array); + + if (isMajority(count, array.length)) { return Optional.of(candidate); } return Optional.empty(); } - private static int findCandidate(final int[] a) { + /** + * Identifies the potential majority candidate using Boyer-Moore's Voting Algorithm. + * + * @param array the input array + * @return the candidate for the majority element + */ + private static int findCandidate(final int[] array) { int count = 0; int candidate = -1; - for (final var k : a) { + for (int value : array) { if (count == 0) { - candidate = k; - count = 1; - } else { - if (k == candidate) { - count++; - } else { - count--; - } + candidate = value; } + count += (value == candidate) ? 1 : -1; } return candidate; } - private static int countOccurrences(final int candidate, final int[] a) { + /** + * Counts the occurrences of the candidate element in the array. + * + * @param candidate the candidate element + * @param array the input array + * @return the number of times the candidate appears in the array + */ + private static int countOccurrences(final int candidate, final int[] array) { int count = 0; - for (final var j : a) { - if (j == candidate) { + for (int value : array) { + if (value == candidate) { count++; } } return count; } - private static boolean isMajority(final int count, final int totalCount) { + /** + * Determines if the count of the candidate element is more than n/2, where n is the length of the array. + * + * @param count the number of occurrences of the candidate + * @param totalCount the total number of elements in the array + * @return true if the candidate is the majority element, false otherwise + */ + private static boolean isMajority(int count, int totalCount) { return 2 * count > totalCount; } } diff --git a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java index b6620793..8416535b 100644 --- a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java +++ b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java @@ -11,7 +11,7 @@ public class BoyerMooreTest { @ParameterizedTest @MethodSource("inputStreamWithExistingMajority") void checkWhenMajorityExists(int expected, int[] input) { - Assertions.assertEquals(expected, BoyerMoore.findMajor(input).get()); + Assertions.assertEquals(expected, BoyerMoore.findMajorityElement(input).get()); } private static Stream inputStreamWithExistingMajority() { @@ -21,7 +21,7 @@ public class BoyerMooreTest { @ParameterizedTest @MethodSource("inputStreamWithoutMajority") void checkWhenMajorityExists(int[] input) { - Assertions.assertFalse(BoyerMoore.findMajor(input).isPresent()); + Assertions.assertFalse(BoyerMoore.findMajorityElement(input).isPresent()); } private static Stream inputStreamWithoutMajority() {