高级排序中的快速排序
数据排序
运行了划分的思想,先来了解一下划分的思想,如图下所示
快速排序的有个很重要的问题:如何决定轴?
一般会有以下两种解决方案:1.取最数组的最右的
2.最右、最左、中间三个数中取数值排中间的那个
稳定性,轴在排完序之后是位置是确定的。
代码:
public int partitionIt(int left, int right, long pivot){
int leftPtr = left - 1; // right of first elem
int rightPtr = right + 1; // left of pivot
while(true){
while(leftPtr < right && // find bigger item
theArray[++leftPtr] < pivot)
; // (nop)
while(rightPtr > left && // find smaller item
theArray[--rightPtr] >pivot)
; // (nop)
if(leftPtr >= rightPtr) // if pointers cross,
break; // partition done
else // not crossed, so
swap(leftPtr, rightPtr); // swap elements
} // end while(true)
return leftPtr; // return partition
} // end partitionIt()
第一方案代码实现
public void recQuickSort(int left, int right){
if(right-left <= 0) // if size <= 1,
return; // already sorted
else // size is 2 or larger
{
long pivot = theArray[right]; // rightmost item
// partition range
int partition = partitionIt(left, right, pivot);
recQuickSort(left, partition-1); // sort left side
recQuickSort(partition+1, right); // sort right side
}
} // end recQuickSort()
//--------------------------------------------------------------
public int partitionIt(int left, int right, long pivot){
int leftPtr = left-1; // left (after ++)
int rightPtr = right; // right-1 (after --)
while(true){ // find bigger item
while( theArray[++leftPtr] < pivot )
; // (nop)
// find smaller item
while(rightPtr > 0 && theArray[--rightPtr] > pivot)
; // (nop)
if(leftPtr >= rightPtr) // if pointers cross,
break; // partition done
else // not crossed, so
swap(leftPtr, rightPtr); // swap elements
} // end while(true)
swap(leftPtr, right); // restore pivot
return leftPtr; // return pivot location
} // end partitionIt()
第二方案代码实现
public void quickSort(){
recQuickSort(0, nElems-1);
}
//--------------------------------------------------------------
public void recQuickSort(int left, int right){
int size = right-left+1;
if(size <= 3) // manual sort if small
manualSort(left, right); //小于等于3的,单独排序
else // quicksort if large
{
long median = medianOf3(left, right);
int partition = partitionIt(left, right, median);
recQuickSort(left, partition-1);
recQuickSort(partition+1, right);
}
} // end recQuickSort()
//--------------------------------------------------------------
//找出left ,right,center三个,比较大小,交换,确保theArray[left]<=theArray[center] //<=theArray[right]的数单独递增spaw(center,right-1)返回theArray[right-1]
public long medianOf3(int left, int right)
{
int center = (left+right)/2;
// order left & center
if( theArray[left] > theArray[center] )
swap(left, center);
// order left & right
if( theArray[left] > theArray[right] )
swap(left, right);
// order center & right
if( theArray[center] > theArray[right] )
swap(center, right);
swap(center, right-1); // put pivot on right
return theArray[right-1]; // return median value
} // end medianOf3()
//--------------------------------------------------------------
public void swap(int dex1, int dex2) // swap two elements{
long temp = theArray[dex1]; // A into temp
theArray[dex1] = theArray[dex2]; // B into A
theArray[dex2] = temp; // temp into B
} // end swap(
//--------------------------------------------------------------
//进行划分,left,right,right-1都已有序,与上面的划分方法无异
public int partitionIt(int left, int right, long pivot){
int leftPtr = left; // right of first elem
int rightPtr = right - 1; // left of pivot
while(true){
while( theArray[++leftPtr] < pivot ) // find bigger
; // (nop)
while( theArray[--rightPtr] > pivot ) // find smaller
; // (nop)
if(leftPtr >= rightPtr) // if pointers cross,
break; // partition done
else // not crossed, so
swap(leftPtr, rightPtr); // swap elements
} // end while(true)
swap(leftPtr, right-1); // restore pivot
return leftPtr; // return pivot location
} // end partitionIt()
//--------------------------------------------------------------
public void manualSort(int left, int right){
int size = right-left+1;
if(size <= 1)
return; // no sort necessary
if(size == 2){ // 2-sort left and right
if( theArray[left] > theArray[right] )
swap(left, right);
return;
}
else // size is 3
{ // 3-sort left, center, & right
if( theArray[left] > theArray[right-1] )
swap(left, right-1); // left, center
if( theArray[left] > theArray[right] )
swap(left, right); // left, right
if( theArray[right-1] > theArray[right] )
swap(right-1, right); // center, right
}
} // end manualSort()