WRITE THIS IN JAVA Pick the easiest sorting technique. Candidates are heap, quic
ID: 3771549 • Letter: W
Question
WRITE THIS IN JAVA
Pick the easiest sorting technique. Candidates are heap, quick, shell, cocktail, bucket, or radix sorts. The method takes in a string array, which contains strings (not ints) (strings such as names) and sorts them alphabetically from A-Z, ignoring the cases. Also I want to know the number of comparisons and swaps that the method makes so create a variable to count the number or compares and create another variable to count the number of swaps that the method makes.
Example: String arrayNames = {"Zori", "aron", "John”};
Your sorting method will take in arrayName as an argument and sort or order the array alphabetically. So after the sorting method executes, the arrayNames would have {"aron", "John", "Zori”};
Explanation / Answer
private static void quickSort(String[] n, int start, int end)
{
int i = start; // index for the "left-to-right scan"
int j = end; // index for the "right-to-left scan"
if (j - i >= 1)
{
String pivot = n[i];
while (j > i)
{
while (n[i].compareTo(pivot) <= 0 && i < end && j > i) {
i++;
}
While (n[j].compareTo(pivot) >= 0 && j > start && j >= i){
j--;
}
if (j > i) // check the two elements in the center, the last comparison before the scans cross.
swap(n, i, j);
}
swap(n, start, j);
// sort left partition
quickSort(n, start, j - 1);
// sort right partition
quickSort(n, j + 1, end);
}
}
//*quickSort method's need to swap two elements */
private static void swap(String[] n, int i, int j)
{
String temp = n[i];
n[i] = n[j];
n[j] = temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.