Question

This assignment simply requests that you take the quicksort algorithm that you have been working with...

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 the main method that passes a list of Integers, Doubles, and Strings and sorts them all.

NOTE: this is the code, please tell me which one of the blocks of code does.

import java.util.*;

public class Driver{

public static <T extends Comparable<T>> void quickSort(T[] data, int a, int b) {

if (a < b) {

int i = a, j = b;

T x = data[(i + j) / 2];

do {

while (data[i].compareTo(x) < 0) i++;

while (x.compareTo(data[j]) < 0) j--;

if ( i <= j) {

T tmp = data[i];

data[i] = data[j];

data[j] = tmp;

i++;

j--;

}

} while (i <= j);

quickSort(data, a, j);

quickSort(data, i, b);

}

}

public static void main(String args[]) {

String[] names = {"John", "Paul", "Anna", "Rock"};

Driver.<String>quickSort(names, 0, names.length-1);

for(String i: names) {

System.out.println(i);

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

public class Driver {
    public static <T extends Comparable<T>> void quickSort(T[] data, int a, int b) {
        if (a < b) {
            int i = a, j = b;
            T x = data[(i + j) / 2];
            do {
                while (data[i].compareTo(x) < 0) i++;
                while (x.compareTo(data[j]) < 0) j--;
                if (i <= j) {
                    T tmp = data[i];
                    data[i] = data[j];
                    data[j] = tmp;
                    i++;
                    j--;
                }
            } while (i <= j);
            quickSort(data, a, j);
            quickSort(data, i, b);
        }
    }

    public static void main(String args[]) {
        String[] names = {"John", "Paul", "Anna", "Rock"};
        Driver.quickSort(names, 0, names.length - 1);
        System.out.println("Strings in sorted order are");
        for (String i : names) {
            System.out.println(i);
        }
        System.out.println();

        Integer[] integers = {4, 0, 1, 2, 9, 5};
        Driver.quickSort(integers, 0, integers.length - 1);
        System.out.println("Integers in sorted order are");
        for (Integer i : integers) {
            System.out.println(i);
        }
        System.out.println();

        Double[] doubles = {9.5, 2.0, 5.6, 8.2, 3.7};
        Driver.quickSort(doubles, 0, doubles.length - 1);
        System.out.println("Doubles in sorted order are");
        for (Double i : doubles) {
            System.out.println(i);
        }
        System.out.println();
    }
}

Strings in sorted order are Anna John Rock Integers in sorted order are Doubles in sorted order are 2. 0 5.6 8.2 9.5

Add a comment
Know the answer?
Add Answer to:
This assignment simply requests that you take the quicksort algorithm that you have been working with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • 7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the...

    7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...

  • When my quicksort is passed a large array of words, i get a stackOverflow error, what...

    When my quicksort is passed a large array of words, i get a stackOverflow error, what can i do so it doesnt end up in an infinite recursive loop public void quickSort(String[] words, int low, int high) { if(low high) { int p = partition (words, low, high); quickSort(words, low,--p); quickSort(words, ++p, high); private int partition(String[] words, int low, int high) { int i = low - 1; for(int j = low; j < high; j++) { if(words[j].compareTo(words[high]) < 0)...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

  • Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick...

    Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code where required import java.util.Random; public class QuickSort public void quickSectlinti] A) QuickSort/A, O, A.length-1); private void guickSortlin Aiat low.int high) //Complete the code for the quicksort method (5 marks] private void swaplint[] a, int indexl, int index2) //Complete the code for the swap method [3 marks] private int setPivotlint low, int high) Random rand = new Random();...

  • I currently have this but it doesn't work for the three item arrays public class Quicksort...

    I currently have this but it doesn't work for the three item arrays public class Quicksort extends Partitioner { public static void quicksort(int[] values) { if (values == null || values.length < 2) { return; } quicksort(values, 0, values.length - 1); } private static void quicksort(int[] values, int start, int end) { System.out.println(values); System.out.println(start); System.out.println(end); if (values == null || Math.abs(start - end) == 1) { return; } if (end > start) { int pivotValueIndex = partition(values, start, end); quicksort(values,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT