Posted by: greencookie April 30, 2008
Java le DusPutra (tenson) diyo!
Login in to Rate this Post:     0       ?        
Hello friends,

I would like your help and assistance if possible on my final Java program. Unfortunately I am ashamed to say that I haven't yet learnt the basics of java entirely so forgive me if any of my questions sound 'noobish' or wierd.

Here is the assignment:

# Implement a quicksort that selects the median of the first, middle and last as the pivot.
#Implement the sort so that it uses an insertion sort whenever the number of items to be sorted is less than some constant (i.e 20). you can choose to either build the insertion sort directly into the quicksort or make it a separate method.
#use java's random number generator to fill an array with random integers. Be certain to use the seed operation to always generate exactly the same set of random numbers for repeatability of  your results.

Now so far here is what I have for my QuickSort.java program :

/** Implements the quicksort algorithm. */

public class  QuickSort {
    public static <T extends Comparable<T>> void sort(T[] table) {      
        quickSort(table,0,table.length - 1);
    }
    private static <T extends Comparable<T>> void quickSort(T[] table,int first, int last) {
        if (first < last) {
            int pivIndex = partition(table,first,last);
            quickSort(table,first,pivIndex - 1);
            quickSort(table,pivIndex +1,last);
        }
    }
    private static <T extends Comparable<T>> int partition(T[] table,int first,int last) {
        bubbleSort3(table,first,last);
        swap(table,first,(first+last)/2);
            T pivot = table[first];
        int up = first;
        int down = last;
        do {
            while ((up<last)&&(pivot.compareTo(table[up]) >= 0)) {
                up++;
            }
            while (pivot.compareTo(table[down]) <0) {
                down--;
            }
            if (up<down) {
                swap(table,up,down);
            }
        }
        while (up<down);
        swap(table,first,down);
        return down;
    }
   
        private static <T extends Comparable<T>> void bubbleSort3(T[] table,int first,int last) {
            int middle = (first + last) / 2;
            if (table[middle].compareTo(table[first])<0) {
                swap(table,first,middle);
            }
            if (table[last].compareTo(table[middle])<0) {
                swap(table,middle,last);
            }
            if (table[middle].compareTo(table[first])<0) {
                swap(table,first,middle);
            }
        }
   
}

compiling this results in a couple of errors. Mainly its not recognizing the swap method. Do I have to import something?

Secondly, I dont know how to insert the random number seeder into this program. If any of you have ideas, they are more than welcome!!

Thanks in advance!!!


Read Full Discussion Thread for this article