Skip to content

Commit 5167f86

Browse files
Merge branch 'master' into master
2 parents 216be09 + 9aa2544 commit 5167f86

File tree

6 files changed

+122
-25
lines changed

6 files changed

+122
-25
lines changed

src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* [Brief description of what the algorithm does]
3+
* <p>
4+
* Time Complexity: O(n) [or appropriate complexity]
5+
* Space Complexity: O(n)
6+
* * @author Reshma Kakkirala
7+
*/
18
package com.thealgorithms.conversions;
29

310
import java.util.Arrays;

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,30 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3333
}
3434

3535
/**
36-
* Sorts a subarray of the given array using the standard Insertion Sort algorithm.
36+
* Sorts a subarray of the given items using the standard Insertion Sort algorithm.
3737
*
38-
* @param array The array to be sorted
39-
* @param lo The starting index of the subarray
40-
* @param hi The ending index of the subarray (exclusive)
41-
* @param <T> The type of elements in the array, which must be comparable
42-
* @return The sorted array
38+
* @param items The items to be sorted
39+
* @param startIndex The starting index of the subarray
40+
* @param endIndex The ending index of the subarray (exclusive)
41+
* @param <T> The type of elements in the items, which must be comparable
42+
* @return The sorted items
4343
*/
44-
public <T extends Comparable<T>> T[] sort(T[] array, final int lo, final int hi) {
45-
if (array == null || lo >= hi) {
46-
return array;
44+
public <T extends Comparable<T>> T[] sort(T[] items, final int startIndex, final int endIndex) {
45+
if (items == null || startIndex >= endIndex) {
46+
return items;
4747
}
4848

49-
for (int i = lo + 1; i < hi; i++) {
50-
final T key = array[i];
49+
for (int i = startIndex + 1; i < endIndex; i++) {
50+
final T key = items[i];
5151
int j = i - 1;
52-
while (j >= lo && SortUtils.less(key, array[j])) {
53-
array[j + 1] = array[j];
52+
while (j >= startIndex && SortUtils.less(key, items[j])) {
53+
items[j + 1] = items[j];
5454
j--;
5555
}
56-
array[j + 1] = key;
56+
items[j + 1] = key;
5757
}
5858

59-
return array;
59+
return items;
6060
}
6161

6262
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@SuppressWarnings("rawtypes")
1111
class MergeSort implements SortAlgorithm {
1212

13-
private Comparable[] aux;
13+
private Comparable[] tempArray;
1414

1515
/**
1616
* Generic merge sort algorithm.
@@ -26,7 +26,7 @@ class MergeSort implements SortAlgorithm {
2626
*/
2727
@Override
2828
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
29-
aux = new Comparable[unsorted.length];
29+
tempArray = new Comparable[unsorted.length];
3030
doSort(unsorted, 0, unsorted.length - 1);
3131
return unsorted;
3232
}
@@ -58,17 +58,17 @@ private <T extends Comparable<T>> void doSort(T[] arr, int left, int right) {
5858
private <T extends Comparable<T>> void merge(T[] arr, int left, int mid, int right) {
5959
int i = left;
6060
int j = mid + 1;
61-
System.arraycopy(arr, left, aux, left, right + 1 - left);
61+
System.arraycopy(arr, left, tempArray, left, right + 1 - left);
6262

6363
for (int k = left; k <= right; k++) {
6464
if (j > right) {
65-
arr[k] = (T) aux[i++];
65+
arr[k] = (T) tempArray[i++];
6666
} else if (i > mid) {
67-
arr[k] = (T) aux[j++];
68-
} else if (less(aux[j], aux[i])) {
69-
arr[k] = (T) aux[j++];
67+
arr[k] = (T) tempArray[j++];
68+
} else if (less(tempArray[j], tempArray[i])) {
69+
arr[k] = (T) tempArray[j++];
7070
} else {
71-
arr[k] = (T) aux[i++];
71+
arr[k] = (T) tempArray[i++];
7272
}
7373
}
7474
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
1515
}
1616

1717
for (int currentSize = 0; currentSize < array.length; currentSize++) {
18-
int maxIndex = findMaxIndex(array, currentSize);
18+
int maxIndex = findIndexOfMax(array, currentSize);
1919
SortUtils.flip(array, maxIndex, array.length - 1 - currentSize);
2020
}
2121

@@ -30,7 +30,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3030
* @param <T> the type of elements in the array
3131
* @return the index of the maximum element
3232
*/
33-
private <T extends Comparable<T>> int findMaxIndex(T[] array, int currentSize) {
33+
private <T extends Comparable<T>> int findIndexOfMax(T[] array, int currentSize) {
3434
T max = array[0];
3535
int maxIndex = 0;
3636
for (int i = 0; i < array.length - currentSize; i++) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.strings;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* Utility class to find the top-k most frequent words.
11+
*
12+
* <p>Words are ranked by frequency in descending order. For equal frequencies,
13+
* words are ranked in lexicographical ascending order.
14+
*
15+
* <p>Reference:
16+
* https://en.wikipedia.org/wiki/Top-k_problem
17+
*
18+
*/
19+
public final class TopKFrequentWords {
20+
private TopKFrequentWords() {
21+
}
22+
23+
/**
24+
* Finds the k most frequent words.
25+
*
26+
* @param words input array of words
27+
* @param k number of words to return
28+
* @return list of top-k words ordered by frequency then lexicographical order
29+
* @throws IllegalArgumentException if words is null, k is negative, or words contains null
30+
*/
31+
public static List<String> findTopKFrequentWords(String[] words, int k) {
32+
if (words == null) {
33+
throw new IllegalArgumentException("Input words array cannot be null.");
34+
}
35+
if (k < 0) {
36+
throw new IllegalArgumentException("k cannot be negative.");
37+
}
38+
if (k == 0 || words.length == 0) {
39+
return List.of();
40+
}
41+
42+
Map<String, Integer> frequency = new HashMap<>();
43+
for (String word : words) {
44+
if (word == null) {
45+
throw new IllegalArgumentException("Input words cannot contain null values.");
46+
}
47+
frequency.put(word, frequency.getOrDefault(word, 0) + 1);
48+
}
49+
50+
List<String> candidates = new ArrayList<>(frequency.keySet());
51+
candidates.sort(Comparator.<String>comparingInt(frequency::get).reversed().thenComparing(Comparator.naturalOrder()));
52+
53+
int limit = Math.min(k, candidates.size());
54+
return new ArrayList<>(candidates.subList(0, limit));
55+
}
56+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class TopKFrequentWordsTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("validTestCases")
16+
void testFindTopKFrequentWords(String[] words, int k, List<String> expected) {
17+
assertEquals(expected, TopKFrequentWords.findTopKFrequentWords(words, k));
18+
}
19+
20+
static Stream<Arguments> validTestCases() {
21+
return Stream.of(Arguments.of(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2, List.of("i", "love")), Arguments.of(new String[] {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"}, 4, List.of("the", "is", "sunny", "day")),
22+
Arguments.of(new String[] {"bbb", "aaa", "bbb", "aaa", "ccc"}, 2, List.of("aaa", "bbb")), Arguments.of(new String[] {"one", "two", "three"}, 10, List.of("one", "three", "two")), Arguments.of(new String[] {}, 3, List.of()), Arguments.of(new String[] {"x", "x", "y"}, 0, List.of()));
23+
}
24+
25+
@ParameterizedTest
26+
@MethodSource("invalidTestCases")
27+
void testFindTopKFrequentWordsInvalidInput(String[] words, int k) {
28+
assertThrows(IllegalArgumentException.class, () -> TopKFrequentWords.findTopKFrequentWords(words, k));
29+
}
30+
31+
static Stream<Arguments> invalidTestCases() {
32+
return Stream.of(Arguments.of((String[]) null, 1), Arguments.of(new String[] {"a", null, "b"}, 2), Arguments.of(new String[] {"a"}, -1));
33+
}
34+
}

0 commit comments

Comments
 (0)