Skip to content

Commit 267ae5f

Browse files
Merge branch 'master' into add-factorial-recursion
2 parents 0bc3398 + 1eb0d61 commit 267ae5f

File tree

3 files changed

+114
-22
lines changed

3 files changed

+114
-22
lines changed

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
package com.thealgorithms.sorts;
22

33
/**
4-
* @author Varun Upadhyay (https://github.com/varunu28)
5-
* @author Podshivalov Nikita (https://github.com/nikitap492)
4+
* QuickSort is a divide-and-conquer sorting algorithm.
5+
*
6+
* <p>The algorithm selects a pivot element and partitions the array into two
7+
* subarrays such that:
8+
* <ul>
9+
* <li>Elements smaller than the pivot are placed on the left</li>
10+
* <li>Elements greater than the pivot are placed on the right</li>
11+
* </ul>
12+
*
13+
* <p>The subarrays are then recursively sorted until the entire array is ordered.
14+
*
15+
* <p>This implementation uses randomization to reduce the probability of
16+
* encountering worst-case performance on already sorted inputs.
17+
*
18+
* <p>Time Complexity:
19+
* <ul>
20+
* <li>Best Case: O(n log n)</li>
21+
* <li>Average Case: O(n log n)</li>
22+
* <li>Worst Case: O(n^2)</li>
23+
* </ul>
24+
*
25+
* <p>Space Complexity: O(log n) due to recursion stack (in-place sorting).
26+
*
27+
* @author Varun Upadhyay
28+
* @author Podshivalov Nikita
629
* @see SortAlgorithm
730
*/
31+
832
class QuickSort implements SortAlgorithm {
933

10-
/**
11-
* Generic Quick Sort algorithm.
12-
*
13-
* Time Complexity:
14-
* - Best case: O(n log n) – pivot splits array roughly in half each time.
15-
* - Average case: O(n log n)
16-
* - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
17-
*
18-
* Space Complexity: O(log n) – recursion stack, in-place sorting.
19-
*
20-
* @see SortAlgorithm
21-
*/
2234
@Override
2335
public <T extends Comparable<T>> T[] sort(T[] array) {
2436
doSort(array, 0, array.length - 1);
@@ -28,27 +40,33 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2840
/**
2941
* The sorting process
3042
*
31-
* @param left The first index of an array
32-
* @param right The last index of an array
3343
* @param array The array to be sorted
44+
* @param left The first index of an array
45+
* @param right The last index of an array
3446
*/
3547
private static <T extends Comparable<T>> void doSort(T[] array, final int left, final int right) {
48+
// Continue sorting only if the subarray has more than one element
3649
if (left < right) {
50+
// Randomly choose a pivot and partition the array
3751
final int pivot = randomPartition(array, left, right);
52+
// Recursively sort elements before the pivot
3853
doSort(array, left, pivot - 1);
54+
// Recursively sort elements after the pivot
3955
doSort(array, pivot, right);
4056
}
4157
}
4258

4359
/**
44-
* Randomize the array to avoid the basically ordered sequences
60+
* Randomizes the array to avoid already ordered or nearly ordered sequences
4561
*
4662
* @param array The array to be sorted
47-
* @param left The first index of an array
63+
* @param left The first index of an array
4864
* @param right The last index of an array
4965
* @return the partition index of the array
5066
*/
5167
private static <T extends Comparable<T>> int randomPartition(T[] array, final int left, final int right) {
68+
// Randomizing the pivot helps avoid worst-case performance
69+
// for already sorted or nearly sorted arrays
5270
final int randomIndex = left + (int) (Math.random() * (right - left + 1));
5371
SortUtils.swap(array, randomIndex, right);
5472
return partition(array, left, right);
@@ -58,21 +76,26 @@ private static <T extends Comparable<T>> int randomPartition(T[] array, final in
5876
* This method finds the partition index for an array
5977
*
6078
* @param array The array to be sorted
61-
* @param left The first index of an array
62-
* @param right The last index of an array Finds the partition index of an
63-
* array
79+
* @param left The first index of an array
80+
* @param right The last index of an array
6481
*/
6582
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
6683
final int mid = (left + right) >>> 1;
84+
// Choose the middle element as the pivot
6785
final T pivot = array[mid];
68-
86+
// Move the left and right pointers towards each other
6987
while (left <= right) {
88+
// Move left pointer until an element >= pivot is found
7089
while (SortUtils.less(array[left], pivot)) {
7190
++left;
7291
}
92+
93+
// Move right pointer until an element <= pivot is found
7394
while (SortUtils.less(pivot, array[right])) {
7495
--right;
7596
}
97+
98+
// Swap elements that are on the wrong side of the pivot
7699
if (left <= right) {
77100
SortUtils.swap(array, left, right);
78101
++left;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* The {@code LengthOfLastWord} class provides a utility method to determine
5+
* the length of the last word in a given string.
6+
*
7+
* <p>A "word" is defined as a maximal substring consisting of non-space
8+
* characters only. Trailing spaces at the end of the string are ignored.
9+
*
10+
* <p><strong>Example:</strong>
11+
* <pre>{@code
12+
* LengthOfLastWord obj = new LengthOfLastWord();
13+
* System.out.println(obj.lengthOfLastWord("Hello World")); // Output: 5
14+
* System.out.println(obj.lengthOfLastWord(" fly me to the moon ")); // Output: 4
15+
* System.out.println(obj.lengthOfLastWord("luffy is still joyboy")); // Output: 6
16+
* }</pre>
17+
*
18+
* <p>This implementation runs in O(n) time complexity, where n is the length
19+
* of the input string, and uses O(1) additional space.
20+
*/
21+
public class LengthOfLastWord {
22+
23+
/**
24+
* Returns the length of the last word in the specified string.
25+
*
26+
* <p>The method iterates from the end of the string, skipping trailing
27+
* spaces first, and then counts the number of consecutive non-space characters
28+
* characters until another space (or the beginning of the string) is reached.
29+
*
30+
* @param s the input string to analyze
31+
* @return the length of the last word in {@code s}; returns 0 if there is no word
32+
* @throws NullPointerException if {@code s} is {@code null}
33+
*/
34+
public int lengthOfLastWord(String s) {
35+
int sizeOfString = s.length() - 1;
36+
int lastWordLength = 0;
37+
38+
// Skip trailing spaces from the end of the string
39+
while (sizeOfString >= 0 && s.charAt(sizeOfString) == ' ') {
40+
sizeOfString--;
41+
}
42+
43+
// Count the characters of the last word
44+
while (sizeOfString >= 0 && s.charAt(sizeOfString) != ' ') {
45+
lastWordLength++;
46+
sizeOfString--;
47+
}
48+
49+
return lastWordLength;
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class LengthOfLastWordTest {
8+
@Test
9+
public void testLengthOfLastWord() {
10+
assertEquals(5, new LengthOfLastWord().lengthOfLastWord("Hello World"));
11+
assertEquals(4, new LengthOfLastWord().lengthOfLastWord(" fly me to the moon "));
12+
assertEquals(6, new LengthOfLastWord().lengthOfLastWord("luffy is still joyboy"));
13+
assertEquals(5, new LengthOfLastWord().lengthOfLastWord("Hello"));
14+
assertEquals(0, new LengthOfLastWord().lengthOfLastWord(" "));
15+
assertEquals(0, new LengthOfLastWord().lengthOfLastWord(""));
16+
assertEquals(3, new LengthOfLastWord().lengthOfLastWord("JUST LIE "));
17+
}
18+
}

0 commit comments

Comments
 (0)