/*
* 快速排序是最流行的排序算法本质上通过把一个数组划分为两个子数组
* 然后递归地调用自身为每一个子数组进行快速排序来实现
* ArrayInsjava
* l
*/
package linzhanghuiquicksort;
public class ArrayIns {
private long[] theArray;
private int nElems;
public ArrayIns(int max) {
theArray = new long[max];
nElems = ;
}
public void insert(long value) {
theArray[nElems] = value;
nElems++;
}
public void display() {
Systemoutprint(A=)
for(int j=; j<nElems; j++)
Systemoutprint(theArray[j] + )
Systemoutprintln()
}
public void quickSort() {
recQuickSort( nElems)
}
public void recQuickSort(int left int right) {
if(rightleft <= )
return;
else {
long pivot = theArray[right];
int partition = partitionIt(left right pivot)
recQuickSort(left partition)
recQuickSort(partition+ right)
}
}
public int partitionIt(int left int right long pivot) {
int leftPtr = left;
int rightPtr = right;
while(true) {
while( theArray[++leftPtr] < pivot)
;
while(rightPtr > && theArray[rightPtr] > pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr rightPtr)
}
swap(leftPtr right)
return leftPtr;
}
public void swap(int dex int dex) {
long temp = theArray[dex];
theArray[dex] = theArray[dex];
theArray[dex] = temp;
}
}
/*
* 程序随机产生个位随机数显示这个随机数后对其进行快速排序并输出
* l
*/
package linzhanghuiquicksort;
public class QuickSortApp {
public static void main(String[] args) {
int maxSize = ;
ArrayIns arr;
arr = new ArrayIns(maxSize)
for(int j=; j<maxSize; j++) {
long n = (int)(javalangMathrandom()*)
arrinsert(n)
}
arrdisplay()
arrquickSort()
arrdisplay()
}
}