private void quickSort(int[] arr,int begin,int end)
{
if(begin>=end)
return ;
int low=begin;
int high=end;
int index=begin;
int temp=arr[begin];
boolean flag=false;//当flag的值为true向前,否则向后
while(low!=high)
{
if(flag)
{
for(;low<high;low++)
{
if(arr[low]>temp)
{
arr[high]=arr[low];
high--;
index=low;
flag=false;
break;
}
}
}
else
{
for(;high>low;high--)
{
if(arr[high]<temp)
{
arr[low]=arr[high];
low++;
index=high;
flag=true;
break;
}
}
}
}
arr[high]=temp;
if(high==low)
{
quickSort(arr,begin,high-1);
quickSort(arr,high+1,end);
}
}
@Test
public void testquicksort()
{
int[] arr={49,38,65,97,76,13,27,49};
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
{
if(begin>=end)
return ;
int low=begin;
int high=end;
int index=begin;
int temp=arr[begin];
boolean flag=false;//当flag的值为true向前,否则向后
while(low!=high)
{
if(flag)
{
for(;low<high;low++)
{
if(arr[low]>temp)
{
arr[high]=arr[low];
high--;
index=low;
flag=false;
break;
}
}
}
else
{
for(;high>low;high--)
{
if(arr[high]<temp)
{
arr[low]=arr[high];
low++;
index=high;
flag=true;
break;
}
}
}
}
arr[high]=temp;
if(high==low)
{
quickSort(arr,begin,high-1);
quickSort(arr,high+1,end);
}
}
@Test
public void testquicksort()
{
int[] arr={49,38,65,97,76,13,27,49};
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}