2016-11-09 4 views
0

Ich bin sehr verwirrt mit der Weitergabe. Ich habe in Eclipse einen schnellen Sortieralgorithmus erstellt. Die Klasse ist eine abstrakte Klasse. Hier ist die Interface-Klasse.Übergeben Generic Quick sort

public interface ArraySort<T extends Comparable<T>> 
{ 
    /** 
    * Sort the array 
    */ 
    public void sort(T[] array); 

} 

Dies ist die Klasse, in der die Schnellsortierung erstellt wurde.

public class QuickSort <T extends Comparable<T>> extends ArraySortTool<T>{ 
    public <T> void quickSort(T[] array, Comparator<T>com, int a, int b) { 
     if(a >= b) return; 
     int left = a; 
     int right = b-1; 
     T pivot = array[b]; 
     T temp; 

     while (left <= right){ 

      //Look for element larger or equal to the pivot 
      while(left <= right&&com.compare(array[left], pivot)<0)left++; 
      //Look for element smaller or equal to pivot 
      while(left <= right&&com.compare(array[right], pivot)>0)right--; 
      if(left <= right){ 
       temp = array[left]; array[right]=array[right]=temp; 
       left++; right--; 
      } 
     } 

     //place pivot into its final location marked by left index 
     temp = array[left]; array[left] = array[b]; array[b] = temp; 

     quickSort(array, com, a, left - 1); 
     quickSort(array, com, left + 1,b); 

    } 

    @Override 
    public void sort(T[] array) { 
     quickSort(array, int, 0, 0); 


    } 
} 

Um die Referenzen, die ich auch diese Methode versucht haben, passieren aber kein Glück hatte.

@Override 
    public void sort(T[] array, Comparator<T>com, int a, int b) { 
     int left = a; 
     int right = b-1; 
     T pivot = array[b]; 
     T temp; 

war ich immer einen Fehler hier

public class QuickSort <T extends Comparable<T>> extends ArraySortTool<T>{ 

Ich versuche, dies mit der Interface-Klasse, ohne sich zu tun. Hier

ist der Code für die ArraySortTool

public abstract class ArraySortTool<T extends Comparable<T>> implements ArraySort<T> 
{ 
    /** 
    * @param inArray an array to be sorted 
    * @return the time, in milliseconds, taken to sort the array 
    */ 
    private double timeTakenMillis(T[] array) { 
     double startTime = System.nanoTime(); 
     sort(array); 
     return ((System.nanoTime()-startTime)/1000000.0); 
    } 

    /** 
    * Run a sequence of tests on sets of arrays of increasing size, reporting the average time taken for each 
    * size of array. For each size of array, <tt>noPerSize</tt> tests will be run, and the average time taken. 
    * Timings will be generated for array sizes 1,2,...,9,10,20,...,90,100,200,...,900,1000,2000,...until the 
    * maximum time is exceeded. Times are reported in milliseconds. 
    * @param generator an array generator for generating the random arrays 
    * @param noPerSize the number of timings per array size set 
    * @param maxTimeSeconds the cut-off time in seconds - once a timing takes longer than this the timing sequence will be terminated 
    */ 
    public void timeInMillis(RandomArray<T> generator,int noPerSize,int maxTimeSeconds) 
    { 
     int size = 1; // initial size of array to test 
     int step = 1; // initial size increase 
     int stepFactor = 10; // when size reaches 10*current size increase step size by 10 
     double averageTimeTaken; 
     do { 
      double totalTimeTaken = 0; 
      for (int count = 0; count < noPerSize; count++) { 
       T[] array = generator.randomArray(size); 
       totalTimeTaken += timeTakenMillis(array); 
      } 
      averageTimeTaken = totalTimeTaken/noPerSize; 
      System.out.format("Average time to sort %d elements was %.3f milliseconds.\n",size,averageTimeTaken); 
      size += step; 
      if (size >= stepFactor*step) step *= stepFactor;   
     } while (averageTimeTaken < maxTimeSeconds*1000); 
     System.out.println("Tests ended."); 
    } 

    /** 
    * Check whether a given array is sorted. 
    * @param array the array to be checked 
    * @return true iff the array is sorted - either ascending or descending 
    * The first non-equal neighbouring elements will determine the expected 
    * order of sorting. 
    */ 
    public boolean isSorted(T[] array) { 
     int detectedDirection = 0; // have not yet detected increasing or decreasing 
     T previous = array[0]; 
     for (int index = 1; index < array.length; index++) { 
      int currentDirection = previous.compareTo(array[index]); // compare previous and current entry 
      if (currentDirection != 0) { // if current pair increasing or decreasing 
       if (detectedDirection == 0) { // if previously no direction detected 
        detectedDirection = currentDirection; // remember current direction 
       } else if (detectedDirection * currentDirection < 0) { // otherwise compare current and previous direction 
        return false; // if they differ array is not sorted 
       } 
      } 
      previous = array[index]; 
     } 
     // reached end of array without detecting pairs out of order 
     return true; 
    } 
} 

Ich versuche, die quicksort Methode in die Sortiermethode zu übergeben, wie es in der Interface-Klasse ist. Bitte lassen Sie mich wissen, wie dies zu tun ist, da ich neu bin, durch Bezugnahme zu gehen. Ein Beispiel mit meinem Code wird großartig sein. Mit freundlichen Grüßen.

+0

ich Parsen bin, wo es heißt @Override –

+2

Was bedeutet "Sie Parsen" bedeuten? Ich bezweifle, dass dieser Begriff hier richtig ist. –

+0

Die Schnittstelle ist ArraySort, aber die Klasse erweitert ArraySortTool –

Antwort

0

versuchen Sie dies:

public class QuickSort <T extends Comparable<T>> implements ArraySort<T>{...} 

(bearbeitet in der Frage überarbeiteten Code-Match)

+0

Dieser Code gibt mir einen Fehler auf ArraySortTool –

+0

Ich habe den Code für die ArraySortTool-Klasse hinzugefügt, wenn es hilft –

+0

Ich denke, die einzige Möglichkeit ist es, die Verweise auf die Sortiermethode in der QuickSort-Klasse übergeben, aber ich don ' Ich weiß, wie man das macht –