快速排序实践

public final static void quickSort2(int[] array1,int start,int end) {
if (start < end) {
int baseindex = getBaseIndex(array1,start,end);
quickSort2(array1,start,baseindex-1);
quickSort2(array1,baseindex +1 ,end);
}
}

/**
* 根据基数分组后得到基数的位置下标
* 参数会被修改。
*/
public final static int getBaseIndex(int[] array1,int start,int end) {
int baseValue = array1[start];
while (start < end) {
while ((start < end )&&(array1[end] >= baseValue)) {
end--;
}
array1[start]=array1[end];
while ((start < end )&&(array1[start] <= baseValue)) {
start++;
}
array1[end]=array1[start];
}
array1[start] = baseValue;
return start;

}

/**
* 根据基数分组后得到基数的位置下标
* 参数会被修改。
*/
public final static int getBaseIndex(int[] array1,int start,int end) {
int baseValue = array1[start];
while (start < end) {
if (array1[end] >= baseValue) {
array1[start]=array1[end];
end--;
}else {
array1[end]=array1[start];
start++;
}
}
array1[start] = baseValue;
return start;
}



public void quickSort2(int a[]){
if(a==null||a.length<=0)return;
Stack<Integer> index=new Stack<Integer>();
int start=0;
int end=a.length-1;

int pivotPos;

index.push(start);
index.push(end);

while(!index.isEmpty()){
end=index.pop();
start=index.pop();

pivotPos=partition(a,start,end);
if(start<pivotPos-1){
index.push(start);
index.push(pivotPos-1);
}
if(end>pivotPos+1){
index.push(pivotPos+1);
index.push(end);
}
}
}

你可能感兴趣的:(快速排序实践)