<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">public class QuickSort {

    public static void main(String[] args) {
	int length=5,min=0,max=9;
	Comparable[] a = createArray(min,max,length);
	printArray(a);	
	quickSort(a,0,length-1);
	printArray(a);
    }
    
    public static void printArray(Comparable[] a) {
        for (int i = 0; i &lt; a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
    
    public static Comparable[] createArray(int min, int max, int length) {
        Comparable[] a = new Comparable[length];
        for (int i = 0; i &lt; a.length; i++)
            a[i] = MyMath.randInteger(min,max);
	return a;
    }
    
    public static int partition(Comparable[] x, int low, int high, Comparable pivot) {
	
	int i = low;
	int j = high;
	
	boolean flag = true;
	while (flag) {
	    
	    // advance left index to the right:
	    while ( (i &lt; x.length) &amp;&amp; (x[i].compareTo(pivot) &lt; 0) ) 
		i++; 
	    
	    // advance right index to the left:
	    while ( (j &gt;= 0) &amp;&amp; (pivot.compareTo(x[j]) &lt; 0) ) 
		j--; 
	    
	    //swap:
	    if (i &lt; j) { 
		Comparable temp = x[i];
		x[i] = x[j];
		x[j] = temp;
		i++;
		j--;
	    } 

	    // indicies have "met" (i==j), so partitioning:
	    else 
		flag=false;
	}
	return i;
    }
    
    // sort x[a..b]
    public static void quickSort(Comparable[] x, int a, int b) {
	if (a &lt; b) {
	    // pick "random" pivot and partition x:
	    int p = partition(x,a+1,b,x[a]); 

	    // move pivot into its final resting place
	    // swap x[p-1] and x[a]:
	    Comparable temp = x[p-1]; // indicies: a..p-1,p,p+1..b
	    x[p-1] = x[a];
	    x[a] = temp;

	    // sort left and right partitions:
	    // indicies: a..p-1,p,p+1..b
	    quickSort(x,a,p-1);
	    quickSort(x,p,b);
	}
    }
}

class MyMath {
    // Return a random int between low and high, inclusive:
    public static int randInt(int low, int high) {
        return (int) (Math.random()*(high-low+1)) + (int) low; }
    public static Integer randInteger(int low, int high) {
        return new Integer(randInt(low,high)); }
}
</pre></body></html>