snippetjavaMinor
Quicksort for strings in Java
Viewed 0 times
stringsforjavaquicksort
Problem
I have this implementation of Quicksort for strings. The algorithm sorts the requested range by first character, then by second, third, and so on. (Please, do not confuse this with radix sort; it is not.)
The pseudocode might look like this:
StringQuicksort.java:
```
package net.coderodde.util;
import java.util.Arrays;
import java.util.Random;
/**
* This class implements a Quicksort for strings.
*
* @author Rodion "rodde" Efremov
* @version (Dec 17, 2015)
*/
public class StringQuicksort {
private static final int ALPHABET_SIZE = 26;
public static void sort(String[] array) {
sort(array, 0, array.length);
}
public static void sort(String[] array, int fromIndex, int toIndex) {
if (toIndex - fromIndex > 1];
String pivot = median(probeLeft, probeMiddle, probeRight);
// Process strings S for which S[stringLength] < X[stringLength].
for (int index = fromIndex; index < toIndex; ++index) {
String current = array[index];
if (current.charAt(stringLength) < pivot.charAt(stringLength)) {
String tmp = array[finger];
array[finger] = current;
array[index] = tmp;
++finger;
}
}
sortImpl(array, fromIndex, finger, stringLength);
fromIndex = finger;
int processed = 0;
for (int index = fromIndex; index < toIndex; ++index) {
String current = array[index];
if (current.charAt(stringLength) == pivot.charAt(stringLength)) {
String tmp = array[finger];
array[finger] = current;
array[index] = tmp;
++finger;
}
}
sortImpl(array, fromIndex, finger, stringLength + 1);
sortImpl(array, f
The pseudocode might look like this:
# Public API
Sort(R):
Sort(R, 0)
Sort(R, len):
if |R| = all strings S for which S[len] > X[len]
R = Sort(R>, len)
return > # Concatenate sorted sublistsStringQuicksort.java:
```
package net.coderodde.util;
import java.util.Arrays;
import java.util.Random;
/**
* This class implements a Quicksort for strings.
*
* @author Rodion "rodde" Efremov
* @version (Dec 17, 2015)
*/
public class StringQuicksort {
private static final int ALPHABET_SIZE = 26;
public static void sort(String[] array) {
sort(array, 0, array.length);
}
public static void sort(String[] array, int fromIndex, int toIndex) {
if (toIndex - fromIndex > 1];
String pivot = median(probeLeft, probeMiddle, probeRight);
// Process strings S for which S[stringLength] < X[stringLength].
for (int index = fromIndex; index < toIndex; ++index) {
String current = array[index];
if (current.charAt(stringLength) < pivot.charAt(stringLength)) {
String tmp = array[finger];
array[finger] = current;
array[index] = tmp;
++finger;
}
}
sortImpl(array, fromIndex, finger, stringLength);
fromIndex = finger;
int processed = 0;
for (int index = fromIndex; index < toIndex; ++index) {
String current = array[index];
if (current.charAt(stringLength) == pivot.charAt(stringLength)) {
String tmp = array[finger];
array[finger] = current;
array[index] = tmp;
++finger;
}
}
sortImpl(array, fromIndex, finger, stringLength + 1);
sortImpl(array, f
Solution
Both public
Instead of having the comment
you should extract this looping to a separate well named method making the comment removeable. While we are at the looping, the 3 loops with swapping have most code in common except for the
In this way that duplicated code could be removed.
sort() methods should check if array == null otherwise you risk a IndexOutOfBoundsException inside the private sortImpl() method. Instead of having the comment
// Put all strings of length 'stringLength' to the beginning of the
// requested sort range.you should extract this looping to a separate well named method making the comment removeable. While we are at the looping, the 3 loops with swapping have most code in common except for the
if condition when to swap. Maybe you can use some Java 8 vodoo like java.util.function.Function to pass to that said method a parameter which does the condition check for you. In this way that duplicated code could be removed.
Code Snippets
// Put all strings of length 'stringLength' to the beginning of the
// requested sort range.Context
StackExchange Code Review Q#114280, answer score: 5
Revisions (0)
No revisions yet.