This assignment simply requests that you take the quicksort algorithm that you h
ID: 3804832 • Letter: T
Question
This assignment simply requests that you take the quicksort algorithm that you have been working with and make it generic. You are to demonstrate the effectiveness of your work by defining a main method that passes a list of Integers, Doubles, and Strings and sorts them all.
The package should be genericSort.
The quicksort i was working is..
Your task in this assignment is to use the quicksort algorithm to sort the women's names in the file. The file accompanying the problem is a binary file. You are to read the contents of the binary file into either a string array or an ArrayList. If you are using an array, you will have to determine the size of the list to create the correct array.
The binary file has a list of names that your program is to sort and display in ascending alphabetical order.
Finally give the user an opportunity to search for any name in the list using the most efficient search algorithm discussed in this chapter. Use of dialogboxes is encouraged for a nicer looking user interface (UI).
The package name should be names.
Explanation / Answer
import java.util.*;
class QuicksortGeneric {
public static <T extends Comparable<T>> void quicksort(T[] arr, int x, int y) {
if (x < y) {
int i = x, j = y;
T z = ar[(i + j) / 2];
do {
while (ar[i].compareTo(x) < 0) i++;
while (z.compareTo(ar[j]) < 0) j--;
if ( i <= j) {
T tmp = ar[i];
ar[i] = ar[j];
ar[j] = tmp;
i++;
j--;
}
} while (i <= j);
quicksort (ar, x, j);
quicksort (ar, i, y);
}
}
public static void main(String[] a) {
Integer[] ints = {12, 23, 16, 15, 26, 51};
QuicksortGeneric.<Integer>quicksort(ints, 0, 5);
for(Integer i: ints) {
System.out.println(i);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.