Skip to content

Commit 197f637

Browse files
authored
Merge branch 'master' into refactor/insertionsort
2 parents a7fcdc2 + 046f5a4 commit 197f637

File tree

5 files changed

+224
-144
lines changed

5 files changed

+224
-144
lines changed

src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,108 @@
22

33
import java.util.BitSet;
44

5+
/**
6+
* A generic BloomFilter implementation for probabilistic membership checking.
7+
*
8+
* @param <T> The type of elements to be stored in the Bloom filter.
9+
*/
510
public class BloomFilter<T> {
611

7-
private int numberOfHashFunctions;
8-
private BitSet bitArray;
9-
private Hash<T>[] hashFunctions;
12+
private final int numberOfHashFunctions;
13+
private final BitSet bitArray;
14+
private final Hash<T>[] hashFunctions;
1015

11-
public BloomFilter(int numberOfHashFunctions, int n) {
16+
/**
17+
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
18+
*
19+
* @param numberOfHashFunctions the number of hash functions to use
20+
* @param bitArraySize the size of the bit array
21+
*/
22+
@SuppressWarnings("unchecked")
23+
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
1224
this.numberOfHashFunctions = numberOfHashFunctions;
13-
hashFunctions = new Hash[numberOfHashFunctions];
14-
bitArray = new BitSet(n);
15-
insertHash();
25+
this.bitArray = new BitSet(bitArraySize);
26+
this.hashFunctions = new Hash[numberOfHashFunctions];
27+
initializeHashFunctions();
1628
}
1729

18-
private void insertHash() {
30+
/**
31+
* Initializes the hash functions with unique indices.
32+
*/
33+
private void initializeHashFunctions() {
1934
for (int i = 0; i < numberOfHashFunctions; i++) {
20-
hashFunctions[i] = new Hash(i);
35+
hashFunctions[i] = new Hash<>(i);
2136
}
2237
}
2338

39+
/**
40+
* Inserts an element into the Bloom filter.
41+
*
42+
* @param key the element to insert
43+
*/
2444
public void insert(T key) {
2545
for (Hash<T> hash : hashFunctions) {
26-
int position = hash.compute(key) % bitArray.size();
46+
int position = Math.abs(hash.compute(key) % bitArray.size());
2747
bitArray.set(position);
2848
}
2949
}
3050

51+
/**
52+
* Checks if an element might be in the Bloom filter.
53+
*
54+
* @param key the element to check
55+
* @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not
56+
*/
3157
public boolean contains(T key) {
3258
for (Hash<T> hash : hashFunctions) {
33-
int position = hash.compute(key) % bitArray.size();
59+
int position = Math.abs(hash.compute(key) % bitArray.size());
3460
if (!bitArray.get(position)) {
3561
return false;
3662
}
3763
}
3864
return true;
3965
}
4066

41-
private class Hash<T> {
67+
/**
68+
* Inner class representing a hash function used by the Bloom filter.
69+
*
70+
* @param <T> The type of elements to be hashed.
71+
*/
72+
private static class Hash<T> {
4273

43-
int index;
74+
private final int index;
4475

76+
/**
77+
* Constructs a Hash function with a specified index.
78+
*
79+
* @param index the index of this hash function
80+
*/
4581
Hash(int index) {
4682
this.index = index;
4783
}
4884

85+
/**
86+
* Computes the hash of the given key.
87+
*
88+
* @param key the element to hash
89+
* @return the hash value
90+
*/
4991
public int compute(T key) {
5092
return index * asciiString(String.valueOf(key));
5193
}
5294

95+
/**
96+
* Computes the ASCII value sum of the characters in a string.
97+
*
98+
* @param word the string to compute
99+
* @return the sum of ASCII values of the characters
100+
*/
53101
private int asciiString(String word) {
54-
int number = 0;
55-
for (int i = 0; i < word.length(); i++) {
56-
number += word.charAt(i);
102+
int sum = 0;
103+
for (char c : word.toCharArray()) {
104+
sum += c;
57105
}
58-
return number;
106+
return sum;
59107
}
60108
}
61109
}

src/main/java/com/thealgorithms/sorts/CycleSort.java

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,79 @@ class CycleSort implements SortAlgorithm {
99
/**
1010
* Sorts an array of comparable elements using the cycle sort algorithm.
1111
*
12-
* @param array the array to be sorted
13-
* @param <T> the type of elements in the array, must be comparable
14-
* @return the sorted array
12+
* @param <T> The type of elements in the array, which must be comparable
13+
* @param array The array to be sorted
14+
* @return The sorted array
1515
*/
1616
@Override
17-
public <T extends Comparable<T>> T[] sort(T[] array) {
18-
for (int cycleStart = 0; cycleStart <= array.length - 2; cycleStart++) {
19-
T item = array[cycleStart];
17+
public <T extends Comparable<T>> T[] sort(final T[] array) {
18+
final int length = array.length;
2019

21-
// Find the position where we put the element
22-
int pos = cycleStart;
23-
for (int i = cycleStart + 1; i < array.length; i++) {
24-
if (SortUtils.less(array[i], item)) {
25-
pos++;
26-
}
27-
}
20+
for (int cycleStart = 0; cycleStart <= length - 2; cycleStart++) {
21+
T item = array[cycleStart];
22+
int pos = findPosition(array, cycleStart, item);
2823

29-
// If the item is already in the correct position
3024
if (pos == cycleStart) {
31-
continue;
32-
}
33-
34-
// Ignore all duplicate elements
35-
while (item.compareTo(array[pos]) == 0) {
36-
pos++;
25+
continue; // Item is already in the correct position
3726
}
3827

39-
// Put the item to its correct position
40-
if (pos != cycleStart) {
41-
item = replace(array, pos, item);
42-
}
28+
item = placeItem(array, item, pos);
4329

4430
// Rotate the rest of the cycle
4531
while (pos != cycleStart) {
46-
pos = cycleStart;
47-
48-
// Find the position where we put the element
49-
for (int i = cycleStart + 1; i < array.length; i++) {
50-
if (SortUtils.less(array[i], item)) {
51-
pos++;
52-
}
53-
}
54-
55-
// Ignore all duplicate elements
56-
while (item.compareTo(array[pos]) == 0) {
57-
pos++;
58-
}
59-
60-
// Put the item to its correct position
61-
if (item != array[pos]) {
62-
item = replace(array, pos, item);
63-
}
32+
pos = findPosition(array, cycleStart, item);
33+
item = placeItem(array, item, pos);
6434
}
6535
}
6636
return array;
6737
}
6838

39+
/**
40+
* Finds the correct position for the given item starting from cycleStart.
41+
*
42+
* @param <T> The type of elements in the array, which must be comparable
43+
* @param array The array to be sorted
44+
* @param cycleStart The starting index of the cycle
45+
* @param item The item whose position is to be found
46+
* @return The correct position of the item
47+
*/
48+
private <T extends Comparable<T>> int findPosition(final T[] array, final int cycleStart, final T item) {
49+
int pos = cycleStart;
50+
for (int i = cycleStart + 1; i < array.length; i++) {
51+
if (SortUtils.less(array[i], item)) {
52+
pos++;
53+
}
54+
}
55+
return pos;
56+
}
57+
58+
/**
59+
* Places the item in its correct position, handling duplicates, and returns the displaced item.
60+
*
61+
* @param <T> The type of elements in the array, which must be comparable
62+
* @param array The array being sorted
63+
* @param item The item to be placed
64+
* @param pos The position where the item is to be placed
65+
* @return The displaced item
66+
*/
67+
private <T extends Comparable<T>> T placeItem(final T[] array, final T item, int pos) {
68+
while (item.compareTo(array[pos]) == 0) {
69+
pos++;
70+
}
71+
return replace(array, pos, item);
72+
}
73+
6974
/**
7075
* Replaces an element in the array with the given item and returns the replaced item.
7176
*
72-
* @param array the array in which the replacement will occur
73-
* @param pos the position at which the replacement will occur
74-
* @param item the item to be placed in the array
75-
* @param <T> the type of elements in the array, must be comparable
76-
* @return the replaced item
77+
* @param <T> The type of elements in the array, which must be comparable
78+
* @param array The array in which the replacement will occur
79+
* @param pos The position at which the replacement will occur
80+
* @param item The item to be placed in the array
81+
* @return The replaced item
7782
*/
78-
private <T extends Comparable<T>> T replace(T[] array, int pos, T item) {
79-
T replacedItem = array[pos];
83+
private <T extends Comparable<T>> T replace(final T[] array, final int pos, final T item) {
84+
final T replacedItem = array[pos];
8085
array[pos] = item;
8186
return replacedItem;
8287
}
Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,65 @@
1-
// Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer
2-
// (similar to C/C++'s atoi function). Here is my implementation
3-
41
package com.thealgorithms.strings;
52

3+
/**
4+
* A utility class that provides a method to convert a string to a 32-bit signed integer (similar to C/C++'s atoi function).
5+
*/
66
public final class MyAtoi {
77
private MyAtoi() {
88
}
9-
public static int myAtoi(String s) {
10-
s = s.trim();
11-
char[] char1 = s.toCharArray();
12-
String number = "";
13-
boolean negative = false;
14-
boolean zero = false;
15-
boolean isDigit = false;
169

17-
for (char ch : char1) {
18-
if (Character.isDigit(ch)) {
19-
if (number.length() > 1 && !isDigit) {
20-
number = "0";
21-
break;
22-
}
23-
isDigit = true;
24-
if (zero) {
25-
number = "0";
26-
break;
27-
}
28-
if (ch >= '0' && ch <= '9') {
29-
number += ch;
30-
}
31-
} else if (ch == '-' && !isDigit) {
32-
number += "0";
33-
negative = true;
34-
} else if (ch == '+' && !isDigit) {
35-
number += "0";
36-
} else if (ch == '.' && isDigit) {
37-
break;
38-
} else if (ch == '.') {
39-
zero = true;
40-
} else {
41-
if (!isDigit) {
42-
number = "0";
43-
}
44-
break;
45-
}
10+
/**
11+
* Converts the given string to a 32-bit signed integer.
12+
* The conversion discards any leading whitespace characters until the first non-whitespace character is found.
13+
* Then, it takes an optional initial plus or minus sign followed by as many numerical digits as possible and interprets them as a numerical value.
14+
* The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
15+
*
16+
* If the number is out of the range of a 32-bit signed integer:
17+
* - Returns {@code Integer.MAX_VALUE} if the value exceeds {@code Integer.MAX_VALUE}.
18+
* - Returns {@code Integer.MIN_VALUE} if the value is less than {@code Integer.MIN_VALUE}.
19+
*
20+
* If no valid conversion could be performed, a zero is returned.
21+
*
22+
* @param s the string to convert
23+
* @return the converted integer, or 0 if the string cannot be converted to a valid integer
24+
*/
25+
public static int myAtoi(String s) {
26+
if (s == null || s.isEmpty()) {
27+
return 0;
4628
}
4729

48-
if (!isDigit) {
30+
s = s.trim();
31+
int length = s.length();
32+
if (length == 0) {
4933
return 0;
5034
}
5135

52-
number = number.replaceFirst("^0+(?!$)", "");
36+
int index = 0;
37+
boolean negative = false;
38+
39+
// Check for the sign
40+
if (s.charAt(index) == '-' || s.charAt(index) == '+') {
41+
negative = s.charAt(index) == '-';
42+
index++;
43+
}
5344

54-
if (number.length() > 10 && negative) {
55-
return -2147483648;
56-
} else if (number.length() > 10) {
57-
return 2147483647;
58-
} else if (number.length() == 10 && negative) {
59-
double db1 = Double.parseDouble(number);
60-
if (db1 >= 2147483648d) {
61-
return -2147483648;
45+
int number = 0;
46+
while (index < length) {
47+
char ch = s.charAt(index);
48+
if (!Character.isDigit(ch)) {
49+
break;
6250
}
63-
} else if (number.length() == 10) {
64-
double db1 = Double.parseDouble(number);
65-
if (db1 > (2147483647)) {
66-
return 2147483647;
51+
52+
int digit = ch - '0';
53+
54+
// Check for overflow
55+
if (number > (Integer.MAX_VALUE - digit) / 10) {
56+
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
6757
}
68-
}
6958

70-
if (negative) {
71-
return Integer.parseInt(number) * -1;
59+
number = number * 10 + digit;
60+
index++;
7261
}
7362

74-
return Integer.parseInt(number);
63+
return negative ? -number : number;
7564
}
7665
}

0 commit comments

Comments
 (0)