public class QuickSort
{
public void quickSort(int[] data, int left, int right, boolean isDesc)
{
if(left < right)
{
int middle = getMiddle(data, left, right, isDesc);
quickSort(data, left, middle-1, isDesc);
quickSort(data, middle+1, right, isDesc);
}
}
private int getMiddle(int[] data, int left, int right, boolean isDesc)
{
int temp = data[left];
while(left < right)
{
if(isDesc == false)
{
while((left < right)&&(data[right] >= temp))
{
right--;
}
data[left] = data[right];
while((left < right)&&(data[left] <= temp))
{
left++;
}
data[right] = data[left];
}
else
{
while((left < right)&&(data[right] <= temp))
{
right--;
}
data[left] = data[right];
while((left < right)&&(data[left] >= temp))
{
left++;
}
data[right] = data[left];
}
}
data[left] = temp;
return left;
}
private void swap(int[] data, int m, int n)
{
int temp = data[m];
data[m] = data[n];
data[n] = temp;
}
}
Quick Sort
最新推荐文章于 2024-05-13 15:54:23 发布