C. Using the starter code please modify in Java public class L08 { public static void main(String[] args) { // TO DO: // Test your radix sort implementation here } // TO DO: // Implement // NOTE: You can assume that all strings in the array have the same length public static void radixSort(String[] array) { } //////* HELPER METHODS *////// private static String[] generateStringArray(int wordLen, int arrLen) { String[] array = new String[arrLen]; for(int i = 0; i < arrLen; i++) { StringBuilder sb = new StringBuilder(); for(int j = 0; j < wordLen; j++) { sb.append((char)('a' + Math.random() * ('z' - 'a' + 1))); } array[i] = sb.toString(); } return array; } private static void displayStringArray (String[] array) { for(String s : array) { System.out.println(s); } } private static boolean isSortedInNondescendingOrder(String[] array) { for(int i = 1; i < array.length; i++) { if (array[i].compareTo(array[i-1]) < 0) { return false; } } return true; } } Implement radix sort for an array of strings with identical lengths, and test your implementation. The starter code has code methods for generating the arrays, programmatically checking whether the array is sorted, and displaying the strings in a way that makes is easy to "eyeball" results. Pseudo-code for radix-sort on an array of strings of identical length: RADIX-SORT(A): for c = last character to first use ANY stable sort to sort array A on character c You have already practiced implementing sorts (including stable ones) for integers, so you should be able to easily adapt it for the char data type. But remember: whereas in the implementations you did last week "key" and "element" were one and the same, for this lab they are not. This time the whole string is the element, but only of the characters in it will be key during each iteration of the main for loop. Which character acts as the key will change from one iteration to the next.
C.
Using the starter code please modify in Java
public class L08 {
public static void main(String[] args) {
// TO DO:
// Test your radix sort implementation here
}
// TO DO:
// Implement
// NOTE: You can assume that all strings in the array have the same length
public static void radixSort(String[] array) {
}
//////* HELPER METHODS *//////
private static String[] generateStringArray(int wordLen, int arrLen) {
String[] array = new String[arrLen];
for(int i = 0; i < arrLen; i++) {
StringBuilder sb = new StringBuilder();
for(int j = 0; j < wordLen; j++) {
sb.append((char)('a' + Math.random() * ('z' - 'a' + 1)));
}
array[i] = sb.toString();
}
return array;
}
private static void displayStringArray (String[] array) {
for(String s : array) {
System.out.println(s);
}
}
private static boolean isSortedInNondescendingOrder(String[] array) {
for(int i = 1; i < array.length; i++) {
if (array[i].compareTo(array[i-1]) < 0) {
return false;
}
}
return true;
}
}
Implement radix sort for an array of strings with identical lengths, and test your implementation.
The starter code has code methods for generating the arrays, programmatically checking whether the array is sorted, and displaying the strings in a way that makes is easy to "eyeball" results.
Pseudo-code for radix-sort on an array of strings of identical length:
RADIX-SORT(A):
for c = last character to first
use ANY stable sort to sort array A on character c
You have already practiced implementing sorts (including stable ones) for integers, so you should be able to easily adapt it for the char data type.
But remember: whereas in the implementations you did last week "key" and "element" were one and the same, for this lab they are not. This time the whole string is the element, but only of the characters in it will be key during each iteration of the main for loop. Which character acts as the key will change from one iteration to the next.
Step by step
Solved in 3 steps