1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
   |  
___________________________________________________________
package part1e;
 
/*
 * Class QSort without raw types
 */
 
public class QSort extends Sortable  {
 
 
 
	public static <T extends Comparable<? super T>> void quickSort (T[] a, int from, int to) {
		  // Sort a[fromÖto] 
		    if (from < to) {
		      int p = partition(a, from, to);
		      quickSort(a, from, p-1);
		      quickSort(a, p+1, to);
		    }
		  }
 
		 public static <T extends Comparable<? super T>> void quickSort (T[] a) {
		  // Sort a[fromÖto] without indices as parameters
		     int from=0;
		     int to = a.length-1;
		     if (from < to) {
		      int p = partition(a, from, to);
		      quickSort(a, from, p-1);
		      quickSort(a, p+1, to);
		    }
		  }
 
		  private static <T extends Comparable<? super T>> int partition(T[] a,int from, int to) {
		  // Partition a[fromÖto] such that a[fromÖp-1] are all less than
		  // or equal to a[p] and a[p+1Öto] are all greater than or equal to
		  // a[p].
 
		    T pivot = a[from]; int p = from;
		    for (int r = from+1; r <= to; r++) {
		     int comp = a[r].compareTo(pivot);
		     if (comp < 0) {
		      a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot;
		      p++;
		     }
		    }
		    return p;
		  }
 
		  public static <T extends Comparable<? super T>> void test(T[] a,int from, int to){
		  //Test() method to know of the array is sorted
		      boolean b =true;
 
		      while((from<to-1)&&(b==true)){
		          if(a[from].compareTo(a[from+1])>=0){
		              b=false;
		          }
		          from++;
		      }
		      if(b==true)
		          System.out.println("The array is sorted");
		      else
		          System.out.println("The array is not sorted");
		  }
 
 
 
  public static void main(String[] args) {
    Integer[]  aInts = {51, 34, 54, 26, 74, 67, 68, 29, 2, 8, 45};
    test(aInts,0,10);
    QSort.quickSort(aInts,0,10);
    System.out.println("Sorted Integers:");
    for (Integer c : aInts) {
     System.out.print(c + "  ");
   }
   System.out.println();
   test(aInts,0,10);
 
  }
 
}
 
___________________________________________________________
 
package part1f;
 
public interface Sortable {
 
	public  abstract <T extends Comparable<? super T>> void quickSort (T[] a, int from, int to) ;
	public  abstract <T extends Comparable<? super T>> void quickSort (T[] a);
 
}
 
____________________________________________________________ | 
Partager